Small improvements to the Level Maintainer GUI (#283)

This commit is contained in:
Kogepan229 authored and GitHub committed 2025-04-19 10:41:29 -05:00
1 parent c724875af6
commit a4a5dcd9d8
3 files changed
+93 -71

No files matched your search

@@ -2,10 +2,10 @@ package com.glodblock.github.client.gui;
import static com.glodblock.github.client.gui.container.ContainerLevelMaintainer.createLevelValues; import static com.glodblock.github.client.gui.container.ContainerLevelMaintainer.createLevelValues;
import java.text.NumberFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.regex.Pattern;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiButton;
@@ -15,8 +15,10 @@ import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StatCollector;
import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.common.util.ForgeDirection;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.input.Keyboard; import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
@@ -40,7 +42,6 @@ import com.glodblock.github.network.CPacketLevelTerminalCommands;
import com.glodblock.github.util.FCGuiColors; import com.glodblock.github.util.FCGuiColors;
import com.glodblock.github.util.NameConst; import com.glodblock.github.util.NameConst;
import com.glodblock.github.util.Util; import com.glodblock.github.util.Util;
import com.gtnewhorizon.gtnhlib.util.parsing.MathExpressionParser;
import appeng.api.storage.data.IAEItemStack; import appeng.api.storage.data.IAEItemStack;
import appeng.api.util.DimensionalCoord; import appeng.api.util.DimensionalCoord;
@@ -53,11 +54,12 @@ import appeng.core.AELog;
import appeng.core.features.AEFeature; import appeng.core.features.AEFeature;
import appeng.core.sync.network.NetworkHandler; import appeng.core.sync.network.NetworkHandler;
import appeng.core.sync.packets.PacketNEIDragClick; import appeng.core.sync.packets.PacketNEIDragClick;
import appeng.util.calculators.ArithHelper;
import appeng.util.calculators.Calculator;
import codechicken.nei.VisiblityData; import codechicken.nei.VisiblityData;
import codechicken.nei.api.INEIGuiHandler; import codechicken.nei.api.INEIGuiHandler;
import codechicken.nei.api.TaggedInventoryArea; import codechicken.nei.api.TaggedInventoryArea;
import cofh.core.render.CoFHFontRenderer; import cofh.core.render.CoFHFontRenderer;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.Optional; import cpw.mods.fml.common.Optional;
@Optional.Interface(modid = "NotEnoughItems", iface = "codechicken.nei.api.INEIGuiHandler") @Optional.Interface(modid = "NotEnoughItems", iface = "codechicken.nei.api.INEIGuiHandler")
@@ -67,7 +69,7 @@ public class GuiLevelMaintainer extends AEBaseGui implements INEIGuiHandler {
private final ContainerLevelMaintainer cont; private final ContainerLevelMaintainer cont;
private final Component[] component = new Component[TileLevelMaintainer.REQ_COUNT]; private final Component[] component = new Component[TileLevelMaintainer.REQ_COUNT];
private final MouseRegionManager mouseRegions = new MouseRegionManager(this); private final MouseRegionManager mouseRegions = new MouseRegionManager(this);
private FCGuiTextField input; private Widget focusedWidget;
private int lastWorkingTick; private int lastWorkingTick;
private int refreshTick; private int refreshTick;
private final CoFHFontRenderer render; private final CoFHFontRenderer render;
@@ -77,10 +79,6 @@ public class GuiLevelMaintainer extends AEBaseGui implements INEIGuiHandler {
protected Util.DimensionalCoordSide originalBlockPos; protected Util.DimensionalCoordSide originalBlockPos;
protected GuiTabButton originalGuiBtn; protected GuiTabButton originalGuiBtn;
protected static final boolean isGTNHLibLoaded = Loader.isModLoaded("gtnhlib");
protected static final Pattern numberPattern = isGTNHLibLoaded ? MathExpressionParser.EXPRESSION_PATTERN
: Pattern.compile("^[0-9]+");
public GuiLevelMaintainer(InventoryPlayer ipl, TileLevelMaintainer tile) { public GuiLevelMaintainer(InventoryPlayer ipl, TileLevelMaintainer tile) {
super(new ContainerLevelMaintainer(ipl, tile)); super(new ContainerLevelMaintainer(ipl, tile));
this.cont = (ContainerLevelMaintainer) inventorySlots; this.cont = (ContainerLevelMaintainer) inventorySlots;
@@ -152,6 +150,8 @@ public class GuiLevelMaintainer extends AEBaseGui implements INEIGuiHandler {
State state = State.values()[data.getInteger(TLMTags.State.tagName)]; State state = State.values()[data.getInteger(TLMTags.State.tagName)];
component[idx].getQty().textField.setText(String.valueOf(quantity)); component[idx].getQty().textField.setText(String.valueOf(quantity));
component[idx].getBatch().textField.setText(String.valueOf(batch)); component[idx].getBatch().textField.setText(String.valueOf(batch));
component[idx].getQty().validate();
component[idx].getBatch().validate();
component[idx].setEnable(isEnable); component[idx].setEnable(isEnable);
component[idx].setState(state); component[idx].setState(state);
} }
@@ -207,7 +207,7 @@ public class GuiLevelMaintainer extends AEBaseGui implements INEIGuiHandler {
drawTexturedModalRect(offsetX, offsetY, 0, 0, 176, ySize); drawTexturedModalRect(offsetX, offsetY, 0, 0, 176, ySize);
int tick = this.refreshTick; int tick = this.refreshTick;
int interval = 20; int interval = 20;
if (tick > lastWorkingTick + interval && this.input == null) { if (tick > lastWorkingTick + interval && this.focusedWidget == null) {
FluidCraft.proxy.netHandler.sendToServer(new CPacketLevelMaintainer(Action.Refresh)); FluidCraft.proxy.netHandler.sendToServer(new CPacketLevelMaintainer(Action.Refresh));
lastWorkingTick = this.refreshTick; lastWorkingTick = this.refreshTick;
} }
@@ -233,11 +233,11 @@ public class GuiLevelMaintainer extends AEBaseGui implements INEIGuiHandler {
super.func_146977_a(new SlotSingleItem(slot)); super.func_146977_a(new SlotSingleItem(slot));
if (stack == null) return true; if (stack == null) return true;
IAEItemStack fake = stack.copy(); IAEItemStack fake = stack.copy();
if (this.component[slot.getSlotIndex()].getQty().textField.getText().matches("[0-9]+")) {
fake.setStackSize(Long.parseLong(this.component[slot.getSlotIndex()].getQty().textField.getText())); Widget qty = this.component[slot.getSlotIndex()].getQty();
} else { qty.validate();
fake.setStackSize(0); fake.setStackSize(qty.getAmount() != null ? qty.getAmount() : 0);
}
GL11.glTranslatef(0.0f, 0.0f, 200.0f); GL11.glTranslatef(0.0f, 0.0f, 200.0f);
aeRenderItem.setAeStack(fake); aeRenderItem.setAeStack(fake);
aeRenderItem.renderItemOverlayIntoGUI( aeRenderItem.renderItemOverlayIntoGUI(
@@ -255,42 +255,41 @@ public class GuiLevelMaintainer extends AEBaseGui implements INEIGuiHandler {
@Override @Override
protected void mouseClicked(final int xCoord, final int yCoord, final int btn) { protected void mouseClicked(final int xCoord, final int yCoord, final int btn) {
if (btn == 0) { if (btn == 0) {
if (input != null) { if (focusedWidget != null) {
input.setFocused(false); focusedWidget.textField.setFocused(false);
} }
for (Component com : this.component) { for (Component com : this.component) {
FCGuiTextField textField = com.isMouseIn(xCoord, yCoord); Widget textField = com.isMouseIn(xCoord, yCoord);
if (textField != null) { if (textField != null) {
textField.setFocused(true); textField.textField.setFocused(true);
this.input = textField; this.focusedWidget = textField;
super.mouseClicked(xCoord, yCoord, btn); super.mouseClicked(xCoord, yCoord, btn);
return; return;
} }
} }
this.input = null; this.focusedWidget = null;
} }
super.mouseClicked(xCoord, yCoord, btn); super.mouseClicked(xCoord, yCoord, btn);
} }
@Override @Override
protected void keyTyped(final char character, final int key) { protected void keyTyped(final char character, final int key) {
if (this.input == null) { if (this.focusedWidget == null) {
super.keyTyped(character, key); super.keyTyped(character, key);
return; return;
} }
if (!this.checkHotbarKeys(key)) { if (!this.checkHotbarKeys(key)) {
if (!((character == ' ') && this.input.getText().isEmpty())) { if (!((character == ' ') && this.focusedWidget.textField.getText().isEmpty())) {
this.input.textboxKeyTyped(character, key); this.focusedWidget.textField.textboxKeyTyped(character, key);
} }
super.keyTyped(character, key); super.keyTyped(character, key);
if (!numberPattern.matcher(this.input.getText()).matches()) {
this.input.setTextColor(0xFF0000); this.focusedWidget.validate();
} else {
this.input.setTextColor(0xFFFFFF);
}
if (key == Keyboard.KEY_RETURN || key == Keyboard.KEY_NUMPADENTER) { if (key == Keyboard.KEY_RETURN || key == Keyboard.KEY_NUMPADENTER) {
this.input.setFocused(false); this.component[this.focusedWidget.componentIndex].submit();
this.input = null; this.focusedWidget.textField.setFocused(false);
this.focusedWidget = null;
} }
} }
} }
@@ -409,39 +408,38 @@ public class GuiLevelMaintainer extends AEBaseGui implements INEIGuiHandler {
} }
public int getIndex() { public int getIndex() {
return this.qty.idx; return this.qty.componentIndex;
} }
public void setEnable(boolean enable) { public void setEnable(boolean enable) {
this.isEnable = enable; this.isEnable = enable;
} }
private boolean send(Widget widget) { private void send(Widget widget) {
if (((SlotFluidConvertingFake) cont.inventorySlots.get(widget.idx)).getHasStack()) { if (cont.inventorySlots.get(widget.componentIndex).getHasStack() && widget.getAmount() != null) {
if (isGTNHLibLoaded) { FluidCraft.proxy.netHandler.sendToServer(
long value = (long) MathExpressionParser.parse(widget.textField.getText()); new CPacketLevelMaintainer(widget.action, widget.componentIndex, widget.getAmount()));
widget.textField.setText(String.valueOf(value));
}
if (!widget.textField.getText().isEmpty()
&& numberPattern.matcher(widget.textField.getText()).matches()) {
String str = widget.textField.getText().replaceAll("^(0+)", "");
widget.textField.setText(str.isEmpty() ? "0" : str);
FluidCraft.proxy.netHandler.sendToServer(
new CPacketLevelMaintainer(widget.action, widget.idx, widget.textField.getText()));
widget.textField.setTextColor(0xFFFFFF);
return true;
} else {
widget.textField.setTextColor(0xFF0000);
return false;
}
} }
return false; }
public void submit() {
this.sendToServer(this.submit);
} }
protected boolean sendToServer(GuiButton btn) { protected boolean sendToServer(GuiButton btn) {
boolean didSomething = false; boolean didSomething = false;
if (this.submit == btn) { if (this.submit == btn) {
if (this.send(this.getQty())) this.send(this.getBatch()); final Widget qty = this.getQty();
final Widget batch = this.getBatch();
qty.validate();
batch.validate();
if (qty.getAmount() != null) {
this.send(qty);
}
if (batch.getAmount() != null) {
this.send(batch);
}
didSomething = true; didSomething = true;
} else if (this.enable == btn) { } else if (this.enable == btn) {
FluidCraft.proxy.netHandler.sendToServer(new CPacketLevelMaintainer(Action.Enable, this.getIndex())); FluidCraft.proxy.netHandler.sendToServer(new CPacketLevelMaintainer(Action.Enable, this.getIndex()));
@@ -453,9 +451,9 @@ public class GuiLevelMaintainer extends AEBaseGui implements INEIGuiHandler {
return didSomething; return didSomething;
} }
public FCGuiTextField isMouseIn(final int xCoord, final int yCoord) { public Widget isMouseIn(final int xCoord, final int yCoord) {
if (this.qty.textField.isMouseIn(xCoord, yCoord)) return this.getQty().textField; if (this.qty.textField.isMouseIn(xCoord, yCoord)) return this.getQty();
if (this.batch.textField.isMouseIn(xCoord, yCoord)) return this.getBatch().textField; if (this.batch.textField.isMouseIn(xCoord, yCoord)) return this.getBatch();
return null; return null;
} }
@@ -540,28 +538,41 @@ public class GuiLevelMaintainer extends AEBaseGui implements INEIGuiHandler {
private class Widget { private class Widget {
public final int idx; public final int componentIndex;
public final Action action; public final Action action;
private final FCGuiTextField textField; public final FCGuiTextField textField;
private final String tooltip; private final String tooltip;
private Long amount;
public Widget(FCGuiTextField textField, String tooltip, int idx, Action action) { public Widget(FCGuiTextField textField, String tooltip, int componentIndex, Action action) {
this.textField = textField; this.textField = textField;
this.textField.setEnableBackgroundDrawing(false); this.textField.setEnableBackgroundDrawing(false);
this.textField.setText("0"); this.textField.setText("0");
this.textField.setMaxStringLength(16); // this length is enough to be useful this.textField.setMaxStringLength(16); // this length is enough to be useful
this.idx = idx; this.componentIndex = componentIndex;
this.action = action; this.action = action;
this.tooltip = tooltip; this.tooltip = tooltip;
} }
public void draw() { public void draw() {
String current = amount != null
? StatCollector.translateToLocal(NameConst.TT_LEVEL_MAINTAINER_CURRENT) + " "
+ NumberFormat.getNumberInstance().format(amount)
+ "\n"
: "";
if (isShiftKeyDown()) { if (isShiftKeyDown()) {
this.setTooltip(render.wrapFormattedStringToWidth(NameConst.i18n(this.tooltip), xSize / 2)); this.setTooltip(
render.wrapFormattedStringToWidth(
StatCollector.translateToLocal(this.tooltip) + "\n"
+ current
+ "\n"
+ StatCollector.translateToLocal(this.tooltip + ".hint"),
xSize / 2));
} else { } else {
this.setTooltip( this.setTooltip(
render.wrapFormattedStringToWidth( render.wrapFormattedStringToWidth(
NameConst.i18n(this.tooltip, "\n", false) + "\n" NameConst.i18n(this.tooltip, "\n", false) + "\n"
+ current
+ NameConst.i18n(NameConst.TT_SHIFT_FOR_MORE), + NameConst.i18n(NameConst.TT_SHIFT_FOR_MORE),
(int) Math.floor(xSize * 0.8))); (int) Math.floor(xSize * 0.8)));
} }
@@ -571,5 +582,21 @@ public class GuiLevelMaintainer extends AEBaseGui implements INEIGuiHandler {
public void setTooltip(String message) { public void setTooltip(String message) {
this.textField.setMessage(message); this.textField.setMessage(message);
} }
public void validate() {
final double result = Calculator.conversion(this.textField.getText());
if (Double.isNaN(result)) {
this.amount = null;
this.textField.setTextColor(0xFF0000);
} else {
this.amount = (long) ArithHelper.round(result, 0);
this.textField.setTextColor(0xFFFFFF);
}
}
@Nullable
public Long getAmount() {
return this.amount;
}
} }
} }
@@ -28,6 +28,7 @@ import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.renderer.RenderHelper; import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.Tessellator; import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.entity.RenderItem;
import net.minecraft.client.resources.I18n; import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.entity.player.InventoryPlayer;
@@ -95,6 +96,7 @@ public class GuiLevelTerminal extends FCBaseMEGui implements IDropToFillTextFiel
public static final int VIEW_WIDTH = 174; public static final int VIEW_WIDTH = 174;
public static final int VIEW_LEFT = 8; public static final int VIEW_LEFT = 8;
private static final ResourceLocation TEX_BG = FluidCraft.resource("textures/gui/level_terminal.png"); private static final ResourceLocation TEX_BG = FluidCraft.resource("textures/gui/level_terminal.png");
private static final RenderItem renderItem = new RenderItem();
protected int offsetY; protected int offsetY;
private static final int offsetX = 21; private static final int offsetX = 21;
protected static String searchFieldOutputsText = ""; protected static String searchFieldOutputsText = "";
@@ -502,7 +504,7 @@ public class GuiLevelTerminal extends FCBaseMEGui implements IDropToFillTextFiel
Tessellator.instance.startDrawingQuads(); Tessellator.instance.startDrawingQuads();
int relY = 0; int relY = 0;
final int slotLeftMargin = (VIEW_WIDTH - entry.rowSize * 18); final int slotLeftMargin = (VIEW_WIDTH - entry.rowSize * 18);
float lastZLevel = aeRenderItem.zLevel; float lastZLevel = renderItem.zLevel;
entry.dispY = viewY; entry.dispY = viewY;
/* PASS 1: BG */ /* PASS 1: BG */
@@ -590,10 +592,9 @@ public class GuiLevelTerminal extends FCBaseMEGui implements IDropToFillTextFiel
GL11.glEnable(GL12.GL_RESCALE_NORMAL); GL11.glEnable(GL12.GL_RESCALE_NORMAL);
RenderHelper.enableGUIStandardItemLighting(); RenderHelper.enableGUIStandardItemLighting();
itemStack.stackSize = 0; itemStack.stackSize = 0;
aeRenderItem.setAeStack(null); renderItem.zLevel = ITEM_STACK_Z - MAGIC_RENDER_ITEM_Z;
aeRenderItem.zLevel = ITEM_STACK_Z - MAGIC_RENDER_ITEM_Z; renderItem.renderItemAndEffectIntoGUI(fontRendererObj, mc.getTextureManager(), itemStack, 0, 0);
aeRenderItem.renderItemAndEffectIntoGUI(fontRendererObj, mc.getTextureManager(), itemStack, 0, 0); renderItem.renderItemOverlayIntoGUI(fontRendererObj, mc.getTextureManager(), itemStack, 0, 0);
aeRenderItem.renderItemOverlayIntoGUI(fontRendererObj, mc.getTextureManager(), itemStack, 0, 0);
GL11.glTranslatef(0.0f, 0.0f, ITEM_STACK_OVERLAY_Z - ITEM_STACK_Z); GL11.glTranslatef(0.0f, 0.0f, ITEM_STACK_OVERLAY_Z - ITEM_STACK_Z);
int color = switch (state) { int color = switch (state) {
case Idle -> FCGuiColors.StateIdle.getColor(); case Idle -> FCGuiColors.StateIdle.getColor();
@@ -641,7 +642,7 @@ public class GuiLevelTerminal extends FCBaseMEGui implements IDropToFillTextFiel
} }
} }
GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f); GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
aeRenderItem.zLevel = lastZLevel; renderItem.zLevel = lastZLevel;
return relY + 1; return relY + 1;
} }
@@ -54,12 +54,6 @@ public class CPacketLevelMaintainer implements IMessage {
this.size = size; this.size = size;
} }
public CPacketLevelMaintainer(Action action, int slotIndex, String size) {
this.action = action;
this.slotIndex = slotIndex;
this.size = size.isEmpty() ? 0 : Long.parseLong(size);
}
@Override @Override
public void fromBytes(ByteBuf buf) { public void fromBytes(ByteBuf buf) {
this.action = Action.values()[buf.readInt()]; this.action = Action.values()[buf.readInt()];