Handle Baubles inv more careful im MP

This commit is contained in:
HenryLoenwind committed 2016-01-27 23:10:54 +01:00
1 parent ff423ae92a
commit 7b7ca9ac6a
3 files changed
+50 -6

No files matched your search

@@ -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();
}
}
}
@@ -23,7 +23,16 @@ public class ContainerCapBank extends ContainerEnder<TileCapBank> {
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<TileCapBank> {
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_);
}
}
}
@@ -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;
}
}
}
}