Posted May 19, 20187 yr Hello, I am having a little problem with telling the server who the EntityPlayerMP is. I am using capabilities to save details about the player and then transfer it from server to client. I resorted to packets as the health was not in sync with the server when above 20. Such issue did not exist in single player as the health always returned to what it was supposed to be. My packet code is as follows: public class PacketCurrentHealth implements IMessage { EntityPlayerMP player; private float currentHealth; @Override public void fromBytes(ByteBuf buf) { currentHealth = buf.readFloat(); } @Override public void toBytes(ByteBuf buf) { buf.writeFloat(currentHealth); } public PacketCurrentHealth() { ICurrentHealth cH = player.getCapability(CurrentHealthProvider.CURRENTHEALTH_CAP, null); currentHealth = cH.getCurrentHealth(); } public static class Handler implements IMessageHandler<PacketCurrentHealth, IMessage> { @Override public IMessage onMessage(PacketCurrentHealth message, MessageContext ctx) { FMLCommonHandler.instance().getWorldThread(ctx.netHandler).addScheduledTask(() -> handle(message, ctx)); return null; } public void handle(PacketCurrentHealth message, MessageContext ctx) { EntityPlayerMP playerEntity = ctx.getServerHandler().player; ICurrentHealth cH = playerEntity.getCapability(CurrentHealthProvider.CURRENTHEALTH_CAP, null); playerEntity.setHealth(message.currentHealth); } } } I call it when the player joins the world on the common(client and server) side: @SubscribeEvent public void onJoinWorld(EntityJoinWorldEvent event) { if (event.getEntity() instanceof EntityPlayerMP) { EntityPlayerMP player = (EntityPlayerMP) event.getEntity(); PacketHandler.INSTANCE.sendToServer(new PacketCurrentHealth()); } } I register the packets using this method where the registerMessages() is called on the common. public static void registerMessages() { INSTANCE.registerMessage(PacketCurrentHealth.Handler.class, PacketCurrentHealth.class, nextID(), Side.SERVER); } I have tested the capability and must say that it does work on both sides. Due to the issue being a multiplayer problem, I update the capability on the server only using: @SubscribeEvent public void playerTick(LivingEvent.LivingUpdateEvent event) { if(event.getEntityLiving() instanceof EntityPlayerMP) { EntityPlayerMP player = (EntityPlayerMP) event.getEntityLiving(); if (player.world.isRemote) return; ICurrentHealth currentHealth = player.getCapability(CurrentHealthProvider.CURRENTHEALTH_CAP, null); currentHealth.set(player.getHealth()); } } Thank you, I am looking forward to seeing possible solutions. Edited May 19, 20187 yr by netherstorm123
May 19, 20187 yr Author Thank you, I apparently had an issue with my build where it stopped syncing after I modified the maximum health after 20 points. I have tested it on a dedicated server and it works fine.
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.