Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[Solved][1.16.4] How do I correctly sync capability data between Client and server?


Recommended Posts

Okay, I believe I've done what you have told me, and my gui does update me gaining xp.

 

However, theres something wrong with my sync method, as whenever it is called, it resets all xp values to zero. So the values in my gui are lost when reloading the world.

 

Heres the sync method in my Skills class:

	@Override
	public void sync(ServerPlayerEntity player) {
		if(entity instanceof ServerPlayerEntity) {
			PacketHandler.sendTo(new SkillsPacket(serializeNBT()), player);
		}
	}

 

Heres my attempt at syncing on login, in my player event handler:

	@SubscribeEvent
	public static void onPlayerConnect(PlayerLoggedInEvent event) {
		ServerPlayerEntity player = (ServerPlayerEntity) event.getPlayer();
		if(!player.world.isRemote) {
			player.getCapability(SkillCapability.SKILL_CAP).ifPresent(c -> c.sync(player));
		}
	}

 

Updated my git repo, here.

Link to post
Share on other sites

I've changed the serializeNBT and deserialize methods so the value of i is in the key:

	@Override
	public CompoundNBT serializeNBT() {
		CompoundNBT data = new CompoundNBT();
		
		for(int i = 0; i < 26; i++) {
			data.putInt("xp_" + i, (int) xp[i]);
			data.putInt("dynamic_" + i, dynamicLevels[i]);
			data.putInt("static_" + i, staticLevels[i]);
		}
		return data;
	}
	
	@Override
	public void deserializeNBT(CompoundNBT data) {
		
		for(int i = 0; i < 26; i++) {
			xp[i] = data.getInt("xp_" + i);
			dynamicLevels[i] = data.getInt("dynamic_" + i);
			staticLevels[i] = data.getInt("static_" + i);
		}
	}

 

And now it works, thank you! (I think thats what the first person to reply to this post was trying to tell me to do but I didn't understand at the time).

Link to post
Share on other sites
  • LK1905 changed the title to [Solved][1.16.4] How do I correctly sync capability data between Client and server?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.