Merge remote-tracking branch 'origin/fix/sync' into dev

This commit is contained in:
Martin Robertz committed 2025-12-20 12:24:49 +01:00
commit a4c6d2402e
6 files changed
+102 -27

No files matched your search

@@ -7,12 +7,14 @@ import baubles.api.BaubleType;
import baubles.common.BaublesConfig;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.LoaderState;
import cpw.mods.fml.common.network.ByteBufUtils;
public class BaubleExpandedSlots {
//The total number of slots that can be added.
//Please keep in mind that increases to this limit are possible with minimal changes.
public static final int slotLimit = 20;
public static final int maxSlotIdBytes = ByteBufUtils.varIntByteCount(slotLimit);
//The type used in place of null.
public static final String invalidType = "";
@@ -1,19 +1,19 @@
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 baubles.common.network.PacketSyncAllBauble;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.PlayerEvent;
import cpw.mods.fml.common.network.FMLNetworkEvent;
import cpw.mods.fml.relauncher.Side;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.server.MinecraftServer;
public class EventHandlerNetwork {
@@ -23,9 +23,14 @@ public class EventHandlerNetwork {
if (side == Side.SERVER) {
// Apply all baubles
InventoryBaubles baubles = PlayerHandler.getPlayerBaubles(event.player);
PacketHandler.INSTANCE.sendToAll(new PacketSyncAllBauble(event.player));
for (Object o : MinecraftServer.getServer().getConfigurationManager().playerEntityList) {
if (o == event.player) continue;
EntityPlayerMP player = (EntityPlayerMP) o;
PacketHandler.INSTANCE.sendTo(new PacketSyncAllBauble(player), (EntityPlayerMP) 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);
}
@@ -34,6 +39,12 @@ public class EventHandlerNetwork {
}
}
@SubscribeEvent
public void onConnectedToServer(FMLNetworkEvent.ClientConnectedToServerEvent event) {
// clear old cache
PlayerHandler.clearClientPlayerBaubles();
}
public static void syncBaubles(EntityPlayer player) {
InventoryBaubles baubles = PlayerHandler.getPlayerBaubles(player);
for (int i = 0; i < baubles.getSizeInventory(); i++) {
@@ -26,6 +26,10 @@ public class PlayerHandler {
playerBaublesClient.clear();
}
public static void clearClientPlayerBaubles(EntityPlayer player) {
playerBaublesClient.remove(player.getCommandSenderName());
}
public static InventoryBaubles getPlayerBaubles(EntityPlayer player) {
if (player.worldObj.isRemote) {
return playerBaublesClient.computeIfAbsent(player.getCommandSenderName(), username -> new InventoryBaubles(player));
@@ -12,5 +12,6 @@ public class PacketHandler {
INSTANCE.registerMessage(PacketOpenBaublesInventory.class, PacketOpenBaublesInventory.class, 0, Side.SERVER);
INSTANCE.registerMessage(PacketOpenNormalInventory.class, PacketOpenNormalInventory.class, 1, Side.SERVER);
INSTANCE.registerMessage(PacketSyncBauble.class, PacketSyncBauble.class, 2, Side.CLIENT);
INSTANCE.registerMessage(PacketSyncAllBauble.class, PacketSyncAllBauble.class, 3, Side.CLIENT);
}
}
@@ -0,0 +1,78 @@
package baubles.common.network;
import baubles.api.IBauble;
import baubles.api.expanded.BaubleExpandedSlots;
import baubles.common.Baubles;
import baubles.common.container.InventoryBaubles;
import baubles.common.lib.PlayerHandler;
import cpw.mods.fml.common.network.ByteBufUtils;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import gnu.trove.map.TIntObjectMap;
import gnu.trove.map.hash.TIntObjectHashMap;
import io.netty.buffer.ByteBuf;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
public class PacketSyncAllBauble implements IMessage, IMessageHandler<PacketSyncAllBauble, IMessage> {
private int playerId;
private TIntObjectMap<ItemStack> inventory = new TIntObjectHashMap<>();
public PacketSyncAllBauble() {}
public PacketSyncAllBauble(EntityPlayer player) {
this.playerId = player.getEntityId();
InventoryBaubles inv = PlayerHandler.getPlayerBaubles(player);
for (int i = 0; i < inv.getSizeInventory(); i++) {
ItemStack slotItem = inv.getStackInSlot(i);
if (slotItem == null) continue;
inventory.put(i, slotItem);
}
}
@Override
public void toBytes(ByteBuf buffer) {
ByteBufUtils.writeVarInt(buffer, playerId, 4);
ByteBufUtils.writeVarInt(buffer, inventory.size(), BaubleExpandedSlots.maxSlotIdBytes);
inventory.forEachEntry((a, b) -> {
ByteBufUtils.writeVarInt(buffer, a, BaubleExpandedSlots.maxSlotIdBytes);
ByteBufUtils.writeItemStack(buffer, b);
return true;
});
}
@Override
public void fromBytes(ByteBuf buffer) {
playerId = ByteBufUtils.readVarInt(buffer, 4);
int size = ByteBufUtils.readVarInt(buffer, BaubleExpandedSlots.maxSlotIdBytes);
for (int i = 0; i < size; i++) {
int slotId = ByteBufUtils.readVarInt(buffer, BaubleExpandedSlots.maxSlotIdBytes);
ItemStack slotItem = ByteBufUtils.readItemStack(buffer);
inventory.put(slotId, slotItem);
}
}
@Override
public IMessage onMessage(PacketSyncAllBauble message, MessageContext ctx) {
World world = Baubles.proxy.getClientWorld();
if (world == null) return null;
Entity e = world.getEntityByID(message.playerId);
if (e instanceof EntityPlayer player) {
PlayerHandler.clearClientPlayerBaubles(player);
InventoryBaubles baubles = PlayerHandler.getPlayerBaubles(player);
message.inventory.forEachEntry((a, b) -> {
baubles.stackList[a] = b;
if (b.getItem() instanceof IBauble itemBauble) {
itemBauble.onPlayerLoad(b, player);
}
return true;
});
}
return null;
}
}
@@ -21,26 +21,19 @@ 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) {}
}
@@ -49,7 +42,6 @@ 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) {}
}
@@ -60,20 +52,7 @@ public class PacketSyncBauble implements IMessage, IMessageHandler<PacketSyncBau
if (world == null) return null;
Entity e = world.getEntityByID(message.playerId);
if (e instanceof EntityPlayer player) {
InventoryBaubles baubles = PlayerHandler.getPlayerBaubles(player);
if (message.initial) {
if (message.slot == 0) {
PlayerHandler.clearClientPlayerBaubles();
baubles = PlayerHandler.getPlayerBaubles(player);
}
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);
}
PlayerHandler.getPlayerBaubles(player).setInventorySlotContents(message.slot, message.bauble);
}
return null;
}