Meldexun Posted July 15, 2019 Posted July 15, 2019 (edited) Hey there, I need to synchronize the inventory of my custom entity (not the player) with the client. How can i do this? So i'm attaching a CapabilityItemHandler to my entity and when the player is riding the entity he should get information from the inventory of the entity displayed. But i currently have to open the inventory once and only then the player can see the information from the inventory of the entity. Edited July 15, 2019 by Meldexun
Meldexun Posted July 15, 2019 Author Posted July 15, 2019 On 7/15/2019 at 2:04 PM, diesieben07 said: Send a custom packet with the inventory data whenever the data changes and the entity is being ridden and whenever a player starts riding the entity. Expand Ok thank you. It's working now.
Meldexun Posted July 15, 2019 Author Posted July 15, 2019 So i'm now sending a packet when the item stack changes while the entity is being ridden and when a player starts riding. But what should i do when the player is riding the entity while the world is loaded. I tried sending a packet inside the onAddedToWorld method of my entity but then the entity isn't loaded client side and nothing will get synchronized.
Meldexun Posted July 15, 2019 Author Posted July 15, 2019 On 7/15/2019 at 9:36 PM, diesieben07 said: PlayerLoggedInEvent, PlayerRespawnEvent and PlayerChangedDimensionEvent. Expand Maybe i didn't described my issue good enough. So lets say you start a singleplayer world and spawn my custom entity. Then you put an item in the inventory of the entity. After that you start riding the entity. And while you are still riding the entity you are leaving the world and go in the main menu. Now you join the world again. The problem is that every event i'm trying to use to send a packet is called to early on the server. So in the end the client receives a packet but the entity isn't there already so nothing can be synchronized. In java the problem is: Minecraft.getMinecraft().world.getEntityByID(message.getEntityId()) returns null because the entity isn't created on the client.
Meldexun Posted July 16, 2019 Author Posted July 16, 2019 But i tested it and it doesn't work. Minecraft.getMinecraft().world.getEntityByID(message.getEntityId()) still returns null.
Meldexun Posted July 16, 2019 Author Posted July 16, 2019 Sure. EventHandler class: Reveal hidden contents @EventBusSubscriber(modid = BetterDiving.MOD_ID) public class SeamothEventHandler { //other events @SubscribeEvent public static void login(PlayerLoggedInEvent event) { Entity entity = event.player.getRidingEntity(); if (entity instanceof EntitySeamoth) { ((EntitySeamoth) entity).syncInventory(); } } } Entity class: Reveal hidden contents public class EntitySeamoth extends Entity { public void syncInventory() { if (!this.world.isRemote && this.getControllingPassenger() instanceof EntityPlayer) { ItemStack stack = this.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null).getStackInSlot(0); BetterDiving.CONNECTION.sendTo(new SPacketSyncSeamothBattery(this.getEntityId(), Item.getIdFromItem(stack.getItem())), (EntityPlayerMP) this.getControllingPassenger()); if (stack.getItem() instanceof ItemEnergyCell) { CapabilityCustomEnergy energy = ((CapabilityCustomEnergy) stack.getCapability(CapabilityEnergy.ENERGY, null)); BetterDiving.CONNECTION.sendTo(new SPacketSyncSeamothEnergy(this.getEntityId(), energy.getEnergyStored()), (EntityPlayerMP) this.getControllingPassenger()); } } } } Packet class: Reveal hidden contents public class SPacketSyncSeamothBattery implements IMessage { private int entityId; private int itemId; public SPacketSyncSeamothBattery() { } public SPacketSyncSeamothBattery(int entityId, int itemId) { this.entityId = entityId; this.itemId = itemId; } @Override public void fromBytes(ByteBuf buf) { this.entityId = buf.readInt(); this.itemId = buf.readInt(); } @Override public void toBytes(ByteBuf buf) { buf.writeInt(this.entityId); buf.writeInt(this.itemId); } public int getEntityId() { return entityId; } public int getItemId() { return itemId; } } PacketHandler class: Reveal hidden contents public class CPacketHandlerSyncSeamothBattery implements IMessageHandler<SPacketSyncSeamothBattery, IMessage> { @Override public IMessage onMessage(SPacketSyncSeamothBattery message, MessageContext ctx) { FMLCommonHandler.instance().getWorldThread(ctx.netHandler).addScheduledTask(() -> { BetterDiving.proxy.handlePacketSyncSeamothBattery(message, ctx); }); return null; } } ClientProxy class: Reveal hidden contents public class ClientProxy implements IProxy { //other methods @Override public void handlePacketSyncSeamothBattery(SPacketSyncSeamothBattery message, MessageContext ctx) { Entity entity = Minecraft.getMinecraft().world.getEntityByID(message.getEntityId()); System.out.println(entity); if (entity instanceof EntitySeamoth) { ItemStack stack = new ItemStack(Item.getItemById(message.getItemId())); ((ItemStackHandler) entity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)).setStackInSlot(0, stack); } } } When i start the game it prints null in the console.
Meldexun Posted July 16, 2019 Author Posted July 16, 2019 Yes, i tried this too. But the problem is that still the entity is loaded on the server but not on the client. -> nothing can synchronize. The only solution i see could be to send a packet from the client and then send an answer packet from the server.
Meldexun Posted July 16, 2019 Author Posted July 16, 2019 Then you misunderstood me. The player and the entity are loaded server side. Then an event is fired and sends a packet from the server side to the client side. The packet is received on the client side but the entity is NOT loaded on the client side. So nothing can be done.
ephys Posted January 15, 2020 Posted January 15, 2020 I have the same issue where the data I send from the server to the client about an entity cannot be used at that time because the entity has not loaded yet on the client My solution was to store the message until the entity actually loaded, and to process the message during the EntityJoinWorldEvent event Hopefully that was the right way to do it You can find an example implementation here https://github.com/Ephys/mc-who-let-the-dogs-out/blob/5d38102cefde1648809db9006708b5d8d1487be2/src/main/java/be/ephys/wltdo/NewSkinMessageHandler.java
Recommended Posts