Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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

  • 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.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.