From 67d46d57d26ffcff077c8f02c01998873b48bff9 Mon Sep 17 00:00:00 2001 From: AlgorithmX2 Date: Thu, 17 Apr 2014 01:15:56 -0500 Subject: [PATCH] Patterns can now be encoded. Added a clear button to the Pattern Terminal. Added description to the Encoded Pattern Item. --- .../gui/implementations/GuiPatternTerm.java | 17 +- container/AEBaseContainer.java | 2 +- .../implementations/ContainerPatternTerm.java | 152 +++++++++++++++++- core/Registration.java | 2 +- core/localization/GuiText.java | 2 +- core/sync/packets/PacketValueConfig.java | 4 + items/misc/ItemEncodedPattern.java | 103 ++++++++++++ items/storage/ItemEncodedPattern.java | 26 --- 8 files changed, 270 insertions(+), 38 deletions(-) create mode 100644 items/misc/ItemEncodedPattern.java delete mode 100644 items/storage/ItemEncodedPattern.java diff --git a/client/gui/implementations/GuiPatternTerm.java b/client/gui/implementations/GuiPatternTerm.java index 6041d0a4..46f36fa4 100644 --- a/client/gui/implementations/GuiPatternTerm.java +++ b/client/gui/implementations/GuiPatternTerm.java @@ -25,6 +25,7 @@ public class GuiPatternTerm extends GuiMEMonitorable GuiTabButton tabProcessButton; GuiImgButton substitutionsBtn; GuiImgButton encodeBtn; + GuiImgButton clearBtn; @Override public void initGui() @@ -34,9 +35,14 @@ public class GuiPatternTerm extends GuiMEMonitorable GuiText.CraftingPattern.getLocal(), itemRender ) ); buttonList.add( tabProcessButton = new GuiTabButton( this.guiLeft + 173, this.guiTop + this.ySize - 179, new ItemStack( Blocks.furnace ), GuiText.ProcessingPattern.getLocal(), itemRender ) ); - buttonList.add( substitutionsBtn = new GuiImgButton( this.guiLeft + 74, this.guiTop + this.ySize - 163, Settings.ACTIONS, ActionItems.SUBSTITUTION ) ); - buttonList.add( encodeBtn = new GuiImgButton( this.guiLeft + 147, this.guiTop + this.ySize - 144, Settings.ACTIONS, ActionItems.ENCODE ) ); + + buttonList.add( substitutionsBtn = new GuiImgButton( this.guiLeft + 84, this.guiTop + this.ySize - 163, Settings.ACTIONS, ActionItems.SUBSTITUTION ) ); substitutionsBtn.halfSize = true; + + buttonList.add( clearBtn = new GuiImgButton( this.guiLeft + 74, this.guiTop + this.ySize - 163, Settings.ACTIONS, ActionItems.CLOSE ) ); + clearBtn.halfSize = true; + + buttonList.add( encodeBtn = new GuiImgButton( this.guiLeft + 147, this.guiTop + this.ySize - 144, Settings.ACTIONS, ActionItems.ENCODE ) ); } @Override @@ -46,6 +52,7 @@ public class GuiPatternTerm extends GuiMEMonitorable try { + if ( tabCraftButton == btn || tabProcessButton == btn ) { NetworkHandler.instance.sendToServer( new PacketValueConfig( "PatternTerminal.CraftMode", tabProcessButton == btn ? "0" : "1" ) ); @@ -55,6 +62,12 @@ public class GuiPatternTerm extends GuiMEMonitorable { NetworkHandler.instance.sendToServer( new PacketValueConfig( "PatternTerminal.Encode", "1" ) ); } + + if ( clearBtn == btn ) + { + NetworkHandler.instance.sendToServer( new PacketValueConfig( "PatternTerminal.Clear", "1" ) ); + } + } catch (IOException e) { diff --git a/container/AEBaseContainer.java b/container/AEBaseContainer.java index fb83b4aa..ac00d518 100644 --- a/container/AEBaseContainer.java +++ b/container/AEBaseContainer.java @@ -744,7 +744,7 @@ public abstract class AEBaseContainer extends Container updateProgressBar( id, (int) value ); } - private void updateHeld(EntityPlayerMP p) + protected void updateHeld(EntityPlayerMP p) { try { diff --git a/container/implementations/ContainerPatternTerm.java b/container/implementations/ContainerPatternTerm.java index 675feac2..d28fe4d6 100644 --- a/container/implementations/ContainerPatternTerm.java +++ b/container/implementations/ContainerPatternTerm.java @@ -1,15 +1,24 @@ package appeng.container.implementations; +import java.util.ArrayList; +import java.util.List; + import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.ICrafting; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.InventoryCrafting; +import net.minecraft.inventory.Slot; import net.minecraft.inventory.SlotCrafting; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.CraftingManager; import net.minecraft.item.crafting.IRecipe; +import net.minecraft.nbt.NBTBase; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; import net.minecraftforge.common.util.ForgeDirection; +import appeng.api.AEApi; import appeng.api.config.Actionable; import appeng.api.networking.security.MachineSource; import appeng.api.storage.IMEMonitor; @@ -103,17 +112,17 @@ public class ContainerPatternTerm extends ContainerMEMonitorable implements IAEA public void putStackInSlot(int par1, ItemStack par2ItemStack) { super.putStackInSlot( par1, par2ItemStack ); - updateOutput(); + getAndUpdateOutput(); } @Override public void putStacksInSlots(ItemStack[] par1ArrayOfItemStack) { super.putStacksInSlots( par1ArrayOfItemStack ); - updateOutput(); + getAndUpdateOutput(); } - public void updateOutput() + public ItemStack getAndUpdateOutput() { InventoryCrafting ic = new InventoryCrafting( this, 3, 3 ); for (int x = 0; x < ic.getSizeInventory(); x++) @@ -121,6 +130,7 @@ public class ContainerPatternTerm extends ContainerMEMonitorable implements IAEA ItemStack is = CraftingManager.getInstance().findMatchingRecipe( ic, this.getPlayerInv().player.worldObj ); cOut.setInventorySlotContents( 0, is ); + return is; } @Override @@ -150,7 +160,7 @@ public class ContainerPatternTerm extends ContainerMEMonitorable implements IAEA if ( idx == 97 ) { craftingMode = value == 1; - updateOutput(); + getAndUpdateOutput(); updateOrderOfOutputSlots(); } } @@ -163,16 +173,127 @@ public class ContainerPatternTerm extends ContainerMEMonitorable implements IAEA public void encode() { + ItemStack output = patternSlotOUT.getStack(); + ItemStack[] in = getInputs(); + ItemStack[] out = getOutputs(); + + // if theres no input, this would be silly. + if ( in == null || out == null ) + return; + + // first check the output slots, should either be null, or a pattern + if ( output != null && !isPattern( output ) ) + return; + + // if nothing is there we should snag a new pattern. + else if ( output == null ) + { + output = patternSlotIN.getStack(); + if ( output == null || !isPattern( output ) ) + return; // no blanks. + + // remove one, and clear the input slot. + output.stackSize--; + if ( output.stackSize == 0 ) + patternSlotIN.putStack( null ); + + // add a new encoded pattern. + patternSlotOUT.putStack( output = AEApi.instance().items().itemEncodedPattern.stack( 1 ) ); + } + + // encode the slot. + NBTTagCompound encodedValue = new NBTTagCompound(); + + NBTTagList tagIn = new NBTTagList(); + NBTTagList tagOut = new NBTTagList(); + + for (ItemStack i : in) + tagIn.appendTag( createItemTag( i ) ); + + for (ItemStack i : out) + tagOut.appendTag( createItemTag( i ) ); + + encodedValue.setTag( "in", tagIn ); + encodedValue.setTag( "out", tagOut ); + encodedValue.setBoolean( "crafting", craftingMode ); + + output.setTagCompound( encodedValue ); + } + + private NBTBase createItemTag(ItemStack i) + { + NBTTagCompound c = new NBTTagCompound(); + + if ( i != null ) + i.writeToNBT( c ); + + return c; + } + + private ItemStack[] getInputs() + { + ItemStack[] input = new ItemStack[9]; + boolean hasValue = false; + + for (int x = 0; x < craftingSlots.length; x++) + { + input[x] = craftingSlots[x].getStack(); + if ( input[x] != null ) + hasValue = true; + } + + if ( hasValue ) + return input; + + return null; + } + + private ItemStack[] getOutputs() + { + if ( craftingMode ) + { + ItemStack out = getAndUpdateOutput(); + if ( out != null ) + return new ItemStack[] { out }; + } + else + { + List list = new ArrayList( 3 ); + boolean hasValue = false; + + for (int x = 0; x < outputSlots.length; x++) + { + ItemStack out = outputSlots[x].getStack(); + if ( out != null ) + { + list.add( out ); + hasValue = true; + } + } + + if ( hasValue ) + return list.toArray( new ItemStack[list.size()] ); + } + + return null; + } + + private boolean isPattern(ItemStack output) + { + if ( output == null ) + return false; + + return AEApi.instance().items().itemEncodedPattern.sameAs( output ) || AEApi.instance().materials().materialBlankPattern.sameAs( output ); } @Override public boolean isSlotEnabled(int idx) { if ( idx == 1 ) - return craftingMode == false; + return Platform.isServer() ? ct.craftingMode == false : craftingMode == false; if ( idx == 2 ) - return craftingMode == true; + return Platform.isServer() ? ct.craftingMode == true : craftingMode == true; return false; } @@ -197,9 +318,13 @@ public class ContainerPatternTerm extends ContainerMEMonitorable implements IAEA return; IAEItemStack extracted = Platform.poweredExtraction( powerSrc, cellInv, out, mySrc ); + EntityPlayer p = getPlayerInv().player; + if ( extracted != null ) { inv.addItems( extracted.getItemStack() ); + if ( p instanceof EntityPlayerMP ) + updateHeld( (EntityPlayerMP) p ); detectAndSendChanges(); return; } @@ -211,7 +336,6 @@ public class ContainerPatternTerm extends ContainerMEMonitorable implements IAEA ic.setInventorySlotContents( x, packetPatternSlot.pattern[x] == null ? null : packetPatternSlot.pattern[x].getItemStack() ); } - EntityPlayer p = getPlayerInv().player; IRecipe r = Platform.findMatchingRecipe( ic, p.worldObj ); if ( r == null ) @@ -249,6 +373,8 @@ public class ContainerPatternTerm extends ContainerMEMonitorable implements IAEA } inv.addItems( is ); + if ( p instanceof EntityPlayerMP ) + updateHeld( (EntityPlayerMP) p ); detectAndSendChanges(); } else @@ -265,4 +391,16 @@ public class ContainerPatternTerm extends ContainerMEMonitorable implements IAEA } } + + public void clear() + { + for (Slot s : craftingSlots) + s.putStack( null ); + + for (Slot s : outputSlots) + s.putStack( null ); + + detectAndSendChanges(); + getAndUpdateOutput(); + } } diff --git a/core/Registration.java b/core/Registration.java index 0901e4f6..66e6fa3a 100644 --- a/core/Registration.java +++ b/core/Registration.java @@ -95,12 +95,12 @@ import appeng.hooks.TickHandler; import appeng.items.materials.ItemMaterial; import appeng.items.materials.MaterialType; import appeng.items.misc.ItemCrystalSeed; +import appeng.items.misc.ItemEncodedPattern; import appeng.items.parts.ItemFacade; import appeng.items.parts.ItemPart; import appeng.items.parts.PartType; import appeng.items.storage.ItemBasicStorageCell; import appeng.items.storage.ItemCreativeStorageCell; -import appeng.items.storage.ItemEncodedPattern; import appeng.items.storage.ItemSpatialStorageCell; import appeng.items.storage.ItemViewCell; import appeng.items.tools.ToolBiometricCard; diff --git a/core/localization/GuiText.java b/core/localization/GuiText.java index dde2f96f..58407944 100644 --- a/core/localization/GuiText.java +++ b/core/localization/GuiText.java @@ -28,7 +28,7 @@ public enum GuiText StoredPower, MaxPower, RequiredPower, Efficiency, CopyMode, CopyModeDesc, PatternTerminal, - CraftingPattern, ProcessingPattern; + CraftingPattern, ProcessingPattern, Crafts, Creates, And, With; String root; diff --git a/core/sync/packets/PacketValueConfig.java b/core/sync/packets/PacketValueConfig.java index 1435ed77..9cf433fa 100644 --- a/core/sync/packets/PacketValueConfig.java +++ b/core/sync/packets/PacketValueConfig.java @@ -78,6 +78,10 @@ public class PacketValueConfig extends AppEngPacket { cpt.encode(); } + else if ( Name.equals( "PatternTerminal.Clear" ) ) + { + cpt.clear(); + } } else if ( Name.startsWith( "CellWorkbench." ) && c instanceof ContainerCellWorkbench ) { diff --git a/items/misc/ItemEncodedPattern.java b/items/misc/ItemEncodedPattern.java new file mode 100644 index 00000000..9233aec1 --- /dev/null +++ b/items/misc/ItemEncodedPattern.java @@ -0,0 +1,103 @@ +package appeng.items.misc; + +import java.util.EnumSet; +import java.util.List; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import appeng.api.crafting.ICraftingPatternDetails; +import appeng.api.implementations.ICraftingPatternItem; +import appeng.core.features.AEFeature; +import appeng.core.localization.GuiText; +import appeng.items.AEBaseItem; +import appeng.util.Platform; + +public class ItemEncodedPattern extends AEBaseItem implements ICraftingPatternItem +{ + + public ItemEncodedPattern() { + super( ItemEncodedPattern.class ); + setfeature( EnumSet.of( AEFeature.Crafting ) ); + setMaxStackSize( 1 ); + } + + @Override + public void addInformation(ItemStack is, EntityPlayer p, List l, boolean more) + { + NBTTagCompound encodedValue = is.getTagCompound(); + + if ( encodedValue == null ) + return; + + NBTTagList inTag = encodedValue.getTagList( "in", 10 ); + NBTTagList outTag = encodedValue.getTagList( "out", 10 ); + boolean isCrafting = encodedValue.getBoolean( "crafting" ); + + // kinda needs info.. + if ( inTag.tagCount() == 0 || outTag.tagCount() == 0 ) + return; + + ItemStack[] in = new ItemStack[inTag.tagCount()]; + ItemStack[] out = new ItemStack[outTag.tagCount()]; + + readItems( in, inTag ); + readItems( out, outTag ); + + String label = (isCrafting ? GuiText.Crafts.getLocal() : GuiText.Creates.getLocal()) + ": "; + String and = " " + GuiText.And.getLocal() + " "; + String with = GuiText.With.getLocal() + ": "; + + boolean first = true; + for (int x = 0; x < out.length; x++) + { + if ( out[x] == null ) + continue; + + l.add( (first ? label : and) + out[x].stackSize + " " + Platform.getItemDisplayName( out[x] ) ); + first = false; + } + + first = true; + for (int x = 0; x < in.length; x++) + { + if ( in[x] == null ) + continue; + + l.add( (first ? with : and) + in[x].stackSize + " " + Platform.getItemDisplayName( in[x] ) ); + first = false; + } + } + + private void readItems(ItemStack[] itemList, NBTTagList tagSrc) + { + for (int x = 0; x < itemList.length; x++) + { + ItemStack readItem = ItemStack.loadItemStackFromNBT( tagSrc.getCompoundTagAt( x ) ); + if ( readItem != null ) + { + boolean used = false; + + for (int y = 0; y < x; y++) + { + if ( itemList[y] != null && Platform.isSameItemPrecise( readItem, itemList[y] ) ) + { + itemList[y].stackSize += readItem.stackSize; + used = true; + } + } + + if ( !used ) + itemList[x] = readItem; + } + } + } + + @Override + public ICraftingPatternDetails getPatternForItem(ItemStack is) + { + return null; + } + +} diff --git a/items/storage/ItemEncodedPattern.java b/items/storage/ItemEncodedPattern.java deleted file mode 100644 index d5a375ea..00000000 --- a/items/storage/ItemEncodedPattern.java +++ /dev/null @@ -1,26 +0,0 @@ -package appeng.items.storage; - -import java.util.EnumSet; - -import net.minecraft.item.ItemStack; -import appeng.api.crafting.ICraftingPatternDetails; -import appeng.api.implementations.ICraftingPatternItem; -import appeng.core.features.AEFeature; -import appeng.items.AEBaseItem; - -public class ItemEncodedPattern extends AEBaseItem implements ICraftingPatternItem -{ - - public ItemEncodedPattern() { - super( ItemEncodedPattern.class ); - setfeature( EnumSet.of( AEFeature.Crafting ) ); - setMaxStackSize( 1 ); - } - - @Override - public ICraftingPatternDetails getPatternForItem(ItemStack is) - { - return null; - } - -}