only register eventHandler once, modify lastClickTime to be a property of EntityPlayer

This commit is contained in:
loenaaaa committed 2026-01-28 11:51:48 +08:00
1 parent ff8d9822b0
commit 62e62fb758
3 files changed
+72 -9

No files matched your search

@@ -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);
@@ -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;
@@ -1196,7 +1209,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;
}
}
}
@@ -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) {
}
}