diff --git a/src/main/java/crazypants/enderio/item/ItemMagnet.java b/src/main/java/crazypants/enderio/item/ItemMagnet.java index 1a1e6398..1b2cccef 100644 --- a/src/main/java/crazypants/enderio/item/ItemMagnet.java +++ b/src/main/java/crazypants/enderio/item/ItemMagnet.java @@ -195,11 +195,7 @@ public class ItemMagnet extends ItemEnergyContainer implements IResourceTooltipP ItemMagnet.drainPerSecondPower(itemstack); IInventory baubles = BaublesUtil.instance().getBaubles((EntityPlayer) player); if(baubles != null) { - for (int i = 0; i < baubles.getSizeInventory(); i++) { - if(baubles.getStackInSlot(i) == itemstack) { - baubles.setInventorySlotContents(i, itemstack); - } - } + baubles.markDirty(); } } } diff --git a/src/main/java/crazypants/enderio/machine/capbank/ContainerCapBank.java b/src/main/java/crazypants/enderio/machine/capbank/ContainerCapBank.java index d74a7bf3..8cbba552 100644 --- a/src/main/java/crazypants/enderio/machine/capbank/ContainerCapBank.java +++ b/src/main/java/crazypants/enderio/machine/capbank/ContainerCapBank.java @@ -23,7 +23,16 @@ public class ContainerCapBank extends ContainerEnder { private InventoryImpl inv; // Note: Modifying the Baubles inventory on the client side of an integrated - // server is a bad idea as Baubles does some very bad things... + // server is a bad idea because Baubles actually shares the object between + // server and client. However, by not doing so, we prevent the callback hooks + // to be called in the client thread. It doesn't seem to matter now, but if we + // ever get reports of problems, we need to find a solution to synthesize + // these calls. + // In a multiplayer client this works well, but we need to prevent Baubles + // from calling those callbacks when the server syncs the slot contents to the + // client (packet 30) initially. They are still called every time the server + // updates the slots (which is wrong), but the original Baubles inventory + // works this way, so... private IInventory baubles; public ContainerCapBank(InventoryPlayer playerInv, TileCapBank cb) { @@ -207,4 +216,24 @@ public class ContainerCapBank extends ContainerEnder { return inventory.isItemValidForSlot(getSlotIndex(), itemStack); } } + + /** + * called when the content of slots is synced from the server to the client + * (packet 30) + */ + @Override + @SideOnly(Side.CLIENT) + public void putStacksInSlots(ItemStack[] p_75131_1_) { + if (hasBaublesSlots() && BaublesUtil.WhoAmI.whoAmI(getInv().getWorldObj()) == BaublesUtil.WhoAmI.MPCLIENT) { + try { + BaublesUtil.instance().disableCallbacks(baubles, true); + super.putStacksInSlots(p_75131_1_); + } finally { + BaublesUtil.instance().disableCallbacks(baubles, false); + } + } else { + super.putStacksInSlots(p_75131_1_); + } + } + } diff --git a/src/main/java/crazypants/util/BaublesUtil.java b/src/main/java/crazypants/util/BaublesUtil.java index 1e66dc95..ebc7135f 100644 --- a/src/main/java/crazypants/util/BaublesUtil.java +++ b/src/main/java/crazypants/util/BaublesUtil.java @@ -1,6 +1,9 @@ package crazypants.util; +import java.lang.reflect.Field; + import net.minecraft.client.Minecraft; +import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.server.MinecraftServer; @@ -8,7 +11,9 @@ import net.minecraft.world.World; import baubles.api.BaublesApi; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.Loader; +import cpw.mods.fml.relauncher.ReflectionHelper; import cpw.mods.fml.relauncher.Side; +import crazypants.enderio.Log; public class BaublesUtil { @@ -68,4 +73,18 @@ public class BaublesUtil { return BaublesApi.getBaubles(player); } + private static boolean failedDirectAccess = false; + public void disableCallbacks(IInventory baubles, boolean b) { + if (!failedDirectAccess) { + try { + Class inventoryBaubles = Class.forName("baubles.common.container.InventoryBaubles"); + Field blockEvents = inventoryBaubles.getDeclaredField("blockEvents"); + blockEvents.set(baubles, b); + } catch (Throwable t) { + Log.info("Failed to access Baubles internals: " + t); + failedDirectAccess = true; + } + } + } + }