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

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 by JimiIT92
Wrong format

Don't blame me if i always ask for your help. I just want to learn to be better :)

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

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.