Jump to content

[1.12.2] Synchronize inventory of an entity


Meldexun

Recommended Posts

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 by Meldexun
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

1 hour ago, diesieben07 said:

PlayerLoggedInEvent, PlayerRespawnEvent and PlayerChangedDimensionEvent.

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.

Link to comment
Share on other sites

Sure.

EventHandler class:

Spoiler

@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:

Spoiler

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:

Spoiler

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:

Spoiler

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:

Spoiler

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 5 months later...

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

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.