From 87f24867e8a1a703a6035c3cc3ddb16a493a2058 Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Sat, 6 Sep 2014 01:02:05 -0400 Subject: [PATCH] Add double-right-click behavior to drawers --- .../storagedrawers/block/BlockDrawers.java | 25 +++++++-------- .../block/tile/TileEntityDrawers.java | 31 +++++++++++++++++++ 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java index 79d938a..10553bb 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java @@ -154,29 +154,28 @@ public class BlockDrawers extends BlockContainer implements IExtendedBlockClickH @Override public boolean onBlockActivated (World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ) { - ItemStack item = player.inventory.getCurrentItem(); - if (item == null || item.getItem() == null) - return false; - TileEntityDrawers tileDrawers = getTileEntitySafe(world, x, y, z); + ItemStack item = player.inventory.getCurrentItem(); - if (item.getItem() == ModItems.upgrade) { - tileDrawers.setLevel(item.getItemDamage()); - world.markBlockForUpdate(x, y, z); + if (item != null && item.getItem() != null) { + if (item.getItem() == ModItems.upgrade) { + tileDrawers.setLevel(item.getItemDamage()); + world.markBlockForUpdate(x, y, z); - if (player != null && !player.capabilities.isCreativeMode) { - if (--item.stackSize <= 0) - player.inventory.setInventorySlotContents(player.inventory.currentItem, null); + if (player != null && !player.capabilities.isCreativeMode) { + if (--item.stackSize <= 0) + player.inventory.setInventorySlotContents(player.inventory.currentItem, null); + } + + return true; } - - return true; } if (tileDrawers.getDirection() != side) return false; int slot = getDrawerSlot(side, hitX, hitY, hitZ); - int countAdded = tileDrawers.putItemsIntoSlot(slot, item, item.stackSize); + int countAdded = tileDrawers.interactPutItemsIntoSlot(slot, player); //if (countAdded > 0) // world.markBlockForUpdate(x, y, z); diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java index 774e9c8..74aaeb8 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java @@ -14,6 +14,8 @@ import net.minecraft.util.MathHelper; import net.minecraftforge.common.util.Constants; import net.minecraftforge.common.util.ForgeDirection; +import java.util.UUID; + public class TileEntityDrawers extends TileEntity implements ISidedInventory { private class DrawerData { @@ -101,6 +103,9 @@ public class TileEntityDrawers extends TileEntity implements ISidedInventory private int[] autoSides = new int[] { 0, 1 }; + private long lastClickTime; + private UUID lastClickUUID; + public TileEntityDrawers () { setDrawerCount(2); } @@ -190,6 +195,32 @@ public class TileEntityDrawers extends TileEntity implements ISidedInventory return stack; } + public int interactPutItemsIntoSlot (int slot, EntityPlayer player) { + int count = 0; + + ItemStack currentStack = player.inventory.getCurrentItem(); + if (currentStack != null) + count += putItemsIntoSlot(slot, currentStack, currentStack.stackSize); + + if (worldObj.getWorldTime() - lastClickTime < 10 && player.getPersistentID().equals(lastClickUUID)) { + for (int i = 0; i < player.inventory.getSizeInventory(); i++) { + ItemStack subStack = player.inventory.getStackInSlot(i); + if (subStack != null) { + int subCount = putItemsIntoSlot(slot, subStack, subStack.stackSize); + if (subCount > 0 && subStack.stackSize == 0) + player.inventory.setInventorySlotContents(i, null); + + count += subCount; + } + } + } + + lastClickTime = worldObj.getWorldTime(); + lastClickUUID = player.getPersistentID(); + + return count; + } + public int putItemsIntoSlot (int slot, ItemStack stack, int count) { if (data[slot].getItem() == null) { data[slot].setItem(stack.getItem());