|
| 1 | +package com.glodblock.github.client.gui; |
| 2 | + |
| 3 | +import appeng.api.AEApi; |
| 4 | +import appeng.api.definitions.IDefinitions; |
| 5 | +import appeng.api.definitions.IParts; |
| 6 | +import appeng.api.storage.ITerminalHost; |
| 7 | +import appeng.client.gui.AEBaseGui; |
| 8 | +import appeng.client.gui.widgets.GuiTabButton; |
| 9 | +import appeng.container.AEBaseContainer; |
| 10 | +import appeng.core.AEConfig; |
| 11 | +import appeng.core.localization.GuiText; |
| 12 | +import appeng.helpers.Reflected; |
| 13 | +import appeng.util.calculators.ArithHelper; |
| 14 | +import appeng.util.calculators.Calculator; |
| 15 | +import com.glodblock.github.FluidCraft; |
| 16 | +import com.glodblock.github.client.gui.container.ContainerPatternValueAmount; |
| 17 | +import com.glodblock.github.common.item.ItemFluidPacket; |
| 18 | +import com.glodblock.github.common.parts.PartFluidPatternTerminal; |
| 19 | +import com.glodblock.github.common.parts.PartFluidPatternTerminalEx; |
| 20 | +import com.glodblock.github.inventory.gui.GuiType; |
| 21 | +import com.glodblock.github.loader.ItemAndBlockHolder; |
| 22 | +import com.glodblock.github.network.CPacketPatternValueSet; |
| 23 | +import com.glodblock.github.network.CPacketSwitchGuis; |
| 24 | +import com.glodblock.github.util.Util; |
| 25 | +import net.minecraft.client.Minecraft; |
| 26 | +import net.minecraft.client.gui.GuiButton; |
| 27 | +import net.minecraft.client.gui.GuiTextField; |
| 28 | +import net.minecraft.client.gui.inventory.GuiContainer; |
| 29 | +import net.minecraft.client.resources.I18n; |
| 30 | +import net.minecraft.entity.player.InventoryPlayer; |
| 31 | +import net.minecraft.inventory.Slot; |
| 32 | +import net.minecraft.item.ItemStack; |
| 33 | +import net.minecraftforge.fluids.FluidStack; |
| 34 | + |
| 35 | +public class GuiPatternValueAmount extends AEBaseGui { |
| 36 | + |
| 37 | + private GuiTextField amountToSet; |
| 38 | + private GuiTabButton originalGuiBtn; |
| 39 | + |
| 40 | + private GuiButton set; |
| 41 | + |
| 42 | + private GuiButton plus1; |
| 43 | + private GuiButton plus10; |
| 44 | + private GuiButton plus100; |
| 45 | + private GuiButton plus1000; |
| 46 | + private GuiButton minus1; |
| 47 | + private GuiButton minus10; |
| 48 | + private GuiButton minus100; |
| 49 | + private GuiButton minus1000; |
| 50 | + |
| 51 | + private GuiType originalGui; |
| 52 | + private final int valueIndex; |
| 53 | + private final int originalAmount; |
| 54 | + |
| 55 | + @Reflected |
| 56 | + public GuiPatternValueAmount(final InventoryPlayer inventoryPlayer, final ITerminalHost te) { |
| 57 | + super(new ContainerPatternValueAmount(inventoryPlayer, te)); |
| 58 | + GuiContainer gui = (GuiContainer) Minecraft.getMinecraft().currentScreen; |
| 59 | + if (gui != null && gui.theSlot != null && gui.theSlot.getHasStack()) { |
| 60 | + Slot slot = gui.theSlot; |
| 61 | + originalAmount = this.getOriginalAmount(slot.getStack()); |
| 62 | + valueIndex = slot.slotNumber; |
| 63 | + } else { |
| 64 | + valueIndex = -1; |
| 65 | + originalAmount = 0; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void initGui() { |
| 71 | + super.initGui(); |
| 72 | + |
| 73 | + final int a = AEConfig.instance.craftItemsByStackAmounts(0); |
| 74 | + final int b = AEConfig.instance.craftItemsByStackAmounts(1); |
| 75 | + final int c = AEConfig.instance.craftItemsByStackAmounts(2); |
| 76 | + final int d = AEConfig.instance.craftItemsByStackAmounts(3); |
| 77 | + |
| 78 | + this.buttonList.add(this.plus1 = new GuiButton(0, this.guiLeft + 20, this.guiTop + 26, 22, 20, "+" + a)); |
| 79 | + this.buttonList.add(this.plus10 = new GuiButton(0, this.guiLeft + 48, this.guiTop + 26, 28, 20, "+" + b)); |
| 80 | + this.buttonList.add(this.plus100 = new GuiButton(0, this.guiLeft + 82, this.guiTop + 26, 32, 20, "+" + c)); |
| 81 | + this.buttonList.add(this.plus1000 = new GuiButton(0, this.guiLeft + 120, this.guiTop + 26, 38, 20, "+" + d)); |
| 82 | + |
| 83 | + this.buttonList.add(this.minus1 = new GuiButton(0, this.guiLeft + 20, this.guiTop + 75, 22, 20, "-" + a)); |
| 84 | + this.buttonList.add(this.minus10 = new GuiButton(0, this.guiLeft + 48, this.guiTop + 75, 28, 20, "-" + b)); |
| 85 | + this.buttonList.add(this.minus100 = new GuiButton(0, this.guiLeft + 82, this.guiTop + 75, 32, 20, "-" + c)); |
| 86 | + this.buttonList.add(this.minus1000 = new GuiButton(0, this.guiLeft + 120, this.guiTop + 75, 38, 20, "-" + d)); |
| 87 | + |
| 88 | + this.buttonList.add(this.set = new GuiButton(0, this.guiLeft + 128, this.guiTop + 51, 38, 20, GuiText.Next.getLocal())); |
| 89 | + |
| 90 | + |
| 91 | + ItemStack myIcon = null; |
| 92 | + final Object target = ((AEBaseContainer) this.inventorySlots).getTarget(); |
| 93 | + final IDefinitions definitions = AEApi.instance().definitions(); |
| 94 | + final IParts parts = definitions.parts(); |
| 95 | + |
| 96 | + if (target instanceof PartFluidPatternTerminal) { |
| 97 | + myIcon = new ItemStack(ItemAndBlockHolder.FLUID_TERMINAL, 1); |
| 98 | + this.originalGui = GuiType.FLUID_PATTERN_TERMINAL; |
| 99 | + } |
| 100 | + |
| 101 | + if (target instanceof PartFluidPatternTerminalEx) { |
| 102 | + myIcon = new ItemStack(ItemAndBlockHolder.FLUID_TERMINAL_EX, 1); |
| 103 | + this.originalGui = GuiType.FLUID_PATTERN_TERMINAL_EX; |
| 104 | + } |
| 105 | + |
| 106 | + if (this.originalGui != null && myIcon != null) { |
| 107 | + this.buttonList.add(this.originalGuiBtn = new GuiTabButton(this.guiLeft + 154, this.guiTop, myIcon, myIcon.getDisplayName(), itemRender)); |
| 108 | + } |
| 109 | + |
| 110 | + this.amountToSet = new GuiTextField(this.fontRendererObj, this.guiLeft + 62, this.guiTop + 57, 59, this.fontRendererObj.FONT_HEIGHT); |
| 111 | + this.amountToSet.setEnableBackgroundDrawing(false); |
| 112 | + this.amountToSet.setMaxStringLength(16); |
| 113 | + this.amountToSet.setTextColor(0xFFFFFF); |
| 114 | + this.amountToSet.setVisible(true); |
| 115 | + this.amountToSet.setFocused(true); |
| 116 | + this.amountToSet.setText(String.valueOf(originalAmount)); |
| 117 | + this.amountToSet.setSelectionPos(0); |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + @Override |
| 123 | + public void drawFG(final int offsetX, final int offsetY, final int mouseX, final int mouseY) { |
| 124 | + this.fontRendererObj.drawString(GuiText.SelectAmount.getLocal(), 8, 6, 0x404040); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public void drawBG(final int offsetX, final int offsetY, final int mouseX, final int mouseY) { |
| 129 | + this.set.displayString = I18n.format("ae2fc.gui.button.set"); |
| 130 | + this.set.enabled = valueIndex >= 0; |
| 131 | + |
| 132 | + this.bindTexture("guis/craftAmt.png"); |
| 133 | + this.drawTexturedModalRect(offsetX, offsetY, 0, 0, this.xSize, this.ySize); |
| 134 | + |
| 135 | + try { |
| 136 | + String out = this.amountToSet.getText(); |
| 137 | + |
| 138 | + double resultD = Calculator.conversion(out); |
| 139 | + int resultI; |
| 140 | + |
| 141 | + if (resultD <= 0 || Double.isNaN(resultD)) { |
| 142 | + resultI = 0; |
| 143 | + } else { |
| 144 | + resultI = (int) ArithHelper.round(resultD, 0); |
| 145 | + } |
| 146 | + |
| 147 | + this.set.enabled = resultI > 0; |
| 148 | + } catch (final NumberFormatException e) { |
| 149 | + this.set.enabled = false; |
| 150 | + } |
| 151 | + |
| 152 | + this.amountToSet.drawTextBox(); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + protected void keyTyped(final char character, final int key) { |
| 157 | + if (!this.checkHotbarKeys(key)) { |
| 158 | + if (key == 28) { |
| 159 | + this.actionPerformed(this.set); |
| 160 | + } |
| 161 | + this.amountToSet.textboxKeyTyped(character, key); |
| 162 | + super.keyTyped(character, key); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + protected void actionPerformed(final GuiButton btn) { |
| 168 | + super.actionPerformed(btn); |
| 169 | + |
| 170 | + try { |
| 171 | + |
| 172 | + if (btn == this.originalGuiBtn) { |
| 173 | + FluidCraft.proxy.netHandler.sendToServer(new CPacketSwitchGuis(this.originalGui)); |
| 174 | + } |
| 175 | + |
| 176 | + if (btn == this.set && btn.enabled) { |
| 177 | + double resultD = Calculator.conversion(this.amountToSet.getText()); |
| 178 | + int resultI; |
| 179 | + |
| 180 | + if (resultD <= 0 || Double.isNaN(resultD)) { |
| 181 | + resultI = 1; |
| 182 | + } else { |
| 183 | + resultI = (int) ArithHelper.round(resultD, 0); |
| 184 | + } |
| 185 | + |
| 186 | + FluidCraft.proxy.netHandler.sendToServer(new CPacketPatternValueSet(originalGui.ordinal(), resultI, valueIndex)); |
| 187 | + } |
| 188 | + } catch (final NumberFormatException e) { |
| 189 | + // nope.. |
| 190 | + this.amountToSet.setText("1"); |
| 191 | + } |
| 192 | + |
| 193 | + final boolean isPlus = btn == this.plus1 || btn == this.plus10 || btn == this.plus100 || btn == this.plus1000; |
| 194 | + final boolean isMinus = btn == this.minus1 || btn == this.minus10 || btn == this.minus100 || btn == this.minus1000; |
| 195 | + |
| 196 | + if (isPlus || isMinus) { |
| 197 | + this.addQty(this.getQty(btn)); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + private void addQty(final int i) { |
| 202 | + try { |
| 203 | + String out = this.amountToSet.getText(); |
| 204 | + |
| 205 | + double resultD = Calculator.conversion(out); |
| 206 | + int resultI; |
| 207 | + |
| 208 | + if (resultD <= 0 || Double.isNaN(resultD)) { |
| 209 | + resultI = 0; |
| 210 | + } else { |
| 211 | + resultI = (int) ArithHelper.round(resultD, 0); |
| 212 | + } |
| 213 | + |
| 214 | + if (resultI == 1 && i > 1) { |
| 215 | + resultI = 0; |
| 216 | + } |
| 217 | + |
| 218 | + resultI += i; |
| 219 | + if (resultI < 1) { |
| 220 | + resultI = 1; |
| 221 | + } |
| 222 | + |
| 223 | + out = Integer.toString(resultI); |
| 224 | + |
| 225 | + this.amountToSet.setText(out); |
| 226 | + } catch (final NumberFormatException e) { |
| 227 | + // :P |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + private int getOriginalAmount(ItemStack stack) { |
| 232 | + if (Util.isFluidPacket(stack)) { |
| 233 | + FluidStack fluidStack = ItemFluidPacket.getFluidStack(stack); |
| 234 | + return fluidStack.amount; |
| 235 | + } else { |
| 236 | + return stack.stackSize; |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | +} |
0 commit comments