From 7be75838981ef81900e95ecd7bfa63f77acc9bea Mon Sep 17 00:00:00 2001 From: Barteks2x Date: Wed, 30 Oct 2019 11:06:19 +0100 Subject: [PATCH 1/2] Fix check for multiple tile/entity items, handle switching dimensions correctly. Fixes #201. --- .../carryon/common/event/ItemEvents.java | 46 ++++++++++--------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/src/main/java/tschipp/carryon/common/event/ItemEvents.java b/src/main/java/tschipp/carryon/common/event/ItemEvents.java index 45dce68..cc33464 100644 --- a/src/main/java/tschipp/carryon/common/event/ItemEvents.java +++ b/src/main/java/tschipp/carryon/common/event/ItemEvents.java @@ -1,7 +1,9 @@ package tschipp.carryon.common.event; +import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; @@ -13,11 +15,13 @@ import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.init.Blocks; import net.minecraft.inventory.IInventory; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.network.play.server.SPacketHeldItemChange; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; @@ -434,7 +438,7 @@ public class ItemEvents boolean keepInv = rules.getBoolean("keepInventory"); boolean wasCarrying = player.inventory.hasItemStack(new ItemStack(RegistrationHandler.itemTile)) || player.inventory.hasItemStack(new ItemStack(RegistrationHandler.itemEntity)); - if ((wasDead ? keepInv : true) && wasCarrying) + if (wasDead && keepInv && wasCarrying) { int carrySlot = original.inventory.currentItem; @@ -462,29 +466,30 @@ public class ItemEvents if (!entity.world.isRemote) { - boolean hasCarried = player.inventory.hasItemStack(new ItemStack(RegistrationHandler.itemTile)) || player.inventory.hasItemStack(new ItemStack(RegistrationHandler.itemEntity)); + List itemSlots = new ArrayList<>(); + getSlots(player, RegistrationHandler.itemTile, itemSlots); + getSlots(player, RegistrationHandler.itemEntity, itemSlots); ItemStack inHand = player.getHeldItemMainhand(); - if (hasCarried) + if (itemSlots.size() > 1 || (itemSlots.size() > 0 + && (inHand.getItem() != RegistrationHandler.itemTile && inHand.getItem() != RegistrationHandler.itemEntity))) { - if ((inHand.getItem() != RegistrationHandler.itemTile && inHand.getItem() != RegistrationHandler.itemEntity) && player.getPortalCooldown() == 0) + // if there is only one item, and it's in the hotbar, just force the selection there + // this is necessary, because when switching dimensions, Minecraft will reset selection to 0 for one tick + // and completely reset it to 0 when returning from the end + // if there are multiple, drop all of them, as it's likely a player trying to exploit a bug + if (itemSlots.size() == 1 && InventoryPlayer.isHotbar(itemSlots.get(0))) { - int slotBlock = getSlot(player, RegistrationHandler.itemTile); - int slotEntity = getSlot(player, RegistrationHandler.itemEntity); - - EntityItem item = null; - if (slotBlock != -1) + player.inventory.currentItem = itemSlots.get(0); + ((EntityPlayerMP) player).connection.sendPacket(new SPacketHeldItemChange(player.inventory.currentItem)); + return; + } + else + { + for (int slotBlock : itemSlots) { ItemStack dropped = player.inventory.removeStackFromSlot(slotBlock); - item = new EntityItem(player.world, player.posX, player.posY, player.posZ, dropped); - } - if (slotEntity != -1) - { - ItemStack dropped = player.inventory.removeStackFromSlot(slotEntity); - item = new EntityItem(player.world, player.posX, player.posY, player.posZ, dropped); - } - if (item != null) - { + EntityItem item = new EntityItem(player.world, player.posX, player.posY, player.posZ, dropped); player.world.spawnEntity(item); sendPacket(player, 9, 0); } @@ -514,15 +519,14 @@ public class ItemEvents } } - public int getSlot(EntityPlayer player, Item item) + public void getSlots(EntityPlayer player, Item item, List outputList) { for (int i = 0; i < player.inventory.getSizeInventory(); i++) { ItemStack stack = player.inventory.getStackInSlot(i); if (stack.getItem() == item) - return i; + outputList.add(i); } - return -1; } public static void sendPacket(EntityPlayer player, int currentItem, int hash) From c82c0d97a2428fcf1413845bd7f66fdfc19a1ade Mon Sep 17 00:00:00 2001 From: Barteks2x Date: Wed, 30 Oct 2019 11:20:25 +0100 Subject: [PATCH 2/2] Bump version number, remove early return --- src/main/java/tschipp/carryon/CarryOn.java | 2 +- src/main/java/tschipp/carryon/common/event/ItemEvents.java | 1 - src/main/resources/mcmod.info | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/tschipp/carryon/CarryOn.java b/src/main/java/tschipp/carryon/CarryOn.java index 7117121..5e2be56 100644 --- a/src/main/java/tschipp/carryon/CarryOn.java +++ b/src/main/java/tschipp/carryon/CarryOn.java @@ -38,7 +38,7 @@ public class CarryOn { public static CarryOn instance; public static final String MODID = "carryon"; - public static final String VERSION = "1.12.2"; + public static final String VERSION = "1.12.3"; public static final String NAME = "Carry On"; public static final String ACCEPTED_VERSIONS = "[1.12.2,1.13)"; public static final String UPDATE_JSON = "https://gist.githubusercontent.com/Tschipp/dccadee7c90d7a34e6e76a35d9d6fa2e/raw/"; diff --git a/src/main/java/tschipp/carryon/common/event/ItemEvents.java b/src/main/java/tschipp/carryon/common/event/ItemEvents.java index cc33464..f0159f2 100644 --- a/src/main/java/tschipp/carryon/common/event/ItemEvents.java +++ b/src/main/java/tschipp/carryon/common/event/ItemEvents.java @@ -482,7 +482,6 @@ public class ItemEvents { player.inventory.currentItem = itemSlots.get(0); ((EntityPlayerMP) player).connection.sendPacket(new SPacketHeldItemChange(player.inventory.currentItem)); - return; } else { diff --git a/src/main/resources/mcmod.info b/src/main/resources/mcmod.info index 1aae2e7..e9952ae 100644 --- a/src/main/resources/mcmod.info +++ b/src/main/resources/mcmod.info @@ -2,7 +2,7 @@ { "modid" : "carryon", "name" : "Carry On", - "version" : "1.12.2", "mcversion" : "1.12.2", + "version" : "1.12.3", "mcversion" : "1.12.2", "url" : "", "credits" : "Tschipp, Purplicious_Cow, cy4n", "authorList" : ["Tschipp, Purplicious_Cow, cy4n"],