From ea2e3da07d01925cfdf744fbc08f813f910b5547 Mon Sep 17 00:00:00 2001 From: DrParadox7 Date: Mon, 25 Sep 2023 16:40:42 +0200 Subject: [PATCH 1/3] Personal Chest Pocket Access Config This controls access to Personal Chests from inventories --- .../java/mekanism/api/MekanismConfig.java | 7 +++-- .../java/mekanism/client/ClientProxy.java | 28 ++++++++++--------- .../java/mekanism/common/CommonProxy.java | 2 +- .../common/network/PacketConfigSync.java | 6 ++-- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/main/java/mekanism/api/MekanismConfig.java b/src/main/java/mekanism/api/MekanismConfig.java index 7533a303fe3..48b827aaa72 100644 --- a/src/main/java/mekanism/api/MekanismConfig.java +++ b/src/main/java/mekanism/api/MekanismConfig.java @@ -72,7 +72,7 @@ public static class general } public static class mekce { - public static boolean enableBoPProgression = true; + public static boolean enableBoPProgression; public static boolean EnableQuartzCompat; public static boolean EnableDiamondCompat; public static boolean EnablePoorOresCompat; @@ -82,8 +82,9 @@ public static class mekce { public static int IC2SourceTier; public static int digitalMinerMaxRadius; public static boolean enableSingleUseCardboxes; - public static boolean accurateHeavyWaterCapture = true; - public static int heavyWaterChance = 4; + public static boolean enablePersonalChestPocketAccess; + public static boolean accurateHeavyWaterCapture; + public static int heavyWaterChance; } diff --git a/src/main/java/mekanism/client/ClientProxy.java b/src/main/java/mekanism/client/ClientProxy.java index f94e5271e1e..9cdaa2a99c5 100644 --- a/src/main/java/mekanism/client/ClientProxy.java +++ b/src/main/java/mekanism/client/ClientProxy.java @@ -60,6 +60,8 @@ import java.io.File; +import static mekanism.api.MekanismConfig.mekce.enablePersonalChestPocketAccess; + /** * Client proxy for the Mekanism mod. * @author AidanBrady @@ -117,9 +119,9 @@ public void openPersonalChest(EntityPlayer entityplayer, int id, int windowId, b FMLClientHandler.instance().displayGuiScreen(entityplayer, new GuiPersonalChest(entityplayer.inventory, tileEntity)); entityplayer.openContainer.windowId = windowId; } - else { + else if (enablePersonalChestPocketAccess) + { ItemStack stack = entityplayer.getCurrentEquippedItem(); - if(MachineType.get(stack) == MachineType.PERSONAL_CHEST) { InventoryPersonalChest inventory = new InventoryPersonalChest(entityplayer); @@ -386,29 +388,29 @@ public GuiScreen getClientGui(int ID, EntityPlayer player, World world, int x, i case 58: return new GuiFuelwoodHeater(player.inventory, (TileEntityFuelwoodHeater)tileEntity); } - + return null; } - + @Override public void handleTeleporterUpdate(PortableTeleporterMessage message) { GuiScreen screen = Minecraft.getMinecraft().currentScreen; - + if(screen instanceof GuiTeleporter && ((GuiTeleporter)screen).itemStack != null) { GuiTeleporter teleporter = (GuiTeleporter)screen; - + teleporter.clientStatus = message.status; teleporter.clientFreq = message.frequency; teleporter.clientPublicCache = message.publicCache; teleporter.clientPrivateCache = message.privateCache; teleporter.clientProtectedCache = message.protectedCache; - + teleporter.updateButtons(); } } - + @Override public void addHitEffects(Coord4D coord, MovingObjectPosition mop) { @@ -417,7 +419,7 @@ public void addHitEffects(Coord4D coord, MovingObjectPosition mop) Minecraft.getMinecraft().effectRenderer.addBlockHitEffects(coord.xCoord, coord.yCoord, coord.zCoord, mop); } } - + @Override public void doGenericSparkle(TileEntity tileEntity, INodeChecker checker) { @@ -432,7 +434,7 @@ public void doMultiblockSparkle(final TileEntityMultiblock tileEntity) @Override public boolean isNode(TileEntity tile) { - return MultiblockManager.areEqual(tile, tileEntity); + return MultiblockManager.areEqual(tile, tileEntity); } }).run(); } @@ -442,11 +444,11 @@ public boolean isNode(TileEntity tile) public void loadUtilities() { super.loadUtilities(); - + FMLCommonHandler.instance().bus().register(new ClientPlayerTracker()); FMLCommonHandler.instance().bus().register(new ClientTickHandler()); FMLCommonHandler.instance().bus().register(new RenderTickHandler()); - + new MekanismKeyHandler(); HolidayManager.init(); @@ -522,7 +524,7 @@ public void renderLaser(World world, Pos3D from, Pos3D to, ForgeDirection direct { Minecraft.getMinecraft().effectRenderer.addEffect(new EntityLaser(world, from, to, direction, energy)); } - + @Override public FontRenderer getFontRenderer() { diff --git a/src/main/java/mekanism/common/CommonProxy.java b/src/main/java/mekanism/common/CommonProxy.java index 2d015b70eb9..f2dbd09304a 100644 --- a/src/main/java/mekanism/common/CommonProxy.java +++ b/src/main/java/mekanism/common/CommonProxy.java @@ -315,7 +315,7 @@ public void loadConfiguration() mekce.enableSiliconCompat = Mekanism.configurationce.get("mekce", "EnableSiliconCompat", true, "When a mod that adds silicon (galacticraft, enderio and projectred) is detected, recipe for control circuit is changed from using iron to silicon in the metalurgic infuser").getBoolean(); mekce.digitalMinerMaxRadius = Mekanism.configurationce.get("mekce", "DigitalMinerMaxRadius", 32, "Select range from 1 to 64, we do not recommend going over 32", 1, 64).getInt(); mekce.enableSingleUseCardboxes = Mekanism.configurationce.get("mekce", "EnableSingleUseCardboxes", true, "This allows to force single use on cardboxes or not").getBoolean(); - + mekce.enablePersonalChestPocketAccess = Mekanism.configurationce.get("mekce", "enablePersonalChestPocketAccess", true, "This allows opening Personal Chests from your inventory without having to place them").getBoolean(); mekce.IC2SinkTier = Mekanism.configurationce.get("mekce", "IC2SinkTier", 4, "Tier level for machines, we recommend setting it to tier 2 for ic2 classic", 1, 4).getInt(); mekce.IC2SourceTier = Mekanism.configurationce.get("mekce", "IC2SourceTier", 4, "Tier level for generators, we recommend setting it to tier 2 for ic2 classic", 1, 4).getInt(); diff --git a/src/main/java/mekanism/common/network/PacketConfigSync.java b/src/main/java/mekanism/common/network/PacketConfigSync.java index 44f428974b1..e43a33d5fd4 100644 --- a/src/main/java/mekanism/common/network/PacketConfigSync.java +++ b/src/main/java/mekanism/common/network/PacketConfigSync.java @@ -86,7 +86,8 @@ public void toBytes(ByteBuf dataStream) dataStream.writeInt(mekce.IC2SourceTier); dataStream.writeInt(mekce.digitalMinerMaxRadius); dataStream.writeBoolean(mekce.enableSingleUseCardboxes); - + dataStream.writeBoolean(mekce.enablePersonalChestPocketAccess); + for(MachineType type : MachineType.getValidMachines()) { dataStream.writeBoolean(machines.isEnabled(type.name)); @@ -281,7 +282,8 @@ public void fromBytes(ByteBuf dataStream) mekce.IC2SourceTier = dataStream.readInt(); mekce.digitalMinerMaxRadius = dataStream.readInt(); mekce.enableSingleUseCardboxes = dataStream.readBoolean(); - + mekce.enablePersonalChestPocketAccess = dataStream.readBoolean(); + for(MachineType type : MachineType.getValidMachines()) { machines.setEntry(type.name, dataStream.readBoolean()); From 70a4376f54fe8a7d188eeeba4deea16f7f24175d Mon Sep 17 00:00:00 2001 From: DrParadox7 Date: Mon, 25 Sep 2023 18:53:25 +0200 Subject: [PATCH 2/3] Fix #130 --- .../java/mekanism/client/ClientProxy.java | 4 +- .../mekanism/client/gui/GuiPersonalChest.java | 6 +- .../container/ContainerPersonalChest.java | 12 ++-- .../common/network/PacketPersonalChest.java | 61 ++++++++++--------- .../mekanism/common/util/MekanismUtils.java | 9 +-- 5 files changed, 49 insertions(+), 43 deletions(-) diff --git a/src/main/java/mekanism/client/ClientProxy.java b/src/main/java/mekanism/client/ClientProxy.java index 9cdaa2a99c5..8019cd1e1e6 100644 --- a/src/main/java/mekanism/client/ClientProxy.java +++ b/src/main/java/mekanism/client/ClientProxy.java @@ -122,10 +122,12 @@ public void openPersonalChest(EntityPlayer entityplayer, int id, int windowId, b else if (enablePersonalChestPocketAccess) { ItemStack stack = entityplayer.getCurrentEquippedItem(); + int hotbarSlot = entityplayer.inventory.currentItem; + if(MachineType.get(stack) == MachineType.PERSONAL_CHEST) { InventoryPersonalChest inventory = new InventoryPersonalChest(entityplayer); - FMLClientHandler.instance().displayGuiScreen(entityplayer, new GuiPersonalChest(entityplayer.inventory, inventory)); + FMLClientHandler.instance().displayGuiScreen(entityplayer, new GuiPersonalChest(entityplayer.inventory, inventory, hotbarSlot)); entityplayer.openContainer.windowId = windowId; } } diff --git a/src/main/java/mekanism/client/gui/GuiPersonalChest.java b/src/main/java/mekanism/client/gui/GuiPersonalChest.java index 6ecfe4e2037..b50937afa2c 100644 --- a/src/main/java/mekanism/client/gui/GuiPersonalChest.java +++ b/src/main/java/mekanism/client/gui/GuiPersonalChest.java @@ -19,7 +19,7 @@ public class GuiPersonalChest extends GuiMekanism public GuiPersonalChest(InventoryPlayer inventory, TileEntityPersonalChest tentity) { - super(tentity, new ContainerPersonalChest(inventory, tentity, null, true)); + super(tentity, new ContainerPersonalChest(inventory, tentity, null, true, -1)); xSize+=26; ySize+=64; @@ -28,9 +28,9 @@ public GuiPersonalChest(InventoryPlayer inventory, TileEntityPersonalChest tenti guiElements.add(new GuiSecurityTab(this, tileEntity, MekanismUtils.getResource(ResourceType.GUI, "GuiPersonalChest.png"))); } - public GuiPersonalChest(InventoryPlayer inventory, IInventory inv) + public GuiPersonalChest(InventoryPlayer inventory, IInventory inv, int hotbarSlot) { - super(new ContainerPersonalChest(inventory, null, inv, false)); + super(new ContainerPersonalChest(inventory, null, inv, false, hotbarSlot)); xSize+=26; ySize+=64; diff --git a/src/main/java/mekanism/common/inventory/container/ContainerPersonalChest.java b/src/main/java/mekanism/common/inventory/container/ContainerPersonalChest.java index 6bf6161d622..fce085d4f0c 100644 --- a/src/main/java/mekanism/common/inventory/container/ContainerPersonalChest.java +++ b/src/main/java/mekanism/common/inventory/container/ContainerPersonalChest.java @@ -17,12 +17,14 @@ public class ContainerPersonalChest extends Container private TileEntityPersonalChest tileEntity; private IInventory itemInventory; private boolean isBlock; + private int slot; - public ContainerPersonalChest(InventoryPlayer inventory, TileEntityPersonalChest tentity, IInventory inv, boolean b) + public ContainerPersonalChest(InventoryPlayer inventory, TileEntityPersonalChest tentity, IInventory inv, boolean b, int i) { tileEntity = tentity; itemInventory = inv; isBlock = b; + slot = i; if(isBlock) { @@ -56,7 +58,6 @@ public ContainerPersonalChest(InventoryPlayer inventory, TileEntityPersonalChest addSlotToContainer(new SlotPersonalChest(inventory, slotX, 8 + slotX * 18, 206)); } } - public IInventory getInv() { if(isBlock) @@ -86,12 +87,11 @@ public void onContainerClosed(EntityPlayer entityplayer) @Override public boolean canInteractWith(EntityPlayer entityplayer) { - if(isBlock) - { + if(isBlock) { return tileEntity.isUseableByPlayer(entityplayer); + } else { + return slot == entityplayer.inventory.currentItem; } - - return true; } @Override diff --git a/src/main/java/mekanism/common/network/PacketPersonalChest.java b/src/main/java/mekanism/common/network/PacketPersonalChest.java index e75b01862d0..408f63cb2d9 100644 --- a/src/main/java/mekanism/common/network/PacketPersonalChest.java +++ b/src/main/java/mekanism/common/network/PacketPersonalChest.java @@ -19,10 +19,10 @@ public class PacketPersonalChest implements IMessageHandler { @Override - public IMessage onMessage(PersonalChestMessage message, MessageContext context) + public IMessage onMessage(PersonalChestMessage message, MessageContext context) { EntityPlayer player = PacketHandler.getPlayer(context); - + if(message.packetType == PersonalChestPacketType.SERVER_OPEN) { try { @@ -58,65 +58,68 @@ else if(message.packetType == PersonalChestPacketType.CLIENT_OPEN) e.printStackTrace(); } } - + return null; } - + public static class PersonalChestMessage implements IMessage { public PersonalChestPacketType packetType; - + public boolean isBlock; - + public int guiType; public int windowId; - + public Coord4D coord4D; - + public int hotbarSlot; + public PersonalChestMessage() {} - + //This is a really messy implementation... - public PersonalChestMessage(PersonalChestPacketType type, boolean b1, int i1, int i2, Coord4D c1) + public PersonalChestMessage(PersonalChestPacketType type, boolean b1, int i1, int i2, Coord4D c1, int slot) { packetType = type; - - switch(packetType) + + switch (packetType) { case CLIENT_OPEN: guiType = i1; windowId = i2; isBlock = b1; - - if(isBlock) - { + + if (isBlock) { coord4D = c1; + } else { + hotbarSlot = slot; } - + break; case SERVER_OPEN: isBlock = b1; - - if(isBlock) - { + + if (isBlock) { coord4D = c1; + } else { + hotbarSlot = slot; } - + break; } } - + @Override public void toBytes(ByteBuf dataStream) { dataStream.writeInt(packetType.ordinal()); - + switch(packetType) { case CLIENT_OPEN: dataStream.writeInt(guiType); dataStream.writeInt(windowId); dataStream.writeBoolean(isBlock); - + if(isBlock) { dataStream.writeInt(coord4D.xCoord); @@ -124,11 +127,11 @@ public void toBytes(ByteBuf dataStream) dataStream.writeInt(coord4D.zCoord); dataStream.writeInt(coord4D.dimensionId); } - + break; case SERVER_OPEN: dataStream.writeBoolean(isBlock); - + if(isBlock) { dataStream.writeInt(coord4D.xCoord); @@ -136,16 +139,16 @@ public void toBytes(ByteBuf dataStream) dataStream.writeInt(coord4D.zCoord); dataStream.writeInt(coord4D.dimensionId); } - + break; } } - + @Override public void fromBytes(ByteBuf dataStream) { packetType = PersonalChestPacketType.values()[dataStream.readInt()]; - + if(packetType == PersonalChestPacketType.SERVER_OPEN) { isBlock = dataStream.readBoolean(); @@ -168,7 +171,7 @@ else if(packetType == PersonalChestPacketType.CLIENT_OPEN) } } } - + public static enum PersonalChestPacketType { CLIENT_OPEN, diff --git a/src/main/java/mekanism/common/util/MekanismUtils.java b/src/main/java/mekanism/common/util/MekanismUtils.java index f7996af3ceb..b9e076dfcc8 100644 --- a/src/main/java/mekanism/common/util/MekanismUtils.java +++ b/src/main/java/mekanism/common/util/MekanismUtils.java @@ -941,23 +941,24 @@ else if(fluid == FluidRegistry.LAVA) * @param player - player to open GUI * @param tileEntity - TileEntity of the chest, if it's not an item * @param inventory - IInventory of the item, if it's not a block - * @param isBlock - whether or not this electric chest is in it's block form + * @param isBlock - whether this electric chest is in itss block form */ public static void openPersonalChestGui(EntityPlayerMP player, TileEntityPersonalChest tileEntity, IInventory inventory, boolean isBlock) { player.getNextWindowId(); player.closeContainer(); int id = player.currentWindowId; + int hotbarSlot = player.inventory.currentItem; if(isBlock) { - Mekanism.packetHandler.sendTo(new PersonalChestMessage(PersonalChestPacketType.CLIENT_OPEN, true, 0, id, Coord4D.get(tileEntity)), player); + Mekanism.packetHandler.sendTo(new PersonalChestMessage(PersonalChestPacketType.CLIENT_OPEN, true, 0, id, Coord4D.get(tileEntity), -1), player); } else { - Mekanism.packetHandler.sendTo(new PersonalChestMessage(PersonalChestPacketType.CLIENT_OPEN, false, 0, id, null), player); + Mekanism.packetHandler.sendTo(new PersonalChestMessage(PersonalChestPacketType.CLIENT_OPEN, false, 0, id, null, hotbarSlot), player); } - player.openContainer = new ContainerPersonalChest(player.inventory, tileEntity, inventory, isBlock); + player.openContainer = new ContainerPersonalChest(player.inventory, tileEntity, inventory, isBlock, hotbarSlot); player.openContainer.windowId = id; player.openContainer.addCraftingToCrafters(player); } From 12d5ad345a1c13181da58f5071b0feb5a5a365b5 Mon Sep 17 00:00:00 2001 From: DrParadox7 Date: Mon, 25 Sep 2023 22:08:28 +0200 Subject: [PATCH 3/3] Account for mods that can shuffle inventories Also fix NPE --- .../inventory/container/ContainerPersonalChest.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/mekanism/common/inventory/container/ContainerPersonalChest.java b/src/main/java/mekanism/common/inventory/container/ContainerPersonalChest.java index fce085d4f0c..91abc016deb 100644 --- a/src/main/java/mekanism/common/inventory/container/ContainerPersonalChest.java +++ b/src/main/java/mekanism/common/inventory/container/ContainerPersonalChest.java @@ -2,6 +2,7 @@ import invtweaks.api.container.ChestContainer; import mekanism.common.block.BlockMachine.MachineType; +import mekanism.common.inventory.InventoryPersonalChest; import mekanism.common.inventory.slot.SlotPersonalChest; import mekanism.common.tile.TileEntityPersonalChest; import net.minecraft.entity.player.EntityPlayer; @@ -90,8 +91,15 @@ public boolean canInteractWith(EntityPlayer entityplayer) if(isBlock) { return tileEntity.isUseableByPlayer(entityplayer); } else { - return slot == entityplayer.inventory.currentItem; + if (slot == entityplayer.inventory.currentItem && itemInventory instanceof InventoryPersonalChest) { + ItemStack currentHeldItem = entityplayer.getHeldItem(); + ItemStack stack = ((InventoryPersonalChest) itemInventory).getStack(); + + if (stack != null) + return stack == currentHeldItem; + } } + return false; } @Override @@ -143,7 +151,7 @@ public ItemStack slotClick(int slotNumber, int destSlot, int modifier, EntityPla { ItemStack itemStack = player.inventory.getStackInSlot(destSlot); - if(MachineType.get(itemStack) == MachineType.PERSONAL_CHEST) + if(itemStack != null && MachineType.get(itemStack) == MachineType.PERSONAL_CHEST) { return null; }