Jump to content

Recommended Posts

Posted (edited)

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 by netherstorm123

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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