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 all 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,164 @@
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;
import appeng.util.calculators.ArithHelper;
import appeng.util.calculators.Calculator;

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();
long amount = getAmount();

int restrictionTypes = 0;
long restrictionAmount = 0;

try {
restrictionTypes = Math.min(Integer.parseInt(types), cellData.getTotalTypes());
} catch (Exception ignored) {
//
}
try {
restrictionAmount = Math
.min(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());
String type = cellData.getCellType();
switch (type) {
case "item":
this.fontRendererObj
.drawString(GuiText.NumberOfItems.getLocal(), 64, 23, GuiColors.DefaultBlack.getColor());
break;
case "fluid":
this.fontRendererObj
.drawString(GuiText.NumberOfFluids.getLocal(), 64, 23, GuiColors.DefaultBlack.getColor());
break;
}
this.fontRendererObj.drawString(GuiText.Types.getLocal(), 162, 23, GuiColors.DefaultBlack.getColor());
this.fontRendererObj
.drawString(GuiText.CellRestrictionTips.getLocal(), 64, 50, GuiColors.DefaultBlack.getColor());
switch (type) {
case "item":
this.fontRendererObj.drawString(
GuiText.ItemsPerByte.getLocal() + " " + cellData.getPerByte(),
64,
60,
GuiColors.DefaultBlack.getColor());
break;
case "fluid":
this.fontRendererObj.drawString(
GuiText.FluidsPerByte.getLocal() + " " + cellData.getPerByte() + " mB",
64,
60,
GuiColors.DefaultBlack.getColor());
break;
}
this.fontRendererObj.drawString(
GuiText.BytesPerType.getLocal() + " " + cellData.getPerType(),
64,
70,
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);
}
}

private long getAmount() {
String out = this.amountField.getText();
double resultD = Calculator.conversion(out);

if (resultD <= 0 || Double.isNaN(resultD)) {
return 0;
} else {
return (long) ArithHelper.round(resultD, 0);
}
}
}
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,132 @@
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;
private String cellType;

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 void setCellType(String cellType) {
this.cellType = cellType;
}

public Integer getPerType() {
return perType;
}

public Integer getTotalTypes() {
return totalTypes;
}

public Long getTotalBytes() {
return totalBytes;
}

public Integer getPerByte() {
return perByte;
}

public String getCellType() {
return cellType;
}
}

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(",", 7));
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.cellData.setCellType(newData.get(6));
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
Loading