Fix baubles not being applied on login (#14)

This commit is contained in:
DarkShadow44 authored and GitHub committed 2025-09-19 06:56:27 +02:00
1 parent 0efe4d7541
commit fc27584c13
7 files changed
+66 -17

No files matched your search

+5
View File
@@ -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) {}
}
@@ -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;
@@ -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) {
@@ -81,7 +81,6 @@ public class EventHandlerEntity {
}
PlayerHandler.loadPlayerBaubles(player, mainFile, backupFile);
EventHandlerNetwork.syncBaubles(player);
}
public File getPlayerFile(String extension, File playerDirectory, String playerName) {
@@ -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);
}
}
}
}
@@ -15,22 +15,31 @@ import net.minecraft.nbt.NBTTagCompound;
public class PlayerHandler {
private static HashMap<String, InventoryBaubles> playerBaubles = new HashMap<>();
private static HashMap<String, InventoryBaubles> playerBaublesServer = new HashMap<>();
private static HashMap<String, InventoryBaubles> 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);
}
@@ -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<PacketSyncBau
int slot;
int playerId;
ItemStack bauble = null;
boolean initial;
public PacketSyncBauble() {}
public PacketSyncBauble(EntityPlayer player, int slot) {
this(player, slot, false);
}
public PacketSyncBauble(EntityPlayer player, int slot, boolean reset) {
this.slot = slot;
this.bauble = PlayerHandler.getPlayerBaubles(player).getStackInSlot(slot);
this.playerId = player.getEntityId();
this.initial = reset;
}
@Override
public void toBytes(ByteBuf buffer) {
buffer.writeByte(slot);
buffer.writeInt(playerId);
buffer.writeBoolean(initial);
PacketBuffer pb = new PacketBuffer(buffer);
try { pb.writeItemStackToBuffer(bauble); } catch (IOException ignored) {}
}
@@ -40,6 +49,7 @@ public class PacketSyncBauble implements IMessage, IMessageHandler<PacketSyncBau
public void fromBytes(ByteBuf buffer) {
slot = buffer.readByte();
playerId = buffer.readInt();
initial = buffer.readBoolean();
PacketBuffer pb = new PacketBuffer(buffer);
try { bauble = pb.readItemStackFromBuffer(); } catch (IOException ignored) {}
}
@@ -50,7 +60,19 @@ public class PacketSyncBauble implements IMessage, IMessageHandler<PacketSyncBau
if (world == null) return null;
Entity e = world.getEntityByID(message.playerId);
if (e instanceof EntityPlayer player) {
PlayerHandler.getPlayerBaubles(player).stackList[message.slot]=message.bauble;
InventoryBaubles baubles = PlayerHandler.getPlayerBaubles(player);
if (message.initial) {
if (message.slot == 0) {
PlayerHandler.clearClientPlayerBaubles();
}
baubles.stackList[message.slot] = message.bauble;
if (message.bauble != null && message.bauble.getItem() instanceof IBauble itemBauble) {
itemBauble.onPlayerLoad(message.bauble, player);
}
}
else {
baubles.setInventorySlotContents(message.slot, message.bauble);
}
}
return null;
}