diff --git a/src/main/java/baubles/api/IBauble.java b/src/main/java/baubles/api/IBauble.java index 0e6e7c4..5b9cbb4 100644 --- a/src/main/java/baubles/api/IBauble.java +++ b/src/main/java/baubles/api/IBauble.java @@ -42,4 +42,9 @@ public interface IBauble { * Can this bauble be removed from a bauble slot */ boolean canUnequip(ItemStack itemstack, EntityLivingBase player); + + /** + * This method is called when the player is loaded + */ + default void onPlayerLoad(ItemStack itemstack, EntityLivingBase player) {} } diff --git a/src/main/java/baubles/common/container/ContainerPlayerExpanded.java b/src/main/java/baubles/common/container/ContainerPlayerExpanded.java index 0b2099c..89f3a38 100644 --- a/src/main/java/baubles/common/container/ContainerPlayerExpanded.java +++ b/src/main/java/baubles/common/container/ContainerPlayerExpanded.java @@ -35,11 +35,8 @@ public class ContainerPlayerExpanded extends Container { public ContainerPlayerExpanded(InventoryPlayer playerInv, boolean isClient, EntityPlayer player) { this.thePlayer = player; - baubles = new InventoryBaubles(player); + baubles = PlayerHandler.getPlayerBaubles(player); baubles.setEventHandler(this); - if (isClient) { - baubles.stackList = PlayerHandler.getPlayerBaubles(player).stackList; - } int i; int j; diff --git a/src/main/java/baubles/common/container/InventoryBaubles.java b/src/main/java/baubles/common/container/InventoryBaubles.java index 34a12fe..d5f47d3 100644 --- a/src/main/java/baubles/common/container/InventoryBaubles.java +++ b/src/main/java/baubles/common/container/InventoryBaubles.java @@ -295,7 +295,8 @@ public class InventoryBaubles implements IInventory { public void syncSlotToClients(int slot) { try { - if (Baubles.proxy.getClientWorld() == null) { + EntityPlayer entityPlayer = player.get(); + if (entityPlayer != null && !entityPlayer.worldObj.isRemote) { PacketHandler.INSTANCE.sendToAll(new PacketSyncBauble(player.get(), slot)); } } catch (Exception e) { diff --git a/src/main/java/baubles/common/event/EventHandlerEntity.java b/src/main/java/baubles/common/event/EventHandlerEntity.java index cd40359..f94e5e0 100644 --- a/src/main/java/baubles/common/event/EventHandlerEntity.java +++ b/src/main/java/baubles/common/event/EventHandlerEntity.java @@ -81,7 +81,6 @@ public class EventHandlerEntity { } PlayerHandler.loadPlayerBaubles(player, mainFile, backupFile); - EventHandlerNetwork.syncBaubles(player); } public File getPlayerFile(String extension, File playerDirectory, String playerName) { diff --git a/src/main/java/baubles/common/event/EventHandlerNetwork.java b/src/main/java/baubles/common/event/EventHandlerNetwork.java index d4f7f11..6a76306 100644 --- a/src/main/java/baubles/common/event/EventHandlerNetwork.java +++ b/src/main/java/baubles/common/event/EventHandlerNetwork.java @@ -1,20 +1,36 @@ package baubles.common.event; +import baubles.api.BaublesApi; +import baubles.api.IBauble; +import baubles.common.Baubles; import baubles.common.container.InventoryBaubles; import baubles.common.lib.PlayerHandler; +import baubles.common.network.PacketHandler; +import baubles.common.network.PacketSyncBauble; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.eventhandler.SubscribeEvent; import cpw.mods.fml.common.gameevent.PlayerEvent; import cpw.mods.fml.relauncher.Side; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.inventory.IInventory; +import net.minecraft.item.ItemStack; public class EventHandlerNetwork { @SubscribeEvent - public void playerLoggedInEvent (PlayerEvent.PlayerLoggedInEvent event) { + public void playerLoggedInEvent(PlayerEvent.PlayerLoggedInEvent event) { Side side = FMLCommonHandler.instance().getEffectiveSide(); if (side == Side.SERVER) { - syncBaubles(event.player); + // Apply all baubles + InventoryBaubles baubles = PlayerHandler.getPlayerBaubles(event.player); + for (int i = 0; i < baubles.getSizeInventory(); i++) { + ItemStack stack = baubles.getStackInSlot(i); + PacketHandler.INSTANCE.sendToAll(new PacketSyncBauble(event.player, i, true)); + if (stack != null && stack.getItem() instanceof IBauble itemBauble) { + itemBauble.onPlayerLoad(stack, event.player); + } + } + } } diff --git a/src/main/java/baubles/common/lib/PlayerHandler.java b/src/main/java/baubles/common/lib/PlayerHandler.java index b65f4d3..72c7f11 100644 --- a/src/main/java/baubles/common/lib/PlayerHandler.java +++ b/src/main/java/baubles/common/lib/PlayerHandler.java @@ -15,22 +15,31 @@ import net.minecraft.nbt.NBTTagCompound; public class PlayerHandler { - private static HashMap playerBaubles = new HashMap<>(); + private static HashMap playerBaublesServer = new HashMap<>(); + private static HashMap playerBaublesClient = new HashMap<>(); public static void clearPlayerBaubles(EntityPlayer player) { - playerBaubles.remove(player.getCommandSenderName()); + playerBaublesServer.remove(player.getCommandSenderName()); + } + + public static void clearClientPlayerBaubles() { + playerBaublesClient.clear(); } public static InventoryBaubles getPlayerBaubles(EntityPlayer player) { - if (!playerBaubles.containsKey(player.getCommandSenderName())) { - InventoryBaubles inventory = new InventoryBaubles(player); - playerBaubles.put(player.getCommandSenderName(), inventory); + if (player.worldObj.isRemote) { + return playerBaublesClient.computeIfAbsent(player.getCommandSenderName(), username -> new InventoryBaubles(player)); + } else { + return playerBaublesServer.computeIfAbsent(player.getCommandSenderName(), username -> new InventoryBaubles(player)); } - return playerBaubles.get(player.getCommandSenderName()); } public static void setPlayerBaubles(EntityPlayer player, InventoryBaubles inventory) { - playerBaubles.put(player.getCommandSenderName(), inventory); + if (player.worldObj.isRemote) { + playerBaublesClient.put(player.getCommandSenderName(), inventory); + } else { + playerBaublesServer.put(player.getCommandSenderName(), inventory); + } } public static void loadPlayerBaubles(EntityPlayer player, File mainFile, File backupFile) { @@ -68,7 +77,7 @@ public class PlayerHandler { if(data != null) { InventoryBaubles inventory = new InventoryBaubles(player); inventory.readNBT(data); - playerBaubles.put(player.getCommandSenderName(), inventory); + playerBaublesServer.put(player.getCommandSenderName(), inventory); if(save) { savePlayerBaubles(player, mainFile, backupFile); } diff --git a/src/main/java/baubles/common/network/PacketSyncBauble.java b/src/main/java/baubles/common/network/PacketSyncBauble.java index 2df1112..fab4558 100644 --- a/src/main/java/baubles/common/network/PacketSyncBauble.java +++ b/src/main/java/baubles/common/network/PacketSyncBauble.java @@ -2,7 +2,9 @@ package baubles.common.network; import java.io.IOException; +import baubles.api.IBauble; import baubles.common.Baubles; +import baubles.common.container.InventoryBaubles; import baubles.common.lib.PlayerHandler; import cpw.mods.fml.common.network.simpleimpl.IMessage; import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; @@ -19,19 +21,26 @@ public class PacketSyncBauble implements IMessage, IMessageHandler