diff --git a/src/main/java/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroupInteractive.java b/src/main/java/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroupInteractive.java index 1730728..2aac434 100644 --- a/src/main/java/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroupInteractive.java +++ b/src/main/java/com/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroupInteractive.java @@ -5,7 +5,7 @@ import net.minecraft.item.ItemStack; public interface IDrawerGroupInteractive extends IDrawerGroup { - ItemStack takeItemsFromSlot(int slot, int count); + ItemStack takeItemsFromSlot(int slot, int count, EntityPlayer player); int putItemsIntoSlot(int slot, ItemStack stack, int count); diff --git a/src/main/java/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java b/src/main/java/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java index 7745035..ee3386d 100644 --- a/src/main/java/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java +++ b/src/main/java/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java @@ -44,6 +44,7 @@ import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.ISealable; import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.LockAttribute; import com.jaquadro.minecraft.storagedrawers.block.BlockDrawers; import com.jaquadro.minecraft.storagedrawers.block.BlockDrawersCustom; +import com.jaquadro.minecraft.storagedrawers.common.DrawerClickProperty; import com.jaquadro.minecraft.storagedrawers.config.ConfigManager; import com.jaquadro.minecraft.storagedrawers.core.ModItems; import com.jaquadro.minecraft.storagedrawers.inventory.ISideManager; @@ -77,7 +78,8 @@ public abstract class TileEntityDrawers extends BaseTileEntity implements IDrawe private int ticksClickedInARow = 9; private int itemsOutputInARow = 0; - private long lastLeftClickTime; + + private static boolean eventsRegistered = false; private UUID owner; private String securityKey; @@ -97,8 +99,11 @@ public abstract class TileEntityDrawers extends BaseTileEntity implements IDrawe protected TileEntityDrawers(int drawerCount) { initWithDrawerCount(drawerCount); - TileEntityDrawersEvents eventHandler = new TileEntityDrawersEvents(); - MinecraftForge.EVENT_BUS.register(eventHandler); + if (!eventsRegistered) { + TileEntityDrawersEvents eventHandler = new TileEntityDrawersEvents(); + MinecraftForge.EVENT_BUS.register(eventHandler); + eventsRegistered = true; + } } protected abstract IDrawer createDrawer(int slot); @@ -404,7 +409,7 @@ public abstract class TileEntityDrawers extends BaseTileEntity implements IDrawe } int amount = getTakeAmount(player, invertShift, drawer); - ItemStack item = takeItemsFromSlot(slot, amount); + ItemStack item = takeItemsFromSlot(slot, amount, player); traceItem(item); @@ -610,23 +615,31 @@ public abstract class TileEntityDrawers extends BaseTileEntity implements IDrawe materialTrim = material; } + private DrawerClickProperty getProperties(EntityPlayer player) { + return (DrawerClickProperty) player.getExtendedProperties(DrawerClickProperty.PROP_KEY); + } + /** * Returns an ItemStack that will have a maximum size of {@link ItemStack#getMaxStackSize()} */ - public ItemStack takeItemsFromSlot(int slot, int count) { + public ItemStack takeItemsFromSlot(int slot, int count, EntityPlayer player) { if (slot < 0 || slot >= getDrawerCount()) return null; ItemStack stack = getItemsFromSlot(slot, count); if (stack == null) return null; - if (worldObj.getTotalWorldTime() - lastLeftClickTime > 2) { + DrawerClickProperty property = getProperties(player); + + if (property == null) return null; + + if (worldObj.getTotalWorldTime() - property.lastLeftClickTime > 2) { ticksClickedInARow = 9; itemsOutputInARow = 0; } else { ticksClickedInARow++; } - lastLeftClickTime = worldObj.getTotalWorldTime(); + property.lastLeftClickTime = worldObj.getTotalWorldTime(); Minecraft.getMinecraft().playerController.curBlockDamageMP = 0.0F; @@ -1216,7 +1229,8 @@ public abstract class TileEntityDrawers extends BaseTileEntity implements IDrawe @SubscribeEvent(priority = EventPriority.NORMAL) public void whenPlayerChangesDimension(PlayerEvent.PlayerChangedDimensionEvent event) { - lastLeftClickTime = 0; + DrawerClickProperty property = getProperties(event.player); + property.lastLeftClickTime = 0; } } } diff --git a/src/main/java/com/jaquadro/minecraft/storagedrawers/common/DrawerClickProperty.java b/src/main/java/com/jaquadro/minecraft/storagedrawers/common/DrawerClickProperty.java new file mode 100644 index 0000000..3928cbf --- /dev/null +++ b/src/main/java/com/jaquadro/minecraft/storagedrawers/common/DrawerClickProperty.java @@ -0,0 +1,49 @@ +package com.jaquadro.minecraft.storagedrawers.common; + +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.world.World; +import net.minecraftforge.common.IExtendedEntityProperties; +import net.minecraftforge.event.entity.EntityEvent; + +import com.gtnewhorizon.gtnhlib.eventbus.EventBusSubscriber; + +import cpw.mods.fml.common.eventhandler.SubscribeEvent; + +@EventBusSubscriber +public class DrawerClickProperty implements IExtendedEntityProperties { + + public static final String PROP_KEY = "drawer-click"; + + public long lastLeftClickTime; + + @SubscribeEvent + public static void onEntityConstructing(EntityEvent.EntityConstructing event) { + if (event.entity instanceof EntityPlayer player) { + player.registerExtendedProperties(PROP_KEY, new DrawerClickProperty()); + } + } + + @Override + public void saveNBTData(NBTTagCompound compound) { + NBTTagCompound tag = new NBTTagCompound(); + compound.setTag(PROP_KEY, tag); + + tag.setLong("lastLeftClickTime", lastLeftClickTime); + } + + @Override + public void loadNBTData(NBTTagCompound compound) { + NBTTagCompound tag = compound.getCompoundTag(PROP_KEY); + + if (tag != null) { + lastLeftClickTime = tag.getLong("lastLeftClickTime"); + } + } + + @Override + public void init(Entity entity, World world) { + + } +}