Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to restrict cell #659

Merged
merged 19 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/appeng/api/config/ActionItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ public enum ActionItems {
TOGGLE_SHOW_ONLY_INVALID_PATTERN_OFF,
HIGHLIGHT_INTERFACE,
EXTRA_OPTIONS,
CELL_RESTRICTION
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package appeng.client.gui.implementations;

import java.io.IOException;

import net.minecraft.entity.player.InventoryPlayer;

import org.lwjgl.input.Keyboard;

import appeng.client.gui.AEBaseGui;
import appeng.client.gui.widgets.MEGuiTextField;
import appeng.container.implementations.ContainerCellRestriction;
import appeng.container.implementations.ContainerCellRestriction.cellData;
import appeng.core.AELog;
import appeng.core.localization.GuiColors;
import appeng.core.localization.GuiText;
import appeng.core.sync.GuiBridge;
import appeng.core.sync.network.NetworkHandler;
import appeng.core.sync.packets.PacketSwitchGuis;
import appeng.core.sync.packets.PacketValueConfig;
import appeng.helpers.ICellRestriction;

public class GuiCellRestriction extends AEBaseGui {

private MEGuiTextField amountField;
private MEGuiTextField typesField;
private cellData cellData;

public GuiCellRestriction(InventoryPlayer ip, ICellRestriction obj) {
super(new ContainerCellRestriction(ip, obj));
this.xSize = 256;

this.amountField = new MEGuiTextField(85, 12);
this.typesField = new MEGuiTextField(30, 12);
this.cellData = new cellData();

}

@Override
public void initGui() {
super.initGui();

this.amountField.x = this.guiLeft + 64;
this.amountField.y = this.guiTop + 32;

this.typesField.x = this.guiLeft + 162;
this.typesField.y = this.guiTop + 32;

if (this.inventorySlots instanceof ContainerCellRestriction ccr) {
ccr.setAmountField(this.amountField);
ccr.setTypesField(this.typesField);
ccr.setCellData(cellData);
}
}

@Override
public void onGuiClosed() {
super.onGuiClosed();
}

public String filterCellRestriction() {

String types = this.typesField.getText();
String amount = this.amountField.getText();

int restrictionTypes = 0;
long restrictionAmount = 0;

try {
restrictionTypes = Math.min(Integer.parseInt(types), cellData.getTotalTypes());
} catch (Exception ignored) {
//
}
try {
restrictionAmount = Math.min(
Long.parseLong(amount),
(cellData.getTotalBytes() - cellData.getPerType()) * cellData.getPerByte());
} catch (Exception ignored) {
//
}
return restrictionTypes + "," + restrictionAmount;
}

@Override
public void drawFG(int offsetX, int offsetY, int mouseX, int mouseY) {
this.fontRendererObj.drawString(GuiText.CellRestriction.getLocal(), 58, 6, GuiColors.DefaultBlack.getColor());
this.fontRendererObj.drawString(GuiText.SelectAmount.getLocal(), 64, 23, GuiColors.DefaultBlack.getColor());
this.fontRendererObj.drawString(GuiText.Types.getLocal(), 162, 23, GuiColors.DefaultBlack.getColor());
this.fontRendererObj
.drawString(GuiText.CellRestrictionTips.getLocal(), 64, 50, GuiColors.DefaultBlack.getColor());
}

@Override
public void drawBG(int offsetX, int offsetY, int mouseX, int mouseY) {
this.bindTexture("guis/cellRestriction.png");
this.drawTexturedModalRect(offsetX, offsetY, 0, 0, this.xSize, this.ySize);
this.amountField.drawTextBox();
this.typesField.drawTextBox();
}

@Override
protected void mouseClicked(final int xCoord, final int yCoord, final int btn) {
this.amountField.mouseClicked(xCoord, yCoord, btn);
this.typesField.mouseClicked(xCoord, yCoord, btn);
super.mouseClicked(xCoord, yCoord, btn);
}

@Override
protected void keyTyped(final char character, final int key) {
if (key == Keyboard.KEY_RETURN || key == Keyboard.KEY_NUMPADENTER) {
try {
NetworkHandler.instance.sendToServer(new PacketValueConfig("cellRestriction", filterCellRestriction()));
} catch (IOException e) {
AELog.debug(e);
}
NetworkHandler.instance.sendToServer(new PacketSwitchGuis(GuiBridge.GUI_CELL_WORKBENCH));
} else if (!(this.amountField.textboxKeyTyped(character, key)
|| this.typesField.textboxKeyTyped(character, key))) {
super.keyTyped(character, key);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import appeng.client.gui.widgets.GuiToggleButton;
import appeng.container.implementations.ContainerCellWorkbench;
import appeng.core.localization.GuiText;
import appeng.core.sync.GuiBridge;
import appeng.core.sync.network.NetworkHandler;
import appeng.core.sync.packets.PacketSwitchGuis;
import appeng.core.sync.packets.PacketValueConfig;
import appeng.tile.misc.TileCellWorkbench;
import appeng.util.Platform;
Expand All @@ -41,6 +43,7 @@ public class GuiCellWorkbench extends GuiUpgradeable {
private GuiImgButton clear;
private GuiImgButton partition;
private GuiToggleButton copyMode;
protected GuiImgButton cellRestriction;

public GuiCellWorkbench(final InventoryPlayer inventoryPlayer, final TileCellWorkbench te) {
super(new ContainerCellWorkbench(inventoryPlayer, te));
Expand Down Expand Up @@ -69,12 +72,18 @@ protected void addButtons() {
this.guiTop + 68,
Settings.ACTIONS,
ActionItems.ORE_FILTER);
this.cellRestriction = new GuiImgButton(
this.guiLeft + 134,
this.guiTop + 8,
Settings.ACTIONS,
ActionItems.CELL_RESTRICTION);

this.buttonList.add(this.fuzzyMode);
this.buttonList.add(this.partition);
this.buttonList.add(this.clear);
this.buttonList.add(this.copyMode);
this.buttonList.add(this.oreFilter);
this.buttonList.add(this.cellRestriction);
}

@Override
Expand Down Expand Up @@ -174,6 +183,7 @@ protected void handleButtonVisibility() {
}
this.fuzzyMode.setVisibility(!hasOreFilter && hasFuzzy);
this.oreFilter.setVisibility(hasOreFilter);
this.cellRestriction.setVisibility(this.workbench.haveCellRestrictAble());
}

@Override
Expand Down Expand Up @@ -207,6 +217,8 @@ protected void actionPerformed(final GuiButton btn) {
fz = Platform.rotateEnum(fz, backwards, Settings.FUZZY_MODE.getPossibleValues());

NetworkHandler.instance.sendToServer(new PacketValueConfig("CellWorkbench.Fuzzy", fz.name()));
} else if (btn == this.cellRestriction) {
NetworkHandler.instance.sendToServer(new PacketSwitchGuis(GuiBridge.GUI_CELL_RESTRICTION));
} else {
super.actionPerformed(btn);
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/appeng/client/gui/widgets/GuiImgButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,13 @@ public GuiImgButton(final int x, final int y, final Enum idx, final Enum val) {
ButtonToolTips.StringOrder,
ButtonToolTips.StringOrderAlphanum);

this.registerApp(
16 * 6 + 8,
Settings.ACTIONS,
ActionItems.CELL_RESTRICTION,
ButtonToolTips.CellRestrictionLabel,
ButtonToolTips.CellRestrictionHint);

}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package appeng.container.implementations;

import java.util.Arrays;
import java.util.List;

import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.tileentity.TileEntity;

import appeng.api.parts.IPart;
import appeng.client.gui.widgets.MEGuiTextField;
import appeng.container.AEBaseContainer;
import appeng.container.guisync.GuiSync;
import appeng.helpers.ICellRestriction;
import appeng.util.Platform;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class ContainerCellRestriction extends AEBaseContainer {

public static class cellData {

private Long totalBytes;
private Integer totalTypes;
private Integer perType;
private Integer perByte;

public void setPerType(Integer perType) {
this.perType = perType;
}

public void setTotalBytes(Long totalBytes) {
this.totalBytes = totalBytes;
}

public void setTotalTypes(Integer totalTypes) {
this.totalTypes = totalTypes;
}

public void setPerByte(Integer perByte) {
this.perByte = perByte;
}

public Integer getPerType() {
return perType;
}

public Integer getTotalTypes() {
return totalTypes;
}

public Long getTotalBytes() {
return totalBytes;
}

public Integer getPerByte() {
return perByte;
}
}

private final ICellRestriction Host;

@SideOnly(Side.CLIENT)
private MEGuiTextField typesField;

@SideOnly(Side.CLIENT)
private MEGuiTextField amountField;

@SideOnly(Side.CLIENT)
private cellData cellData;

@GuiSync(69)
public String cellRestriction;

public ContainerCellRestriction(final InventoryPlayer ip, final ICellRestriction te) {
super(ip, (TileEntity) (te instanceof TileEntity ? te : null), (IPart) (te instanceof IPart ? te : null));
this.Host = te;
}

@SideOnly(Side.CLIENT)
public void setAmountField(final MEGuiTextField f) {
this.amountField = f;
}

@SideOnly(Side.CLIENT)
public void setTypesField(final MEGuiTextField f) {
this.typesField = f;
}

@SideOnly(Side.CLIENT)
public void setCellData(cellData newCellData) {
this.cellData = newCellData;
}

public void setCellRestriction(String data) {
this.Host.setCellRestriction(null, data);
this.cellRestriction = data;
}

@Override
public void detectAndSendChanges() {
if (Platform.isServer()) this.cellRestriction = this.Host.getCellData(null);
super.detectAndSendChanges();
}

@Override
public void onUpdate(final String field, final Object oldValue, final Object newValue) {
if (field.equals("cellRestriction") && (this.amountField != null && this.typesField != null)) {
List<String> newData = Arrays.asList(cellRestriction.split(",", 6));
this.cellData.setTotalBytes(Long.parseLong(newData.get(0)));
this.cellData.setTotalTypes(Integer.parseInt(newData.get(1)));
this.cellData.setPerType(Integer.parseInt(newData.get(2)));
this.cellData.setPerByte(Integer.parseInt(newData.get(3)));
this.typesField.setMaxStringLength(cellData.getTotalTypes().toString().length());
this.amountField.setMaxStringLength(
String.valueOf((cellData.getTotalBytes() - cellData.getPerType()) * cellData.getPerByte())
.length());
this.typesField.setText(newData.get(4));
this.amountField.setText(newData.get(5));
}
super.onUpdate(field, oldValue, newValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import appeng.container.slot.OptionalSlotRestrictedInput;
import appeng.container.slot.SlotFakeTypeOnly;
import appeng.container.slot.SlotRestrictedInput;
import appeng.helpers.ICellRestriction;
import appeng.tile.inventory.AppEngNullInventory;
import appeng.tile.misc.TileCellWorkbench;
import appeng.util.IterationCounter;
Expand Down Expand Up @@ -178,6 +179,14 @@ public IInventory getCellUpgradeInventory() {
return upgradeInventory == null ? this.nullInventory : upgradeInventory;
}

public boolean haveCell() {
return this.workBench.getCell() != null;
}

public boolean haveCellRestrictAble() {
return haveCell() && this.workBench.getCell() instanceof ICellRestriction;
}

@Override
public void onUpdate(final String field, final Object oldValue, final Object newValue) {
if (field.equals("copyMode")) {
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/appeng/core/localization/ButtonToolTips.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,10 @@ public enum ButtonToolTips {

StringOrder,
StringOrderNatural,
StringOrderAlphanum;
StringOrderAlphanum,

CellRestrictionLabel,
CellRestrictionHint;

private final String root;

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/appeng/core/localization/GuiText.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,9 @@ public enum GuiText {
TypesInfo,
BytesInfo,
ToFollow,
ToUnfollow;
ToUnfollow,
CellRestriction,
CellRestrictionTips;

private final String root;

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/appeng/core/sync/GuiBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import appeng.container.ContainerNull;
import appeng.container.ContainerOpenContext;
import appeng.container.implementations.ContainerAdvancedNetworkTool;
import appeng.container.implementations.ContainerCellRestriction;
import appeng.container.implementations.ContainerCellWorkbench;
import appeng.container.implementations.ContainerChest;
import appeng.container.implementations.ContainerCondenser;
Expand Down Expand Up @@ -88,6 +89,7 @@
import appeng.container.implementations.ContainerWireless;
import appeng.container.implementations.ContainerWirelessTerm;
import appeng.core.stats.Achievements;
import appeng.helpers.ICellRestriction;
import appeng.helpers.ICustomNameObject;
import appeng.helpers.IInterfaceHost;
import appeng.helpers.IOreFilterable;
Expand Down Expand Up @@ -217,7 +219,9 @@ public enum GuiBridge implements IGuiHandler {

GUI_RENAMER(ContainerRenamer.class, ICustomNameObject.class, GuiHostType.WORLD, SecurityPermissions.BUILD),

GUI_ORE_FILTER(ContainerOreFilter.class, IOreFilterable.class, GuiHostType.ITEM_OR_WORLD, null);
GUI_ORE_FILTER(ContainerOreFilter.class, IOreFilterable.class, GuiHostType.ITEM_OR_WORLD, null),

GUI_CELL_RESTRICTION(ContainerCellRestriction.class, ICellRestriction.class, GuiHostType.ITEM_OR_WORLD, null);

private final Class tileClass;
private final Class containerClass;
Expand Down
Loading