Posted October 18, 20196 yr I've made a packet that changes the player max health value. The packet itself should work fine, if i add some value to the max health the new hearts are displayed correctly. However if i remove some value from the max health, say i go from 10 hearts to 9.5, the new hearts values in GUI did not update... But if i go from 10 to 9 it did, so just removing half heart is not showing properly. This is the packet i send to the client or the server when i change the max health package com.antiblaze.network; import com.antiblaze.settings.Settings; import net.minecraft.client.Minecraft; import net.minecraft.entity.SharedMonsterAttributes; import net.minecraft.entity.ai.attributes.AttributeModifier; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.network.PacketBuffer; import net.minecraftforge.fml.network.NetworkDirection; import net.minecraftforge.fml.network.NetworkEvent; import java.util.function.Supplier; /** * @author JimiIT92 */ public class MaxHealthPacket { /** * Health value to add or remove from the max health */ float health; /** * Constructor. Set the health amount * @param health Health amount */ public MaxHealthPacket (float health) { this.health = health; } /** * Write the health amount to the buffer * @param packet Packet * @param buffer Buffer */ public static void encode(MaxHealthPacket packet, PacketBuffer buffer) { buffer.writeFloat(packet.health); } /** * Get the health value from the buffer * @param buffer Buffer * @return MaxHealth Packet */ public static MaxHealthPacket decode(PacketBuffer buffer) { float health = buffer.readFloat(); return new MaxHealthPacket(health); } /** * Handler inner class */ public static class Handler{ /** * Handle the Packet * @param packet Packet * @param context Context */ public static void handle(final MaxHealthPacket packet, Supplier<NetworkEvent.Context> context) { context.get().enqueueWork(() -> { PlayerEntity player = null; if (context.get().getDirection() == NetworkDirection.PLAY_TO_CLIENT) { player = Minecraft.getInstance().player; } else { player = context.get().getSender(); } assert player != null; double baseAmount = player.getMaxHealth() - player.getAttribute(SharedMonsterAttributes.MAX_HEALTH).getBaseValue(); AttributeModifier modifier = new AttributeModifier(Settings.MAX_HEALTH_ATTRIBUTE_MODIFIER_ID, Settings.MAX_HEALTH_ATTRIBUTE_NAME, baseAmount + packet.health , AttributeModifier.Operation.ADDITION); player.getAttribute(SharedMonsterAttributes.MAX_HEALTH).removeModifier(Settings.MAX_HEALTH_ATTRIBUTE_MODIFIER_ID); player.getAttribute(SharedMonsterAttributes.MAX_HEALTH).applyModifier(modifier); }); context.get().setPacketHandled(true); } } } What should i do to make so when the max health is a "middle value" (like 9.5 hearts, 8.5 hearts, 11.5 hearts ecc...) the hearts in the GUI are displayed correctly? Edited October 18, 20196 yr by JimiIT92 Wrong format Don't blame me if i always ask for your help. I just want to learn to be better
October 19, 20196 yr Author Any idea? I've kinda solved this by causing the food level to update, but of course is not something i want to keep /** * Handle the Packet * @param packet Packet * @param context Context */ public static void handle(final MaxHealthPacket packet, Supplier<NetworkEvent.Context> context) { context.get().enqueueWork(() -> { PlayerEntity player = null; if (context.get().getDirection() == NetworkDirection.PLAY_TO_CLIENT) { player = Minecraft.getInstance().player; } else { player = context.get().getSender(); } assert player != null; double baseAmount = player.getMaxHealth() - player.getAttribute(SharedMonsterAttributes.MAX_HEALTH).getBaseValue(); AttributeModifier modifier = new AttributeModifier(Settings.MAX_HEALTH_ATTRIBUTE_MODIFIER_ID, Settings.MAX_HEALTH_ATTRIBUTE_NAME, baseAmount + packet.health , AttributeModifier.Operation.ADDITION); player.getAttribute(SharedMonsterAttributes.MAX_HEALTH).removeModifier(Settings.MAX_HEALTH_ATTRIBUTE_MODIFIER_ID); player.getAttribute(SharedMonsterAttributes.MAX_HEALTH).applyModifier(modifier); if(player.getMaxHealth() % 2 == 1) { player.getFoodStats().setFoodLevel(player.getFoodStats().getFoodLevel() - 1); if (context.get().getDirection() == NetworkDirection.PLAY_TO_CLIENT) { Minecraft.getInstance().player.getFoodStats().setFoodLevel(player.getFoodStats().getFoodLevel() - 1); } } }); context.get().setPacketHandled(true); } Don't blame me if i always ask for your help. I just want to learn to be better
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.