diff --git a/src/main/java/baubles/common/Baubles.java b/src/main/java/baubles/common/Baubles.java index 1f54192..776679f 100644 --- a/src/main/java/baubles/common/Baubles.java +++ b/src/main/java/baubles/common/Baubles.java @@ -3,8 +3,10 @@ package baubles.common; import java.io.File; import baubles.Tags; +import baubles.common.lib.PlayerHandler; import codechicken.nei.api.API; import cpw.mods.fml.common.Loader; +import cpw.mods.fml.common.event.FMLServerStoppedEvent; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -75,4 +77,9 @@ public class Baubles { } } + @Mod.EventHandler + public void onServerStopped(FMLServerStoppedEvent event) { + PlayerHandler.clearServerPlayerBaubles(); + } + } diff --git a/src/main/java/baubles/common/container/InventoryBaubles.java b/src/main/java/baubles/common/container/InventoryBaubles.java index 780d9a7..7ee6c56 100644 --- a/src/main/java/baubles/common/container/InventoryBaubles.java +++ b/src/main/java/baubles/common/container/InventoryBaubles.java @@ -295,7 +295,7 @@ public class InventoryBaubles implements IInventory { public void syncSlotToClients(int slot) { try { - EntityPlayer entityPlayer = player.get(); + final EntityPlayer entityPlayer = this.player.get(); if (entityPlayer instanceof EntityPlayerMP playerMP && !entityPlayer.worldObj.isRemote) { PacketHandler.sendToTracking(new PacketSyncBauble(playerMP, slot), playerMP, true); } diff --git a/src/main/java/baubles/common/event/EventHandlerEntity.java b/src/main/java/baubles/common/event/EventHandlerEntity.java index 1e1876b..84e46d2 100644 --- a/src/main/java/baubles/common/event/EventHandlerEntity.java +++ b/src/main/java/baubles/common/event/EventHandlerEntity.java @@ -16,6 +16,7 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraftforge.event.entity.player.PlayerDropsEvent; import net.minecraftforge.event.entity.player.PlayerEvent; +import net.minecraftforge.event.world.WorldEvent; public class EventHandlerEntity { @@ -102,6 +103,11 @@ public class EventHandlerEntity { getPlayerFile("baubback", directory, player.getCommandSenderName())); } + @SubscribeEvent + public void onWorldUnload(WorldEvent.Unload event) { + PlayerHandler.onWorldUnload(event.world); + } + @SubscribeEvent public void startTracking(PlayerEvent.StartTracking event) { // no need to check for isRemote: this event is server only diff --git a/src/main/java/baubles/common/event/EventHandlerNetwork.java b/src/main/java/baubles/common/event/EventHandlerNetwork.java index 82937d3..4d604f5 100644 --- a/src/main/java/baubles/common/event/EventHandlerNetwork.java +++ b/src/main/java/baubles/common/event/EventHandlerNetwork.java @@ -12,6 +12,7 @@ import cpw.mods.fml.common.network.FMLNetworkEvent; import cpw.mods.fml.relauncher.Side; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; +import baubles.common.network.PacketSyncBauble; import net.minecraft.item.ItemStack; public class EventHandlerNetwork { @@ -33,6 +34,13 @@ public class EventHandlerNetwork { } } + @SubscribeEvent + public void playerChangedDim(PlayerEvent.PlayerChangedDimensionEvent event) { + if (event.player instanceof EntityPlayerMP playerMP) { + PacketHandler.INSTANCE.sendTo(new PacketSyncAllBauble(playerMP), playerMP); + } + } + @SubscribeEvent public void onConnectedToServer(FMLNetworkEvent.ClientConnectedToServerEvent event) { // clear old cache @@ -41,10 +49,9 @@ public class EventHandlerNetwork { public static void syncBaubles(EntityPlayer player) { InventoryBaubles baubles = PlayerHandler.getPlayerBaubles(player); - for (int i = 0; i < baubles.getSizeInventory(); i++) { - baubles.syncSlotToClients(i); - } - } - + for (int i = 0; i < baubles.getSizeInventory(); i++) { + baubles.syncSlotToClients(i); + } + } } diff --git a/src/main/java/baubles/common/lib/PlayerHandler.java b/src/main/java/baubles/common/lib/PlayerHandler.java index 420bef8..bff378d 100644 --- a/src/main/java/baubles/common/lib/PlayerHandler.java +++ b/src/main/java/baubles/common/lib/PlayerHandler.java @@ -14,6 +14,7 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.nbt.CompressedStreamTools; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.server.MinecraftServer; +import net.minecraft.world.World; public class PlayerHandler { @@ -68,6 +69,19 @@ public class PlayerHandler { map.remove(playerName); } + public static void clearServerPlayerBaubles() { + playerBaublesServer.clear(); + } + + public static void onWorldUnload(World world) { + if (world.isRemote) { + playerBaublesClient.values().removeIf(inv -> { + final EntityPlayer player = inv.player.get(); + return player == null || player.worldObj == world; + }); + } + } + public static InventoryBaubles getPlayerBaubles(EntityPlayer player) { if (player.worldObj.isRemote) { return playerBaublesClient.computeIfAbsent(player.getCommandSenderName(), username -> new InventoryBaubles(player));