From 604023eb80fe0c50fac956e93625c9be88bb9cd8 Mon Sep 17 00:00:00 2001 From: Draknyte1 Date: Thu, 20 Oct 2016 16:43:59 +1000 Subject: $ Fixed the workbench crafting grid/output doing odd things (Client side item dupes & actual item dupes) % Changed the workbench textures. --- .../core/block/machine/Machine_Workbench.java | 12 +- .../core/container/Container_Workbench.java | 188 ++++++++++++--------- .../gtPlusPlus/core/gui/machine/GUI_Workbench.java | 2 +- .../InventoryWorkbenchHoloCrafting.java | 6 +- .../inventories/InventoryWorkbenchHoloSlots.java | 10 +- src/Java/gtPlusPlus/core/slots/SlotOutput.java | 101 +++++++++++ .../tileentities/machines/TileEntityWorkbench.java | 105 ++++++++---- 7 files changed, 300 insertions(+), 124 deletions(-) create mode 100644 src/Java/gtPlusPlus/core/slots/SlotOutput.java (limited to 'src/Java/gtPlusPlus/core') diff --git a/src/Java/gtPlusPlus/core/block/machine/Machine_Workbench.java b/src/Java/gtPlusPlus/core/block/machine/Machine_Workbench.java index 308413d81f..270d0d1820 100644 --- a/src/Java/gtPlusPlus/core/block/machine/Machine_Workbench.java +++ b/src/Java/gtPlusPlus/core/block/machine/Machine_Workbench.java @@ -8,7 +8,6 @@ import net.minecraft.block.BlockContainer; import net.minecraft.block.material.Material; import net.minecraft.client.renderer.texture.IIconRegister; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.init.Blocks; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.IIcon; import net.minecraft.world.World; @@ -22,6 +21,8 @@ public class Machine_Workbench extends BlockContainer @SideOnly(Side.CLIENT) private IIcon textureTop; @SideOnly(Side.CLIENT) + private IIcon textureBottom; + @SideOnly(Side.CLIENT) private IIcon textureFront; @SuppressWarnings("deprecation") @@ -42,16 +43,17 @@ public class Machine_Workbench extends BlockContainer @SideOnly(Side.CLIENT) public IIcon getIcon(int p_149691_1_, int p_149691_2_) { - return p_149691_1_ == 1 ? this.textureTop : (p_149691_1_ == 0 ? Blocks.planks.getBlockTextureFromSide(p_149691_1_) : (p_149691_1_ != 2 && p_149691_1_ != 4 ? this.blockIcon : this.textureFront)); + return p_149691_1_ == 1 ? this.textureTop : (p_149691_1_ == 0 ? this.textureBottom : (p_149691_1_ != 2 && p_149691_1_ != 4 ? this.blockIcon : this.textureFront)); } @Override @SideOnly(Side.CLIENT) public void registerBlockIcons(IIconRegister p_149651_1_) { - this.blockIcon = p_149651_1_.registerIcon(CORE.MODID + ":" + "workbench"); - this.textureTop = p_149651_1_.registerIcon(CORE.MODID + ":" + "workbench" + "_top"); - this.textureFront = p_149651_1_.registerIcon(CORE.MODID + ":" + "workbench"); + this.blockIcon = p_149651_1_.registerIcon(CORE.MODID + ":" + " /TileEntities/" + "bronze_side_cabinet"); + this.textureTop = p_149651_1_.registerIcon(CORE.MODID + ":" + " /TileEntities/" + "bronze_top_crafting"); + this.textureBottom = p_149651_1_.registerIcon(CORE.MODID + ":" + " /TileEntities/" + "bronze_side"); + this.textureFront = p_149651_1_.registerIcon(CORE.MODID + ":" + " /TileEntities/" + "bronze_side_cabinet"); } /** diff --git a/src/Java/gtPlusPlus/core/container/Container_Workbench.java b/src/Java/gtPlusPlus/core/container/Container_Workbench.java index b3b024f52c..aa1fd1b509 100644 --- a/src/Java/gtPlusPlus/core/container/Container_Workbench.java +++ b/src/Java/gtPlusPlus/core/container/Container_Workbench.java @@ -11,6 +11,7 @@ import gtPlusPlus.core.item.general.ItemBlueprint; import gtPlusPlus.core.slots.SlotBlueprint; import gtPlusPlus.core.slots.SlotGeneric; import gtPlusPlus.core.slots.SlotGtTool; +import gtPlusPlus.core.slots.SlotOutput; import gtPlusPlus.core.tileentities.machines.TileEntityWorkbench; import gtPlusPlus.core.util.Utils; import gtPlusPlus.core.util.item.UtilsItems; @@ -18,7 +19,6 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.IInventory; -import net.minecraft.inventory.InventoryCraftResult; import net.minecraft.inventory.InventoryCrafting; import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; @@ -34,8 +34,6 @@ public class Container_Workbench extends Container { public final InventoryWorkbenchHoloSlots inventoryHolo; public final InventoryWorkbenchHoloCrafting inventoryCrafting; - public IInventory craftResult = new InventoryCraftResult(); - private World worldObj; private int posX; private int posY; @@ -96,7 +94,7 @@ public class Container_Workbench extends Container { this.inventoryTool = tile.inventoryTool; this.inventoryHolo = tile.inventoryHolo; this.inventoryCrafting = tile.inventoryCrafting; - + int var6; int var7; worldObj = tile.getWorldObj(); @@ -107,7 +105,7 @@ public class Container_Workbench extends Container { int o=0; //Output slot - addSlotToContainer(new SlotGeneric(inventoryHolo, 0, 136, 64)); + addSlotToContainer(new SlotOutput(inventory.player, this.craftMatrix, tile.inventoryCraftResult, 0, 136, 64)); //Util Slots addSlotToContainer(new SlotBlueprint(inventoryHolo, 1, 136, 28)); //Blueprint addSlotToContainer(new GT_Slot_Holo(inventoryHolo, 2, 154, 28, false, false, 1)); //Hopper @@ -123,17 +121,19 @@ public class Container_Workbench extends Container { o=0; + updateCraftingMatrix(); + //Crafting Grid for (var6 = 0; var6 < 3; ++var6) { for (var7 = 0; var7 < 3; ++var7) { this.addSlotToContainer(new Slot(this.craftMatrix, var7 + var6 * 3, 82 + var7 * 18, 28 + var6 * 18)); - - if (this.inventoryCrafting.getStackInSlot(o) != null){ + + /*if (this.inventoryCrafting.getStackInSlot(o) != null){ this.craftMatrix.setInventorySlotContents(o, inventoryCrafting.getStackInSlot(o)); this.inventoryCrafting.setInventorySlotContents(o, null); - } + } */ slotCrafting[o] = o+6; o++; } @@ -189,86 +189,114 @@ public class Container_Workbench extends Container { public ItemStack slotClick(int aSlotIndex, int aMouseclick, int aShifthold, EntityPlayer aPlayer){ if (!aPlayer.worldObj.isRemote){ - if (aSlotIndex == 999 || aSlotIndex == -999){ - //Utils.LOG_INFO("??? - "+aSlotIndex); - } + if (aSlotIndex == 999 || aSlotIndex == -999){ + //Utils.LOG_INFO("??? - "+aSlotIndex); + } - if (aSlotIndex == slotOutput){ - Utils.LOG_INFO("Player Clicked on the output slot"); - } + if (aSlotIndex == slotOutput){ + Utils.LOG_INFO("Player Clicked on the output slot"); + //TODO + } - for (int x : slotHolo){ - if (aSlotIndex == x){ - Utils.LOG_INFO("Player Clicked slot "+aSlotIndex+" in the Holo Grid"); - if (x == 1){ - Utils.LOG_INFO("Player Clicked Blueprint slot in the Holo Grid"); - } - else if (x == 2){ - Utils.LOG_INFO("Player Clicked Right Arrow slot in the Holo Grid"); - if (inventoryHolo.getStackInSlot(1) != null){ - Utils.LOG_INFO("Found an ItemStack."); - if (inventoryHolo.getStackInSlot(1).getItem() instanceof IItemBlueprint){ - Utils.LOG_INFO("Found a blueprint."); - ItemStack tempBlueprint = inventoryHolo.getStackInSlot(1); - ItemBlueprint tempItemBlueprint = (ItemBlueprint) tempBlueprint.getItem(); - if (inventoryHolo.getStackInSlot(0) != null && !tempItemBlueprint.hasBlueprint(tempBlueprint)){ - Utils.LOG_INFO("Output slot was not empty."); - Utils.LOG_INFO("Trying to manipulate NBT data on the blueprint stack, then replace it with the new one."); - tempItemBlueprint.setBlueprint(inventoryHolo.getStackInSlot(1), craftMatrix, inventoryHolo.getStackInSlot(0)); - ItemStack newTempBlueprint = UtilsItems.getSimpleStack(tempItemBlueprint); - inventoryHolo.setInventorySlotContents(1, newTempBlueprint); - Utils.LOG_INFO(UtilsItems.getArrayStackNames(tempItemBlueprint.getBlueprint(newTempBlueprint))); - } - else { - if (tempItemBlueprint.hasBlueprint(tempBlueprint)){ - Utils.LOG_INFO("Blueprint already holds a recipe."); + for (int x : slotHolo){ + if (aSlotIndex == x){ + Utils.LOG_INFO("Player Clicked slot "+aSlotIndex+" in the Holo Grid"); + if (x == 1){ + Utils.LOG_INFO("Player Clicked Blueprint slot in the Holo Grid"); + } + else if (x == 2){ + Utils.LOG_INFO("Player Clicked Right Arrow slot in the Holo Grid"); + if (inventoryHolo.getStackInSlot(1) != null){ + Utils.LOG_INFO("Found an ItemStack."); + if (inventoryHolo.getStackInSlot(1).getItem() instanceof IItemBlueprint){ + Utils.LOG_INFO("Found a blueprint."); + ItemStack tempBlueprint = inventoryHolo.getStackInSlot(1); + ItemBlueprint tempItemBlueprint = (ItemBlueprint) tempBlueprint.getItem(); + if (inventoryHolo.getStackInSlot(0) != null && !tempItemBlueprint.hasBlueprint(tempBlueprint)){ + Utils.LOG_INFO("Output slot was not empty."); + Utils.LOG_INFO("Trying to manipulate NBT data on the blueprint stack, then replace it with the new one."); + tempItemBlueprint.setBlueprint(inventoryHolo.getStackInSlot(1), craftMatrix, inventoryHolo.getStackInSlot(0)); + ItemStack newTempBlueprint = UtilsItems.getSimpleStack(tempItemBlueprint); + inventoryHolo.setInventorySlotContents(1, newTempBlueprint); + Utils.LOG_INFO(UtilsItems.getArrayStackNames(tempItemBlueprint.getBlueprint(newTempBlueprint))); } else { - Utils.LOG_INFO("Output slot was empty."); + if (tempItemBlueprint.hasBlueprint(tempBlueprint)){ + Utils.LOG_INFO("Blueprint already holds a recipe."); + } + else { + Utils.LOG_INFO("Output slot was empty."); + } } } + else { + Utils.LOG_INFO("ItemStack found was not a blueprint."); + } } else { - Utils.LOG_INFO("ItemStack found was not a blueprint."); + Utils.LOG_INFO("No ItemStack found in Blueprint slot."); } } - else { - Utils.LOG_INFO("No ItemStack found in Blueprint slot."); + else if (x == 3){ + Utils.LOG_INFO("Player Clicked Big [P] slot in the Holo Grid"); + } + else if (x == 4){ + Utils.LOG_INFO("Player Clicked Transfer to Crafting Grid slot in the Holo Grid"); + } + else if (x == 5){ + Utils.LOG_INFO("Player Clicked Transfer to Storage Grid slot in the Holo Grid"); } - } - else if (x == 3){ - Utils.LOG_INFO("Player Clicked Big [P] slot in the Holo Grid"); - } - else if (x == 4){ - Utils.LOG_INFO("Player Clicked Transfer to Crafting Grid slot in the Holo Grid"); - } - else if (x == 5){ - Utils.LOG_INFO("Player Clicked Transfer to Storage Grid slot in the Holo Grid"); } } - } - for (int x : slotCrafting){ - if (aSlotIndex == x){ - Utils.LOG_INFO("Player Clicked slot "+aSlotIndex+" in the crafting Grid"); + for (int x : slotCrafting){ + if (aSlotIndex == x){ + Utils.LOG_INFO("Player Clicked slot "+aSlotIndex+" in the crafting Grid"); + } } - } - for (int x : slotStorage){ - if (aSlotIndex == x){ - Utils.LOG_INFO("Player Clicked slot "+aSlotIndex+" in the storage Grid"); + for (int x : slotStorage){ + if (aSlotIndex == x){ + Utils.LOG_INFO("Player Clicked slot "+aSlotIndex+" in the storage Grid"); + } } - } - for (int x : slotTools){ - if (aSlotIndex == x){ - Utils.LOG_INFO("Player Clicked slot "+aSlotIndex+" in the tool Grid"); + for (int x : slotTools){ + if (aSlotIndex == x){ + Utils.LOG_INFO("Player Clicked slot "+aSlotIndex+" in the tool Grid"); + } } } - } //Utils.LOG_INFO("Player Clicked slot "+aSlotIndex+" in the Grid"); return super.slotClick(aSlotIndex, aMouseclick, aShifthold, aPlayer); } + private void updateCraftingMatrix() { + for (int i = 0; i < craftMatrix.getSizeInventory(); i++) { + craftMatrix.setInventorySlotContents(i, tile_entity.inventoryCrafting.getStackInSlot(i)); + } + } + + @Override + public void onCraftMatrixChanged(IInventory iiventory) { + tile_entity.inventoryCraftResult.setInventorySlotContents(0, CraftingManager.getInstance().findMatchingRecipe(craftMatrix, worldObj)); + } + @Override + public void onContainerClosed(EntityPlayer par1EntityPlayer) + { + super.onContainerClosed(par1EntityPlayer); + saveCraftingMatrix(); + } + + private void saveCraftingMatrix() { + for (int i = 0; i < craftMatrix.getSizeInventory(); i++) { + tile_entity.inventoryCrafting.setInventorySlotContents(i, craftMatrix.getStackInSlot(i)); + } + } + + + + + /*@Override public void onCraftMatrixChanged(IInventory par1IInventory){ //Custom Recipe Handler //craftResult.setInventorySlotContents(0, Workbench_CraftingHandler.getInstance().findMatchingRecipe(craftMatrix, worldObj)); @@ -278,25 +306,25 @@ public class Container_Workbench extends Container { ItemStack temp = CraftingManager.getInstance().findMatchingRecipe(craftMatrix, worldObj); if (temp != null){ Utils.LOG_INFO("Output found. "+temp.getDisplayName()+" x"+temp.stackSize); - inventoryHolo.setInventorySlotContents(slotOutput, temp); + craftResult.setInventorySlotContents(slotOutput, temp); } else { Utils.LOG_INFO("No Valid output found."); + craftResult.setInventorySlotContents(slotOutput, null); } - } + }*/ - - @Override + /*@Override public void onContainerClosed(EntityPlayer par1EntityPlayer) { for (int o=0; o= 0 && slot < INV_SIZE) + if(slot >= 1 && slot < INV_SIZE) { inventory[slot] = ItemStack.loadItemStackFromNBT(data); } @@ -46,7 +50,7 @@ public class InventoryWorkbenchHoloSlots implements IInventory{ for(int i = 0;i itemstack2.getMaxDamage()) + { + MinecraftForge.EVENT_BUS.post(new PlayerDestroyItemEvent(thePlayer, itemstack2)); + itemstack2 = null; + } + if (!this.thePlayer.inventory.addItemStackToInventory(itemstack2)) + { + if (this.craftMatrix.getStackInSlot(i) == null) + { + this.craftMatrix.setInventorySlotContents(i, itemstack2); + } + else + { + this.thePlayer.dropPlayerItemWithRandomChoice(itemstack2, false); + } + } + } + } + } + } + } +} \ No newline at end of file diff --git a/src/Java/gtPlusPlus/core/tileentities/machines/TileEntityWorkbench.java b/src/Java/gtPlusPlus/core/tileentities/machines/TileEntityWorkbench.java index b0c4fefbf0..725967c31e 100644 --- a/src/Java/gtPlusPlus/core/tileentities/machines/TileEntityWorkbench.java +++ b/src/Java/gtPlusPlus/core/tileentities/machines/TileEntityWorkbench.java @@ -4,16 +4,24 @@ import gtPlusPlus.core.inventories.InventoryWorkbenchChest; import gtPlusPlus.core.inventories.InventoryWorkbenchHoloCrafting; import gtPlusPlus.core.inventories.InventoryWorkbenchHoloSlots; import gtPlusPlus.core.inventories.InventoryWorkbenchTools; +import net.minecraft.inventory.IInventory; +import net.minecraft.inventory.InventoryCraftResult; +import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; import net.minecraft.tileentity.TileEntity; public class TileEntityWorkbench extends TileEntity { + + //Credit to NovaViper in http://www.minecraftforge.net/forum/index.php?topic=26439.0 - Helped me restructure my Inventory system and now the crafting matrix works better. public InventoryWorkbenchChest inventoryChest; public InventoryWorkbenchTools inventoryTool; public InventoryWorkbenchHoloSlots inventoryHolo; - public InventoryWorkbenchHoloCrafting inventoryCrafting; - + public InventoryWorkbenchHoloCrafting inventoryCrafting; + + public IInventory inventoryCraftResult = new InventoryCraftResult(); + public TileEntityWorkbench(){ this.inventoryTool = new InventoryWorkbenchTools();//number of slots - without product slot this.inventoryChest = new InventoryWorkbenchChest();//number of slots - without product slot @@ -21,38 +29,71 @@ public class TileEntityWorkbench extends TileEntity { this.inventoryCrafting = new InventoryWorkbenchHoloCrafting(); this.canUpdate(); } - + @SuppressWarnings("static-method") public NBTTagCompound getTag(NBTTagCompound nbt, String tag) - { - if(!nbt.hasKey(tag)) - { - nbt.setTag(tag, new NBTTagCompound()); + { + if(!nbt.hasKey(tag)) + { + nbt.setTag(tag, new NBTTagCompound()); + } + return nbt.getCompoundTag(tag); + } + + @Override + public void writeToNBT(NBTTagCompound nbt) + { + super.writeToNBT(nbt); + inventoryChest.writeToNBT(getTag(nbt, "ContentsChest")); + inventoryTool.writeToNBT(getTag(nbt, "ContentsTools")); + //inventoryCrafting.writeToNBT(getTag(nbt, "ContentsCrafting")); + inventoryHolo.writeToNBT(getTag(nbt, "ContentsHolo")); + + // Write Crafting Matrix to NBT + NBTTagList craftingTag = new NBTTagList(); + for (int currentIndex = 0; currentIndex < inventoryCrafting.getSizeInventory(); ++currentIndex) { + if (inventoryCrafting.getStackInSlot(currentIndex) != null) { + NBTTagCompound tagCompound = new NBTTagCompound(); + tagCompound.setByte("Slot", (byte) currentIndex); + inventoryCrafting.getStackInSlot(currentIndex).writeToNBT(tagCompound); + craftingTag.appendTag(tagCompound); + } } - return nbt.getCompoundTag(tag); - } - - @Override - public void writeToNBT(NBTTagCompound nbt) - { - super.writeToNBT(nbt); - inventoryChest.writeToNBT(getTag(nbt, "ContentsChest")); - inventoryTool.writeToNBT(getTag(nbt, "ContentsTools")); - inventoryCrafting.writeToNBT(getTag(nbt, "ContentsCrafting")); - inventoryHolo.writeToNBT(getTag(nbt, "ContentsHolo")); - - } - - @Override - public void readFromNBT(NBTTagCompound nbt) - { - super.readFromNBT(nbt); - inventoryChest.readFromNBT(nbt.getCompoundTag("ContentsChest")); - inventoryTool.readFromNBT(nbt.getCompoundTag("ContentsTools")); - inventoryCrafting.readFromNBT(nbt.getCompoundTag("ContentsCrafting")); - inventoryHolo.readFromNBT(nbt.getCompoundTag("ContentsHolo")); - } - - + + nbt.setTag("CraftingMatrix", craftingTag); + // Write craftingResult to NBT + if (inventoryCraftResult.getStackInSlot(0) != null) + nbt.setTag("CraftingResult", inventoryCraftResult.getStackInSlot(0).writeToNBT(new NBTTagCompound())); + + } + + @Override + public void readFromNBT(NBTTagCompound nbt) + { + super.readFromNBT(nbt); + inventoryChest.readFromNBT(nbt.getCompoundTag("ContentsChest")); + inventoryTool.readFromNBT(nbt.getCompoundTag("ContentsTools")); + //inventoryCrafting.readFromNBT(nbt.getCompoundTag("ContentsCrafting")); + inventoryHolo.readFromNBT(nbt.getCompoundTag("ContentsHolo")); + + // Read in the Crafting Matrix from NBT + NBTTagList craftingTag = nbt.getTagList("CraftingMatrix", 10); + inventoryCrafting = new InventoryWorkbenchHoloCrafting(); //TODO: magic number + for (int i = 0; i < craftingTag.tagCount(); ++i) { + NBTTagCompound tagCompound = (NBTTagCompound) craftingTag.getCompoundTagAt(i); + byte slot = tagCompound.getByte("Slot"); + if (slot >= 0 && slot < inventoryCrafting.getSizeInventory()) { + inventoryCrafting.setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(tagCompound)); + } + } + + + // Read craftingResult from NBT + NBTTagCompound tagCraftResult = nbt.getCompoundTag("CraftingResult"); + inventoryCraftResult.setInventorySlotContents(0, ItemStack.loadItemStackFromNBT(tagCraftResult)); + + } + + } \ No newline at end of file -- cgit