From 36f81cf0b7b952fd17dc226fc90dd80ada3731f2 Mon Sep 17 00:00:00 2001 From: AlgorithmX2 Date: Sun, 29 Jun 2014 04:06:07 -0500 Subject: [PATCH] MORE Gui work for crafting --- .../gui/implementations/GuiCraftAmount.java | 8 +- .../gui/implementations/GuiCraftConfirm.java | 124 +++++++++++++++++- .../ContainerCraftConfirm.java | 118 ++++++++++++----- core/localization/GuiText.java | 2 +- core/sync/packets/PacketCraftRequest.java | 10 +- core/sync/packets/PacketValueConfig.java | 7 + crafting/CraftingTreeNode.java | 9 +- 7 files changed, 234 insertions(+), 44 deletions(-) diff --git a/client/gui/implementations/GuiCraftAmount.java b/client/gui/implementations/GuiCraftAmount.java index 9993e9b0..d8d49262 100644 --- a/client/gui/implementations/GuiCraftAmount.java +++ b/client/gui/implementations/GuiCraftAmount.java @@ -118,7 +118,7 @@ public class GuiCraftAmount extends AEBaseGui try { NetworkHandler.instance.sendToServer( new PacketCraftRequest( inventorySlots.getSlot( 0 ).getStack(), Integer.parseInt( this.amountToCraft - .getText() ) ) ); + .getText() ), isShiftKeyDown() ) ); } catch (Throwable e) { @@ -185,6 +185,10 @@ public class GuiCraftAmount extends AEBaseGui { if ( !this.checkHotbarKeys( key ) ) { + if ( key == 28 ) + { + actionPerformed( next ); + } if ( (key == 211 || key == 205 || key == 203 || key == 14 || character == '-' || Character.isDigit( character )) && amountToCraft.textboxKeyTyped( character, key ) ) { @@ -226,6 +230,8 @@ public class GuiCraftAmount extends AEBaseGui @Override public void drawBG(int offsetX, int offsetY, int mouseX, int mouseY) { + next.displayString = isShiftKeyDown() ? GuiText.Start.getLocal() : GuiText.Next.getLocal(); + bindTexture( "guis/craftAmt.png" ); this.drawTexturedModalRect( offsetX, offsetY, 0, 0, xSize, ySize ); diff --git a/client/gui/implementations/GuiCraftConfirm.java b/client/gui/implementations/GuiCraftConfirm.java index b9be8dde..9c03b2bd 100644 --- a/client/gui/implementations/GuiCraftConfirm.java +++ b/client/gui/implementations/GuiCraftConfirm.java @@ -1,5 +1,6 @@ package appeng.client.gui.implementations; +import java.io.IOException; import java.text.NumberFormat; import java.util.ArrayList; import java.util.Iterator; @@ -19,7 +20,16 @@ import appeng.api.storage.data.IItemList; import appeng.client.gui.AEBaseGui; import appeng.client.gui.widgets.GuiScrollbar; import appeng.container.implementations.ContainerCraftConfirm; +import appeng.core.AELog; 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.WirelessTerminalGuiObject; +import appeng.parts.reporting.PartCraftingTerminal; +import appeng.parts.reporting.PartPatternTerminal; +import appeng.parts.reporting.PartTerminal; import appeng.util.Platform; import com.google.common.base.Joiner; @@ -31,14 +41,40 @@ public class GuiCraftConfirm extends AEBaseGui IItemList storage = AEApi.instance().storage().createItemList(); IItemList pending = AEApi.instance().storage().createItemList(); + IItemList missing = AEApi.instance().storage().createItemList(); List visual = new ArrayList(); + GuiBridge OriginalGui; + + boolean isAutoStart() + { + return ((ContainerCraftConfirm) inventorySlots).autoStart; + } + + boolean isSimulation() + { + return ((ContainerCraftConfirm) inventorySlots).simulation; + } + public GuiCraftConfirm(InventoryPlayer inventoryPlayer, ITerminalHost te) { super( new ContainerCraftConfirm( inventoryPlayer, te ) ); xSize = 238; ySize = 206; myScrollBar = new GuiScrollbar(); + + if ( te instanceof WirelessTerminalGuiObject ) + OriginalGui = GuiBridge.GUI_WIRELESS_TERM; + + if ( te instanceof PartTerminal ) + OriginalGui = GuiBridge.GUI_ME; + + if ( te instanceof PartCraftingTerminal ) + OriginalGui = GuiBridge.GUI_CRAFTING_TERMINAL; + + if ( te instanceof PartPatternTerminal ) + OriginalGui = GuiBridge.GUI_PATTERN_TERMINAL; + } GuiButton cancel; @@ -51,13 +87,17 @@ public class GuiCraftConfirm extends AEBaseGui super.initGui(); start = new GuiButton( 0, this.guiLeft + 162, this.guiTop + ySize - 25, 50, 20, GuiText.Start.getLocal() ); + start.enabled = false; buttonList.add( start ); selectcpu = new GuiButton( 0, this.guiLeft + (219 - 150) / 2, this.guiTop + ySize - 68, 150, 20, GuiText.CraftingCPU.getLocal() + ": " + GuiText.Automatic ); + selectcpu.enabled = false; buttonList.add( selectcpu ); - cancel = new GuiButton( 0, this.guiLeft + 6, this.guiTop + ySize - 25, 50, 20, GuiText.Cancel.getLocal() ); + if ( OriginalGui != null ) + cancel = new GuiButton( 0, this.guiLeft + 6, this.guiTop + ySize - 25, 50, 20, GuiText.Cancel.getLocal() ); + buttonList.add( cancel ); } @@ -65,12 +105,38 @@ public class GuiCraftConfirm extends AEBaseGui protected void actionPerformed(GuiButton btn) { super.actionPerformed( btn ); + + if ( btn == cancel ) + { + try + { + NetworkHandler.instance.sendToServer( new PacketSwitchGuis( OriginalGui ) ); + } + catch (IOException e) + { + AELog.error( e ); + } + } + + if ( btn == start ) + { + try + { + NetworkHandler.instance.sendToServer( new PacketValueConfig( "Terminal.Start", "Start" ) ); + } + catch (Throwable e) + { + AELog.error( e ); + } + } + } private long getTotal(IAEItemStack is) { IAEItemStack a = storage.findPrecise( is ); IAEItemStack c = pending.findPrecise( is ); + IAEItemStack m = missing.findPrecise( is ); long total = 0; @@ -80,6 +146,9 @@ public class GuiCraftConfirm extends AEBaseGui if ( c != null ) total += c.getStackSize(); + if ( m != null ) + total += m.getStackSize(); + return total; } @@ -96,6 +165,11 @@ public class GuiCraftConfirm extends AEBaseGui for (IAEItemStack l : list) handleInput( pending, l ); break; + + case 2: + for (IAEItemStack l : list) + handleInput( missing, l ); + break; } for (IAEItemStack l : list) @@ -173,6 +247,19 @@ public class GuiCraftConfirm extends AEBaseGui myScrollBar.setRange( 0, (size + 2) / 3 - rows, 1 ); } + @Override + protected void keyTyped(char character, int key) + { + if ( !this.checkHotbarKeys( key ) ) + { + if ( key == 28 ) + { + actionPerformed( start ); + } + super.keyTyped( character, key ); + } + } + @Override public void drawBG(int offsetX, int offsetY, int mouseX, int mouseY) { @@ -186,6 +273,9 @@ public class GuiCraftConfirm extends AEBaseGui @Override public void drawScreen(int mouse_x, int mouse_y, float btn) { + start.enabled = isSimulation() ? false : true; + selectcpu.enabled = isSimulation() ? false : true; + int x = 0; int y = 0; @@ -232,8 +322,14 @@ public class GuiCraftConfirm extends AEBaseGui String Add = BytesUsed > 0 ? (byteUsed + " " + GuiText.BytesUsed.getLocal()) : GuiText.CalculatingWait.getLocal(); fontRendererObj.drawString( GuiText.CraftingPlan.getLocal() + " - " + Add, 8, 7, 4210752 ); - String dsp = c.cpuBytesAvail > 0 ? (GuiText.Bytes.getLocal() + ": " + c.cpuBytesAvail + " : " + GuiText.CoProcessors.getLocal() + ": " + c.cpuCoProcessors) - : GuiText.Bytes.getLocal() + ": N/A : " + GuiText.CoProcessors.getLocal() + ": N/A"; + String dsp = null; + + if ( isSimulation() ) + dsp = GuiText.Simulation.getLocal(); + else + dsp = c.cpuBytesAvail > 0 ? (GuiText.Bytes.getLocal() + ": " + c.cpuBytesAvail + " : " + GuiText.CoProcessors.getLocal() + ": " + c.cpuCoProcessors) + : GuiText.Bytes.getLocal() + ": N/A : " + GuiText.CoProcessors.getLocal() + ": N/A"; + int offset = (219 - fontRendererObj.getStringWidth( dsp )) / 2; fontRendererObj.drawString( dsp, offset, 165, 4210752 ); @@ -263,6 +359,7 @@ public class GuiCraftConfirm extends AEBaseGui IAEItemStack stored = storage.findPrecise( refStack ); IAEItemStack pendingStack = pending.findPrecise( refStack ); + IAEItemStack missingStack = missing.findPrecise( refStack ); int lines = 0; @@ -270,6 +367,8 @@ public class GuiCraftConfirm extends AEBaseGui lines++; if ( pendingStack != null && pendingStack.getStackSize() > 0 ) lines++; + if ( pendingStack != null && pendingStack.getStackSize() > 0 ) + lines++; int negY = ((lines - 1) * 5) / 2; int downY = 0; @@ -293,6 +392,25 @@ public class GuiCraftConfirm extends AEBaseGui downY += 5; } + if ( missingStack != null && missingStack.getStackSize() > 0 ) + { + String str = Long.toString( missingStack.getStackSize() ); + if ( missingStack.getStackSize() >= 10000 ) + str = Long.toString( missingStack.getStackSize() / 1000 ) + "k"; + if ( missingStack.getStackSize() >= 10000000 ) + str = Long.toString( missingStack.getStackSize() / 1000000 ) + "m"; + + str = GuiText.Missing.getLocal() + ": " + str; + int w = 4 + fontRendererObj.getStringWidth( str ); + fontRendererObj.drawString( str, (int) ((x * (1 + sectionLength) + xo + sectionLength - 19 - ((float) w * 0.5)) * 2), (int) ((y * offY + yo + + 6 - negY + downY) * 2), 4210752 ); + + if ( tooltip == z - viewStart ) + lineList.add( GuiText.Missing.getLocal() + ": " + Long.toString( missingStack.getStackSize() ) ); + + downY += 5; + } + if ( pendingStack != null && pendingStack.getStackSize() > 0 ) { String str = Long.toString( pendingStack.getStackSize() ); diff --git a/container/implementations/ContainerCraftConfirm.java b/container/implementations/ContainerCraftConfirm.java index 5472d00b..1d8c923a 100644 --- a/container/implementations/ContainerCraftConfirm.java +++ b/container/implementations/ContainerCraftConfirm.java @@ -10,11 +10,14 @@ import net.minecraft.inventory.ICrafting; import net.minecraft.util.ChatComponentText; import net.minecraft.world.World; import appeng.api.AEApi; +import appeng.api.config.Actionable; import appeng.api.config.SecurityPermissions; import appeng.api.networking.IGrid; import appeng.api.networking.security.BaseActionSource; import appeng.api.networking.security.IActionHost; import appeng.api.networking.security.PlayerSource; +import appeng.api.networking.storage.IStorageGrid; +import appeng.api.storage.IMEInventory; import appeng.api.storage.ITerminalHost; import appeng.api.storage.data.IAEItemStack; import appeng.api.storage.data.IItemList; @@ -25,6 +28,7 @@ import appeng.core.sync.network.NetworkHandler; import appeng.core.sync.packets.PacketMEInventoryUpdate; import appeng.crafting.CraftingJob; import appeng.crafting.ICraftingHost; +import appeng.me.cache.CraftingCache; public class ContainerCraftConfirm extends AEBaseContainer implements ICraftingHost { @@ -42,6 +46,12 @@ public class ContainerCraftConfirm extends AEBaseContainer implements ICraftingH @GuiSync(2) public int cpuCoProcessors; + @GuiSync(3) + public boolean autoStart = false; + + @GuiSync(4) + public boolean simulation = true; + public ContainerCraftConfirm(InventoryPlayer ip, ITerminalHost te) { super( ip, te ); priHost = te; @@ -56,53 +66,83 @@ public class ContainerCraftConfirm extends AEBaseContainer implements ICraftingH try { result = job.get(); + if ( !result.isSimulation() ) { - try + simulation = false; + if ( autoStart ) { - PacketMEInventoryUpdate a = new PacketMEInventoryUpdate( (byte) 0 ); - PacketMEInventoryUpdate b = new PacketMEInventoryUpdate( (byte) 1 ); + startJob(); + return; + } + } + else + simulation = true; - IItemList plan = AEApi.instance().storage().createItemList(); - result.tree.getPlan( plan ); + try + { + PacketMEInventoryUpdate a = new PacketMEInventoryUpdate( (byte) 0 ); + PacketMEInventoryUpdate b = new PacketMEInventoryUpdate( (byte) 1 ); + PacketMEInventoryUpdate c = result.isSimulation() ? new PacketMEInventoryUpdate( (byte) 2 ) : null; - bytesUsed = result.getByteTotal(); + IItemList plan = AEApi.instance().storage().createItemList(); + result.tree.getPlan( plan ); - for (IAEItemStack out : plan) + bytesUsed = result.getByteTotal(); + + for (IAEItemStack out : plan) + { + IAEItemStack m = null; + + IAEItemStack o = out.copy(); + o.reset(); + o.setStackSize( out.getStackSize() ); + + IAEItemStack p = out.copy(); + p.reset(); + p.setStackSize( out.getCountRequestable() ); + + IStorageGrid sg = getGrid().getCache( IStorageGrid.class ); + IMEInventory itemsg = sg.getItemInventory(); + + if ( c != null && result.isSimulation() ) { - IAEItemStack o = out.copy(); - o.reset(); - o.setStackSize( out.getStackSize() ); + m = o.copy(); + o = itemsg.extractItems( o, Actionable.SIMULATE, mySrc ); - IAEItemStack p = out.copy(); - p.reset(); - p.setStackSize( out.getCountRequestable() ); - - if ( o.getStackSize() > 0 ) - a.appendItem( o ); - - if ( p.getStackSize() > 0 ) - b.appendItem( p ); - } - - for (Object g : this.crafters) - { - if ( g instanceof EntityPlayer ) + if ( o == null ) { - NetworkHandler.instance.sendTo( a, (EntityPlayerMP) g ); - NetworkHandler.instance.sendTo( b, (EntityPlayerMP) g ); + o = m.copy(); + o.setStackSize( 0 ); } + + m.setStackSize( m.getStackSize() - o.getStackSize() ); } - } - catch (IOException e) - { - // :P + + if ( o.getStackSize() > 0 ) + a.appendItem( o ); + + if ( p.getStackSize() > 0 ) + b.appendItem( p ); + + if ( c != null && m != null && m.getStackSize() > 0 ) + c.appendItem( m ); } - // CraftingCache cc = getGrid().getCache( CraftingCache.class ); - // cc.submitJob( result, null, getActionSrc() ); - // AELog.info( "Job info is ready!" ); - // this.isContainerValid = false; + for (Object g : this.crafters) + { + if ( g instanceof EntityPlayer ) + { + NetworkHandler.instance.sendTo( a, (EntityPlayerMP) g ); + NetworkHandler.instance.sendTo( b, (EntityPlayerMP) g ); + if ( c != null ) + NetworkHandler.instance.sendTo( c, (EntityPlayerMP) g ); + } + } + } + catch (IOException e) + { + // :P } } catch (Throwable e) @@ -118,6 +158,16 @@ public class ContainerCraftConfirm extends AEBaseContainer implements ICraftingH verifyPermissions( SecurityPermissions.CRAFT, false ); } + public void startJob() + { + if ( result != null && simulation == false ) + { + CraftingCache cc = getGrid().getCache( CraftingCache.class ); + cc.submitJob( result, null, getActionSrc() ); + this.isContainerValid = false; + } + } + @Override public void onContainerClosed(EntityPlayer par1EntityPlayer) { diff --git a/core/localization/GuiText.java b/core/localization/GuiText.java index 31874f5c..133d0ac8 100644 --- a/core/localization/GuiText.java +++ b/core/localization/GuiText.java @@ -32,7 +32,7 @@ public enum GuiText OfSecondOutput, NoSecondOutput, RFTunnel, Stores, Next, SelectAmount, Lumen, Empty, ConfirmCrafting, - Stored, Crafting, Scheduled, CraftingStatus, Cancel, FromStorage, ToCraft, CraftingPlan, CalculatingWait, Start, Bytes, CraftingCPU, Automatic, CoProcessors; + Stored, Crafting, Scheduled, CraftingStatus, Cancel, FromStorage, ToCraft, CraftingPlan, CalculatingWait, Start, Bytes, CraftingCPU, Automatic, CoProcessors, Simulation, Missing; String root; diff --git a/core/sync/packets/PacketCraftRequest.java b/core/sync/packets/PacketCraftRequest.java index 05cde9f2..fc098c34 100644 --- a/core/sync/packets/PacketCraftRequest.java +++ b/core/sync/packets/PacketCraftRequest.java @@ -26,7 +26,6 @@ import appeng.core.sync.AppEngPacket; import appeng.core.sync.GuiBridge; import appeng.core.sync.network.INetworkInfo; import appeng.crafting.CraftingJob; -import appeng.me.cache.CraftingCache; import appeng.util.Platform; import appeng.util.item.AEItemStack; @@ -34,6 +33,7 @@ public class PacketCraftRequest extends AppEngPacket { final public IAEItemStack slotItem; + final public boolean heldShift; final public static ExecutorService craftingPool; static @@ -53,6 +53,7 @@ public class PacketCraftRequest extends AppEngPacket // automatic. public PacketCraftRequest(ByteBuf stream) throws IOException { + heldShift = stream.readBoolean(); slotItem = AEItemStack.loadItemStackFromPacket( stream ); } @@ -74,8 +75,6 @@ public class PacketCraftRequest extends AppEngPacket if ( g == null ) return; - CraftingCache cc = g.getCache( CraftingCache.class ); - try { CraftingJob cj = new CraftingJob( cca.getWorld(), cca, slotItem, Actionable.SIMULATE ); @@ -89,6 +88,7 @@ public class PacketCraftRequest extends AppEngPacket if ( player.openContainer instanceof ContainerCraftConfirm ) { ContainerCraftConfirm ccc = (ContainerCraftConfirm) player.openContainer; + ccc.autoStart = heldShift; ccc.job = craftingPool.submit( cj, cj ); cca.detectAndSendChanges(); } @@ -103,13 +103,15 @@ public class PacketCraftRequest extends AppEngPacket } } - public PacketCraftRequest(ItemStack stack, int parseInt) throws IOException { + public PacketCraftRequest(ItemStack stack, int parseInt, boolean shift) throws IOException { this.slotItem = AEApi.instance().storage().createItemStack( stack ); this.slotItem.setStackSize( parseInt ); + this.heldShift = shift; ByteBuf data = Unpooled.buffer(); data.writeInt( getPacketID() ); + data.writeBoolean( shift ); slotItem.writeToPacket( data ); configureWrite( data ); diff --git a/core/sync/packets/PacketValueConfig.java b/core/sync/packets/PacketValueConfig.java index b58cfe69..c5171eee 100644 --- a/core/sync/packets/PacketValueConfig.java +++ b/core/sync/packets/PacketValueConfig.java @@ -17,6 +17,7 @@ import appeng.api.util.IConfigManager; import appeng.api.util.IConfigureableObject; import appeng.container.AEBaseContainer; import appeng.container.implementations.ContainerCellWorkbench; +import appeng.container.implementations.ContainerCraftConfirm; import appeng.container.implementations.ContainerCraftingCPU; import appeng.container.implementations.ContainerLevelEmitter; import appeng.container.implementations.ContainerPatternTerm; @@ -54,6 +55,12 @@ public class PacketValueConfig extends AppEngPacket si.onWheel( is, Value.equals( "WheelUp" ) ); return; } + else if ( Name.equals( "Terminal.Start" ) && c instanceof ContainerCraftConfirm ) + { + ContainerCraftConfirm qk = (ContainerCraftConfirm) c; + qk.startJob(); + return; + } else if ( Name.equals( "TileCrafting.Cancel" ) && c instanceof ContainerCraftingCPU ) { ContainerCraftingCPU qk = (ContainerCraftingCPU) c; diff --git a/crafting/CraftingTreeNode.java b/crafting/CraftingTreeNode.java index 8d200e74..242b3f92 100644 --- a/crafting/CraftingTreeNode.java +++ b/crafting/CraftingTreeNode.java @@ -193,7 +193,7 @@ public class CraftingTreeNode { if ( missing > 0 ) job.addMissing( getStack( missing ) ); - missing = 0; + // missing = 0; job.addBytes( 8 + bytes ); @@ -231,6 +231,13 @@ public class CraftingTreeNode public void getPlan(IItemList plan) { + if ( missing > 0 ) + { + IAEItemStack o = what.copy(); + o.setStackSize( missing ); + plan.add( o ); + } + for (IAEItemStack i : used) plan.add( i.copy() );