Sending Packets?
Terrails opened this issue ยท 7 comments
How should I send a packet with a thirst level to the client. People requested to I add a feature to keep thirst on death in my mod, but I'm having problems with syncing client and server. 40% of time when you die thirst has a visual bug where its full until I update it with losing or gaining it. I saw to you use write NBT in your ThirstHandler and read it in MessageUpdateStat but is there a way to I just send the thirst level without implemeting IThirst?
Doesn't help... I'm not sure what the problem is.
My thirst is 19 and I can drink water but every time I die there is a chance to my bar gets full like this (You can see to something Overrides it and gets it to full):
This is the code I use
https://github.com/Terrails/KeepHunger/blob/master/src/main/java/terrails/keephunger/event/TANEvent.java#L57
You should be using the player clone event to do that, by the time the spawn event is fired the new player has already been created and the values have been reset
Still doesn't work... I'm not sure what's happening, I tried removing my entityPlayer.setThirst and only using the thirstData.setThirst.
https://github.com/Terrails/KeepHunger/blob/master/src/main/java/terrails/keephunger/event/TANEvent.java#L30
Looking into it now, the change packet is actually received by the client, however sometimes at the time of receiving it Minecraft.getMinecraft().player still refers to the player that has just died, meaning no change actually ends up occurring. Seems like it's some sort of timing issue. I'd imagine if you kept track of the newly respawned player entity and then perform the update on the first PlayerTickEvent it'd work correctly.
I'll keep digging and see what I can find, however the above would work as a workaround
Thirst data is intended to be read/written using ThirstHelper. I'm not sure why you'd need to handle sending data yourself, something like this should suffice:
IThirst thirstData = ThirstHelper.getThirstData(player);
thirstData.setThirst(whatever);
The timing issue is because you do are trying to send packets in the clone event, which is not guaranteed to work, like you discovered. You have to delay the syncing until the respawn event. Just sending the data always in respawn event should not hurt anyone.
Looking at the code I'm not sure I see any alternative ways of implementing it on our side to work around the issue. Essentially all you'd need to do is add the player to some sort of 'need to update' list, save their previous thirst value, and then update it during the PlayerTickEvent respawn event as suggested earlier using ThirstHelper
EDIT: Updated to include diesieben's suggestion