diff --git a/Treasure2-1.12.2/src/com/someguyssoftware/treasure2/client/gui/GuiHandler.java b/Treasure2-1.12.2/src/com/someguyssoftware/treasure2/client/gui/GuiHandler.java index 029122bff..4aff9d99a 100644 --- a/Treasure2-1.12.2/src/com/someguyssoftware/treasure2/client/gui/GuiHandler.java +++ b/Treasure2-1.12.2/src/com/someguyssoftware/treasure2/client/gui/GuiHandler.java @@ -4,14 +4,22 @@ package com.someguyssoftware.treasure2.client.gui; import com.someguyssoftware.treasure2.Treasure; +import com.someguyssoftware.treasure2.client.gui.inventory.KeyRingGui; import com.someguyssoftware.treasure2.client.gui.inventory.StandardChestGui; import com.someguyssoftware.treasure2.client.gui.inventory.StrongboxChestGui; +import com.someguyssoftware.treasure2.inventory.KeyRingInventory; +import com.someguyssoftware.treasure2.inventory.KeyRingContainer; import com.someguyssoftware.treasure2.inventory.StandardChestContainer; import com.someguyssoftware.treasure2.inventory.StrongboxChestContainer; +import com.someguyssoftware.treasure2.item.KeyRingItem; import com.someguyssoftware.treasure2.tileentity.AbstractTreasureChestTileEntity; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.IInventory; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.EnumHand; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.minecraftforge.fml.common.network.IGuiHandler; @@ -29,6 +37,7 @@ public class GuiHandler implements IGuiHandler { public static final int STANDARD_CHEST_GUIID = 1; public static final int STRONGBOX_CHEST_GUIID = 2; + public static final int KEY_RING_GUIID = 3; /* (non-Javadoc) @@ -51,6 +60,19 @@ public Object getServerGuiElement(int ID, EntityPlayer player, World world, int return new StrongboxChestContainer(player.inventory, ((AbstractTreasureChestTileEntity)tileEntity).getInventoryProxy()); } break; + case KEY_RING_GUIID: + // get the held item + ItemStack keyRingItem = player.getHeldItemMainhand(); + if (keyRingItem == null || !(keyRingItem.getItem() instanceof KeyRingItem)) { + keyRingItem = player.getHeldItemOffhand(); + if (keyRingItem == null || !(keyRingItem.getItem() instanceof KeyRingItem)) return null; + } + + // create inventory from item + IInventory inventory = new KeyRingInventory(keyRingItem); + // open the container + return new KeyRingContainer(player.inventory, inventory); + default: return null; } return null; @@ -72,6 +94,7 @@ public Object getClientGuiElement(int ID, EntityPlayer player, World world, int else { Treasure.logger.warn("Umm, GUI handler error - wrong tile entity."); } + break; case STRONGBOX_CHEST_GUIID: if (tileEntity instanceof AbstractTreasureChestTileEntity) { return new StrongboxChestGui(player.inventory, (AbstractTreasureChestTileEntity) tileEntity); @@ -80,6 +103,18 @@ public Object getClientGuiElement(int ID, EntityPlayer player, World world, int Treasure.logger.warn("Umm, GUI handler error - wrong tile entity."); } break; + case KEY_RING_GUIID: + // get the held item + ItemStack keyRingItem = player.getHeldItemMainhand(); + if (keyRingItem == null || !(keyRingItem.getItem() instanceof KeyRingItem)) { + keyRingItem = player.getHeldItemOffhand(); + if (keyRingItem == null || !(keyRingItem.getItem() instanceof KeyRingItem)) return null; + } + + // create inventory from item + IInventory inventory = new KeyRingInventory(keyRingItem); + // open the container + return new KeyRingGui(player.inventory, inventory); default: return null; } return null; diff --git a/Treasure2-1.12.2/src/com/someguyssoftware/treasure2/client/gui/inventory/KeyRingGui.java b/Treasure2-1.12.2/src/com/someguyssoftware/treasure2/client/gui/inventory/KeyRingGui.java index ae0852bc5..a7455cde8 100644 --- a/Treasure2-1.12.2/src/com/someguyssoftware/treasure2/client/gui/inventory/KeyRingGui.java +++ b/Treasure2-1.12.2/src/com/someguyssoftware/treasure2/client/gui/inventory/KeyRingGui.java @@ -6,6 +6,7 @@ import java.awt.Color; import com.someguyssoftware.treasure2.Treasure; +import com.someguyssoftware.treasure2.inventory.KeyRingContainer; import com.someguyssoftware.treasure2.inventory.StrongboxChestContainer; import com.someguyssoftware.treasure2.tileentity.AbstractTreasureChestTileEntity; @@ -23,22 +24,20 @@ public class KeyRingGui extends GuiContainer { // This is the resource location for the background image for the GUI - private static final ResourceLocation texture = new ResourceLocation(Treasure.MODID, "textures/gui/container/king_ring.png"); - private AbstractTreasureChestTileEntity tileEntity; + private static final ResourceLocation texture = new ResourceLocation(Treasure.MODID, "textures/gui/container/key_ring.png"); + private static final String KEY_RING_LABEL = "Key Ring"; /** - * NOTE can pass anything into the ChestGui (GuiContainer) as long as the player's inventory and the container's inventory is present. - * NOTE both can be IInventory - doesn't have to be TileEntity + * * @param invPlayer - * @param tileEntity + * @param inventory */ - public KeyRingGui(InventoryPlayer invPlayer, AbstractTreasureChestTileEntity tileEntity) { - super(new StrongboxChestContainer(invPlayer, (IInventory) tileEntity.getInventoryProxy())); - this.tileEntity = tileEntity; + public KeyRingGui(InventoryPlayer invPlayer, IInventory inventory) { + super(new KeyRingContainer(invPlayer, inventory)); // Set the width and height of the gui. Should match the size of the texture! xSize = 176; - ySize = 167; + ySize = 182; } /* (non-Javadoc) @@ -59,6 +58,6 @@ protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, i protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) { final int LABEL_XPOS = 5; final int LABEL_YPOS = 5; - fontRenderer.drawString(tileEntity.getDisplayName().getUnformattedText(), LABEL_XPOS, LABEL_YPOS, Color.darkGray.getRGB()); + fontRenderer.drawString(KEY_RING_LABEL, LABEL_XPOS, LABEL_YPOS, Color.darkGray.getRGB()); } } diff --git a/Treasure2-1.12.2/src/com/someguyssoftware/treasure2/inventory/InventoryProxy.java b/Treasure2-1.12.2/src/com/someguyssoftware/treasure2/inventory/InventoryProxy.java index 9f9c7a818..8c2c6f953 100644 --- a/Treasure2-1.12.2/src/com/someguyssoftware/treasure2/inventory/InventoryProxy.java +++ b/Treasure2-1.12.2/src/com/someguyssoftware/treasure2/inventory/InventoryProxy.java @@ -3,6 +3,7 @@ */ package com.someguyssoftware.treasure2.inventory; +import com.someguyssoftware.treasure2.Treasure; import com.someguyssoftware.treasure2.block.TreasureChestBlock; import com.someguyssoftware.treasure2.tileentity.AbstractTreasureChestTileEntity; @@ -177,6 +178,7 @@ public void closeInventory(EntityPlayer player) { */ @Override public boolean isItemValidForSlot(int index, ItemStack stack) { + Treasure.logger.debug("Proxy.isItemValid() being called @ {} : {}", index, stack); return true; } diff --git a/Treasure2-1.12.2/src/com/someguyssoftware/treasure2/inventory/KeyRingContainer.java b/Treasure2-1.12.2/src/com/someguyssoftware/treasure2/inventory/KeyRingContainer.java index 5ba436b8a..e3a29c604 100644 --- a/Treasure2-1.12.2/src/com/someguyssoftware/treasure2/inventory/KeyRingContainer.java +++ b/Treasure2-1.12.2/src/com/someguyssoftware/treasure2/inventory/KeyRingContainer.java @@ -5,11 +5,17 @@ import net.minecraft.inventory.Slot; /** - * This is the base/standard container for chests that is similar configuration to that of a vanilla container. + * This is a special container for the Key Ring * @author Mark Gottschling on Mar 9, 2018 * */ public class KeyRingContainer extends AbstractChestContainer { + class Point { + public int x; + public int y; + public Point(int x, int y) { this.x = x; this.y = y;} + } + /** * * @param invPlayer @@ -17,32 +23,65 @@ public class KeyRingContainer extends AbstractChestContainer { */ public KeyRingContainer(InventoryPlayer invPlayer, IInventory inventory) { super(invPlayer, inventory); - - // TODO column count and xpos doen't work for key ring where each slot is determined from a pre-made array // set the dimensions - setContainerInventoryColumnCount(5); - //key ring only has 15 slots, arranged in a circle - setContainerInventoryXPos(8 + getSlotXSpacing() + getSlotXSpacing()); + setContainerInventoryXPos(64); // build the container buildContainer(invPlayer, inventory); } /** - * TODO set this up from an array + * */ @Override - public void buildContainerInventory() { - /* - * Add the tile inventory container to the gui - */ - for (int y = 0; y < getContainerInventoryRowCount(); y++) { - for (int x = 0; x < getContainerInventoryColumnCount(); x++) { - int slotNumber = y * getContainerInventoryColumnCount() + x; - int xpos = getContainerInventoryXPos() + x * getSlotXSpacing(); - int ypos = getContainerInventoryYPos() + y * getSlotYSpacing(); - addSlotToContainer(new Slot(inventory, slotNumber, xpos, ypos)); + public void buildContainerInventory() { + Point[][] points = { + { + new Point(28, 38), + new Point(28 + getSlotXSpacing(), 38), + new Point(118, 38), + new Point(118 + getSlotXSpacing(), 38) + }, + { + new Point(28, 56), + new Point(28 + getSlotXSpacing(), 56), + new Point(118, 56), + new Point(118 + getSlotXSpacing(), 56) + } + }; + + // add top row + for (int x = 0; x < 3; x++) { + int slotNumber = x; + int xpos = getContainerInventoryXPos() + x * getSlotXSpacing(); + int ypos = 18; + addSlotToContainer(new KeyRingSlot(inventory, slotNumber, xpos, ypos)); + } + + // middle rows + for (int y = 0; y < 2; y++) { + for (int x = 0; x < 4; x++) { + int slotNumber = (y * 4 + x) + 3; + addSlotToContainer(new KeyRingSlot(inventory, slotNumber, points[y][x].x, points[y][x].y)); } + } + + // add bottom row + for (int x = 0; x < 3; x++) { + int slotNumber = x + 11; + int xpos = getContainerInventoryXPos() + x * getSlotXSpacing(); + int ypos = 76; + addSlotToContainer(new KeyRingSlot(inventory, slotNumber, xpos, ypos)); } } + + @Override + public int getHotbarYPos() { + return 158; + } + + @Override + public int getPlayerInventoryYPos() { + return 100; + } } diff --git a/Treasure2-1.12.2/src/com/someguyssoftware/treasure2/inventory/KeyRingInventory.java b/Treasure2-1.12.2/src/com/someguyssoftware/treasure2/inventory/KeyRingInventory.java new file mode 100644 index 000000000..aee3587b1 --- /dev/null +++ b/Treasure2-1.12.2/src/com/someguyssoftware/treasure2/inventory/KeyRingInventory.java @@ -0,0 +1,292 @@ +/** + * + */ +package com.someguyssoftware.treasure2.inventory; + +import com.someguyssoftware.treasure2.Treasure; +import com.someguyssoftware.treasure2.item.KeyItem; +import com.someguyssoftware.treasure2.tileentity.AbstractTreasureChestTileEntity; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.IInventory; +import net.minecraft.inventory.ItemStackHelper; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.util.NonNullList; +import net.minecraft.util.text.ITextComponent; +import net.minecraft.util.text.TextComponentString; +import net.minecraft.util.text.TextComponentTranslation; + +/** + * @author Mark Gottschling on Mar 9, 2018 + * + */ +public class KeyRingInventory implements IInventory { + + /* + * Reference to the owning ItemStack + */ + private ItemStack itemStack; + + /** IInventory properties */ + private int numberOfSlots = 15; // default size + private NonNullList items = NonNullList.withSize(numberOfSlots, ItemStack.EMPTY); + + /** + * + * @param stack + */ + public KeyRingInventory(ItemStack stack) { + // save a ref to the item stack + this.itemStack = stack; + + // copy items from stack to items property; + if (stack.hasTagCompound()) { + readInventoryFromNBT(stack.getTagCompound()); + } + } + + /** + * + * @param parentNBT + */ + public void readInventoryFromNBT(NBTTagCompound parentNBT) { + try { + // read the inventory + ItemStackHelper.loadAllItems(parentNBT, this.getItems()); + } + catch(Exception e) { + Treasure.logger.error("Error reading Properties from NBT:", e); + } + } + + /** + * Writes the inventory to NBT + * @param parentNBT + * @return + */ + public NBTTagCompound writeInventoryToNBT(NBTTagCompound parentNBT) { + try { + // write inventory + ItemStackHelper.saveAllItems(parentNBT, this.getItems()); + } + catch(Exception e) { + Treasure.logger.error("Error writing Inventory to NBT:", e); + } + return parentNBT; + } + + ///////////// IInventory Method + + /* (non-Javadoc) + * @see net.minecraft.world.IWorldNameable#getName() + */ + @Override + public String getName() { + return "display.key_ring.name"; + } + + /* (non-Javadoc) + * @see net.minecraft.world.IWorldNameable#hasCustomName() + */ + @Override + public boolean hasCustomName() { + return false; + } + + /* (non-Javadoc) + * @see net.minecraft.world.IWorldNameable#getDisplayName() + */ + @Override + public ITextComponent getDisplayName() { + return (ITextComponent) new TextComponentTranslation(this.getName(), new Object[0]); + } + + /* (non-Javadoc) + * @see net.minecraft.inventory.IInventory#getSizeInventory() + */ + @Override + public int getSizeInventory() { + return getNumberOfSlots(); + } + + /* (non-Javadoc) + * @see net.minecraft.inventory.IInventory#isEmpty() + */ + @Override + public boolean isEmpty() { + for (ItemStack itemstack : getItems()) { + if (!itemstack.isEmpty()) { + return false; + } + } + return true; + } + + /* (non-Javadoc) + * @see net.minecraft.inventory.IInventory#getStackInSlot(int) + */ + @Override + public ItemStack getStackInSlot(int index) { + return getItems().get(index); + } + + /* (non-Javadoc) + * @see net.minecraft.inventory.IInventory#decrStackSize(int, int) + */ + @Override + public ItemStack decrStackSize(int index, int count) { + ItemStack itemstack = ItemStackHelper.getAndSplit(getItems(), index, count); + if (!itemstack.isEmpty()) { + this.markDirty(); + } + return itemstack; + } + + /* (non-Javadoc) + * @see net.minecraft.inventory.IInventory#removeStackFromSlot(int) + */ + @Override + public ItemStack removeStackFromSlot(int index) { + return ItemStackHelper.getAndRemove(getItems(), index); + } + + /* (non-Javadoc) + * @see net.minecraft.inventory.IInventory#setInventorySlotContents(int, net.minecraft.item.ItemStack) + */ + @Override + public void setInventorySlotContents(int index, ItemStack stack) { + getItems().set(index, stack); + if (stack.getCount() > this.getInventoryStackLimit()) { + stack.setCount(this.getInventoryStackLimit()); + } + } + + /* (non-Javadoc) + * @see net.minecraft.inventory.IInventory#getInventoryStackLimit() + */ + @Override + public int getInventoryStackLimit() { + return 8; + } + + /* (non-Javadoc) + * @see net.minecraft.inventory.IInventory#markDirty() + */ + @Override + public void markDirty() { + } + + /* (non-Javadoc) + * @see net.minecraft.inventory.IInventory#isUsableByPlayer(net.minecraft.entity.player.EntityPlayer) + */ + @Override + public boolean isUsableByPlayer(EntityPlayer player) { + return true; + } + + /* (non-Javadoc) + * @see net.minecraft.inventory.IInventory#openInventory(net.minecraft.entity.player.EntityPlayer) + */ + @Override + public void openInventory(EntityPlayer player) { + } + + /* (non-Javadoc) + * @see net.minecraft.inventory.IInventory#closeInventory(net.minecraft.entity.player.EntityPlayer) + */ + @Override + public void closeInventory(EntityPlayer player) { + /* + * write the locked state to the nbt + */ + if (!getItemStack().hasTagCompound()) { + getItemStack().setTagCompound(new NBTTagCompound()); + } + writeInventoryToNBT(getItemStack().getTagCompound()); + } + + /* (non-Javadoc) + * @see net.minecraft.inventory.IInventory#isItemValidForSlot(int, net.minecraft.item.ItemStack) + */ + @Override + public boolean isItemValidForSlot(int index, ItemStack stack) { + return true; + } + + /* (non-Javadoc) + * @see net.minecraft.inventory.IInventory#getField(int) + */ + @Override + public int getField(int id) { + return 0; + } + + /* (non-Javadoc) + * @see net.minecraft.inventory.IInventory#setField(int, int) + */ + @Override + public void setField(int id, int value) { + } + + /* (non-Javadoc) + * @see net.minecraft.inventory.IInventory#getFieldCount() + */ + @Override + public int getFieldCount() { + return 0; + } + + /* (non-Javadoc) + * @see net.minecraft.inventory.IInventory#clear() + */ + @Override + public void clear() { + getItems().clear(); + } + + /** + * @return the itemStack + */ + public ItemStack getItemStack() { + return itemStack; + } + + /** + * @param itemStack the itemStack to set + */ + public void setItemStack(ItemStack itemStack) { + this.itemStack = itemStack; + } + + /** + * @return the numberOfSlots + */ + public int getNumberOfSlots() { + return numberOfSlots; + } + + /** + * @param numberOfSlots the numberOfSlots to set + */ + public void setNumberOfSlots(int numberOfSlots) { + this.numberOfSlots = numberOfSlots; + } + + /////////// End of IInventory methods + + /** + * @return the items + */ + public NonNullList getItems() { + return items; + } + + /** + * @param items the items to set + */ + public void setItems(NonNullList items) { + this.items = items; + } + +} diff --git a/Treasure2-1.12.2/src/com/someguyssoftware/treasure2/inventory/KeyRingSlot.java b/Treasure2-1.12.2/src/com/someguyssoftware/treasure2/inventory/KeyRingSlot.java new file mode 100644 index 000000000..25e7dcd5e --- /dev/null +++ b/Treasure2-1.12.2/src/com/someguyssoftware/treasure2/inventory/KeyRingSlot.java @@ -0,0 +1,40 @@ +/** + * + */ +package com.someguyssoftware.treasure2.inventory; + +import com.someguyssoftware.treasure2.item.KeyItem; + +import net.minecraft.inventory.IInventory; +import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemStack; +import net.minecraft.tileentity.TileEntityFurnace; + +/** + * @author Mark Gottschling on Mar 12, 2018 + * + */ +public class KeyRingSlot extends Slot { + + /** + * + * @param inventoryIn + * @param index + * @param xPosition + * @param yPosition + */ + public KeyRingSlot(IInventory inventoryIn, int index, int xPosition, int yPosition) { + super(inventoryIn, index, xPosition, yPosition); + } + + /** + * Check if the stack is allowed to be placed in this slot, used for armor slots as well as furnace fuel. + */ + public boolean isItemValid(ItemStack stack) { + return (stack.getItem() instanceof KeyItem); + } + + public int getItemStackLimit(ItemStack stack) { + return 8; + } +} diff --git a/Treasure2-1.12.2/src/com/someguyssoftware/treasure2/item/KeyItem.java b/Treasure2-1.12.2/src/com/someguyssoftware/treasure2/item/KeyItem.java index 38be7752b..fadf00ceb 100644 --- a/Treasure2-1.12.2/src/com/someguyssoftware/treasure2/item/KeyItem.java +++ b/Treasure2-1.12.2/src/com/someguyssoftware/treasure2/item/KeyItem.java @@ -174,14 +174,18 @@ public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos p LockState lockState = null; // check if this key is one that opens a lock (only first lock that key fits is unlocked). - for (LockState ls : tcte.getLockStates()) { - if (ls.getLock() != null) { - lockState = ls; - if (lockState.getLock().acceptsKey(this) || fitsLock(lockState.getLock())) { - fitsLock = true; - break; - } - } +// for (LockState ls : tcte.getLockStates()) { +// if (ls.getLock() != null) { +// lockState = ls; +// if (lockState.getLock().acceptsKey(this) || fitsLock(lockState.getLock())) { +// fitsLock = true; +// break; +// } +// } +// } + lockState = fitsFirstLock(tcte.getLockStates()); + if (lockState != null) { + fitsLock = true; } if (fitsLock) { @@ -207,11 +211,6 @@ public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos p heldItem.shrink(1); player.sendMessage(new TextComponentString("Key broke.")); worldIn.playSound(player, pos, SoundEvents.BLOCK_METAL_BREAK, SoundCategory.BLOCKS, 0.3F, 0.6F); - -// if (heldItem.getCount() <=0) { -// IInventory inventory = player.inventory; -// inventory.setInventorySlotContents(player.inventory.currentItem, null); -// } } else { player.sendMessage(new TextComponentString("Failed to unlock.")); @@ -239,6 +238,25 @@ public boolean fitsLock(LockItem lockItem) { return false; } + /** + * + * @param lockStates + * @return + */ + public LockState fitsFirstLock(List lockStates) { + LockState lockState = null; + // check if this key is one that opens a lock (only first lock that key fits is unlocked). + for (LockState ls : lockStates) { + if (ls.getLock() != null) { + lockState = ls; + if (lockState.getLock().acceptsKey(this) || fitsLock(lockState.getLock())) { + return ls; + } + } + } + return null; // <-- TODO should return EMPTY_LOCKSTATE + } + /** * * @param lockItem diff --git a/Treasure2-1.12.2/src/com/someguyssoftware/treasure2/item/KeyRingItem.java b/Treasure2-1.12.2/src/com/someguyssoftware/treasure2/item/KeyRingItem.java index b642d8a41..a1ad6a658 100644 --- a/Treasure2-1.12.2/src/com/someguyssoftware/treasure2/item/KeyRingItem.java +++ b/Treasure2-1.12.2/src/com/someguyssoftware/treasure2/item/KeyRingItem.java @@ -3,18 +3,37 @@ */ package com.someguyssoftware.treasure2.item; +import java.util.List; + import com.someguyssoftware.gottschcore.item.ModItem; import com.someguyssoftware.treasure2.Treasure; import com.someguyssoftware.treasure2.block.TreasureChestBlock; +import com.someguyssoftware.treasure2.client.gui.GuiHandler; +import com.someguyssoftware.treasure2.config.TreasureConfig; +import com.someguyssoftware.treasure2.inventory.KeyRingInventory; +import com.someguyssoftware.treasure2.lock.LockState; +import com.someguyssoftware.treasure2.tileentity.AbstractTreasureChestTileEntity; import net.minecraft.block.Block; +import net.minecraft.client.util.ITooltipFlag; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.init.Items; +import net.minecraft.init.SoundEvents; +import net.minecraft.inventory.IInventory; +import net.minecraft.inventory.InventoryHelper; +import net.minecraft.item.Item; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.tileentity.TileEntity; import net.minecraft.util.ActionResult; import net.minecraft.util.EnumActionResult; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; +import net.minecraft.util.SoundCategory; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.text.TextComponentString; +import net.minecraft.util.text.TextFormatting; +import net.minecraft.util.text.translation.I18n; import net.minecraft.world.World; /** @@ -22,7 +41,13 @@ * */ public class KeyRingItem extends ModItem { + private static final String USED_ON_CHEST = "usedOnChest"; + /* + * The GUIID; + */ + private int keyRingGuiID = GuiHandler.KEY_RING_GUIID; + /** * * @param modID @@ -32,31 +57,150 @@ public KeyRingItem(String modID, String name) { setItemName(modID, name); setCreativeTab(Treasure.TREASURE_TAB); } - + + /** + * + */ + @SuppressWarnings("deprecation") + @Override + public void addInformation(ItemStack stack, World worldIn, List tooltip, ITooltipFlag flagIn) { + super.addInformation(stack, worldIn, tooltip, flagIn); + + tooltip.add(I18n.translateToLocalFormatted("tooltip.label.key_ring", TextFormatting.GOLD)); + } + /** * */ @Override public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) { + + // exit if on the client + if (worldIn.isRemote) { + return EnumActionResult.FAIL; + } // use the key ring to unlock locks // determine if block at pos is a treasure chest Block block = worldIn.getBlockState(pos).getBlock(); if (block instanceof TreasureChestBlock) { + // get the tile entity + TileEntity te = worldIn.getTileEntity(pos); + if (te == null || !(te instanceof AbstractTreasureChestTileEntity)) { + Treasure.logger.warn("Null or incorrect TileEntity"); + return EnumActionResult.FAIL; + } + AbstractTreasureChestTileEntity tcte = (AbstractTreasureChestTileEntity)te; + + ItemStack heldItem = player.getHeldItem(hand); + if (!heldItem.hasTagCompound()) { + heldItem.setTagCompound(new NBTTagCompound()); + } + // set a flag that the item was used on a treasure chest + heldItem.getTagCompound().setBoolean(USED_ON_CHEST, true); + + // determine if chest is locked + if (!tcte.hasLocks()) { + return EnumActionResult.SUCCESS; + } + + try { + KeyRingInventory inv = new KeyRingInventory(heldItem); + // cycle through all keys in key ring until one is able to fit lock and use it to unlock the lock. + for (int i = 0; i < inv.getSizeInventory(); i++) { + ItemStack stack = inv.getStackInSlot(i); + if (stack != null && stack.getItem() != Items.AIR) { + KeyItem key = (KeyItem) stack.getItem(); + boolean breakKey = true; + // boolean fitsLock = false; + LockState lockState = null; + + // check if this key is one that opens a lock (only first lock that key fits is unlocked). + lockState = key.fitsFirstLock(tcte.getLockStates()); + + // TODO move to a method in KeyItem + if (lockState != null) { + if (key.unlock(lockState.getLock())) { + LockItem lock = lockState.getLock(); + // remove the lock + lockState.setLock(null); + // play noise + worldIn.playSound(player, pos, SoundEvents.BLOCK_LEVER_CLICK, SoundCategory.BLOCKS, 0.3F, 0.6F); + // update the client + tcte.sendUpdates(); + // spawn the lock + InventoryHelper.spawnItemStack(worldIn, (double)pos.getX(), (double)pos.getY(), (double)pos.getZ(), new ItemStack(lock)); + // don't break the key + breakKey = false; } + } + + // TODO move to a method in KeyItem + if (breakKey) { + if (key.isBreakable() && TreasureConfig.enableKeyBreaks) { + // break key; + heldItem.shrink(1); + player.sendMessage(new TextComponentString("Key broke.")); + worldIn.playSound(player, pos, SoundEvents.BLOCK_METAL_BREAK, SoundCategory.BLOCKS, 0.3F, 0.6F); + + } + else { + player.sendMessage(new TextComponentString("Failed to unlock.")); + if (isDamageable()) { + heldItem.damageItem(1, player); + } + } + } + } + } + } + catch (Exception e) { + Treasure.logger.error("error: ", e); + } + } - - return super.onItemUse(player, worldIn, pos, hand, facing, hitX, hitY, hitZ); + // this should prevent the onItemRightClick from happening./ + return EnumActionResult.PASS; } - + /** * */ @Override public ActionResult onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) { + // exit if on the client + if (worldIn.isRemote) { + return new ActionResult(EnumActionResult.FAIL, playerIn.getHeldItem(handIn)); + } - // open the key ring - // TODO Auto-generated method stub - return super.onItemRightClick(worldIn, playerIn, handIn); + boolean useOnChest = false; + ItemStack stack = playerIn.getHeldItem(handIn); + if (stack.hasTagCompound()) { + useOnChest = stack.getTagCompound().getBoolean(USED_ON_CHEST); + } + // exit if item already used on chest + if (useOnChest) { + stack.getTagCompound().setBoolean(USED_ON_CHEST, false); + return new ActionResult(EnumActionResult.FAIL, playerIn.getHeldItem(handIn)); + } + else { + BlockPos pos = playerIn.getPosition(); + playerIn.openGui(Treasure.instance, getKeyRingGuiID(), worldIn, pos.getX(), pos.getY(), pos.getZ()); + return super.onItemRightClick(worldIn, playerIn, handIn); + } + } + + /** + * @return the keyRingGuiID + */ + public int getKeyRingGuiID() { + return keyRingGuiID; + } + + /** + * @param keyRingGuiID the keyRingGuiID to set + */ + public void setKeyRingGuiID(int keyRingGuiID) { + this.keyRingGuiID = keyRingGuiID; } } diff --git a/Treasure2-1.12.2/src/resources/assets/treasure2/textures/items/keys/key_ring.png b/Treasure2-1.12.2/src/resources/assets/treasure2/textures/items/keys/key_ring.png index 66915042e..2d13602a8 100644 Binary files a/Treasure2-1.12.2/src/resources/assets/treasure2/textures/items/keys/key_ring.png and b/Treasure2-1.12.2/src/resources/assets/treasure2/textures/items/keys/key_ring.png differ diff --git a/Treasure2-1.12.2/src/resources/assets/treasure2/textures/items/keys/key_ring_old.png b/Treasure2-1.12.2/src/resources/assets/treasure2/textures/items/keys/key_ring_old.png new file mode 100644 index 000000000..66915042e Binary files /dev/null and b/Treasure2-1.12.2/src/resources/assets/treasure2/textures/items/keys/key_ring_old.png differ