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

[1.12.2]Problems with changing Player Health and Healthbar


_Cruelar_
 Share

Recommended Posts

After implementing some mobs with high health and attackdamage values I thought I should allow the Player to increase his maximumhealth(with heartcontainers) to a quite high value (I planned 120) but capping his life at this value even with absorption. This would result in six rows of hearts. This is a Problem as you can't see what's behind them. So I subscribed to the RenderGameOverlayEvent.Pre And the hearts render like they should. But the armorvalue is always diplayed in the first row of hearts. Also I noticed that when using a normal java approach the count of used items will be saved for all worlds. I think I should use Capabilities for that but while following the docs I got completely lost.(I never used them before).

My Code:

My EventHandler (Mostly adapted from GuiIngame):

package com.cruelar.cruelars_triforcemod.util;

import net.minecraft.block.material.Material;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiIngame;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.attributes.IAttributeInstance;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.MobEffects;
import net.minecraft.util.FoodStats;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import java.util.Random;

@Mod.EventBusSubscriber
@SideOnly(Side.CLIENT)
public class PlayerHealthHandler {
    protected static final Random rand = new Random();
    public static int playerHealth;
    protected static long healthUpdateCounter;
    protected static int lastPlayerHealth;
    protected static long lastSystemTime;
    protected static Minecraft minecraft=Minecraft.getMinecraft();
    protected static GuiIngame ingameGUI = minecraft.ingameGUI;
    protected static int updateCounter;

    @SubscribeEvent
    public static void renderPlayerHealth(RenderGameOverlayEvent.Pre event){
        if (event.getType()==RenderGameOverlayEvent.ElementType.HEALTH){
            ScaledResolution scaledRes =event.getResolution();
            if (!event.isCanceled()) {
                event.setCanceled(true);
            }
            if (minecraft.getRenderViewEntity() instanceof EntityPlayer)
            {
                updateCounter=ingameGUI.getUpdateCounter();
                EntityPlayer entityplayer = (EntityPlayer)minecraft.getRenderViewEntity();
                int health = MathHelper.ceil(entityplayer.getHealth());
                boolean flag = healthUpdateCounter > (long)updateCounter && (healthUpdateCounter - (long)updateCounter) / 3L % 2L == 1L;

                if (health < playerHealth && entityplayer.hurtResistantTime > 0)
                {
                    lastSystemTime = Minecraft.getSystemTime();
                    healthUpdateCounter = (long)(updateCounter + 20);
                }
                else if (health > playerHealth && entityplayer.hurtResistantTime > 0)
                {
                    lastSystemTime = Minecraft.getSystemTime();
                    healthUpdateCounter = (long)(updateCounter + 10);
                }

                if (Minecraft.getSystemTime() - lastSystemTime > 1000L)
                {
                    playerHealth = health;
                    lastPlayerHealth = health;
                    lastSystemTime = Minecraft.getSystemTime();
                }

                playerHealth = health;
                int lastHealth = lastPlayerHealth;
                rand.setSeed((long)(updateCounter * 312871));
                FoodStats foodstats = entityplayer.getFoodStats();
                int foodLevel = foodstats.getFoodLevel();
                IAttributeInstance iattributeinstance = entityplayer.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH);
                int widthleft = scaledRes.getScaledWidth() / 2 - 91;
                int widthright = scaledRes.getScaledWidth() / 2 + 91;
                int height = scaledRes.getScaledHeight() - 39;
                float maxHealth = (float)iattributeinstance.getAttributeValue();
                int absorption = MathHelper.ceil(entityplayer.getAbsorptionAmount());
                int rows = Math.max(MathHelper.ceil((maxHealth + (float)absorption) / 4.0F / 10.0F),1);
                int rows1 = Math.max(10 - (rows - 2), 3);
                int armorPosY = height-(rows-1)*rows1-10;
                int airPosY = height - 10;
                int absorption1 = absorption;
                int armorValue = entityplayer.getTotalArmorValue();
                int regen = -1;

                if (entityplayer.isPotionActive(MobEffects.REGENERATION))
                {
                    regen = updateCounter % MathHelper.ceil(maxHealth + 5.0F);
                }
                minecraft.mcProfiler.startSection("armor");
                minecraft.getTextureManager().bindTexture(new ResourceLocation("cruelars_triforcemod:textures/gui/icons.png"));

                for (int i = 0; i < 10; ++i)
                {
                    if (armorValue > 0)
                    {
                        int armorPosX = widthleft + i * 8;

                        if (i * 2 + 1 < armorValue)
                        {
                            ingameGUI.drawTexturedModalRect(armorPosX, armorPosY, 18, 36, 9, 9);
                        }

                        if (i * 2 + 1 == armorValue)
                        {
                            ingameGUI.drawTexturedModalRect(armorPosX, armorPosY, 9, 36, 9, 9);
                        }

                        if (i * 2 + 1 > armorValue)
                        {
                            ingameGUI.drawTexturedModalRect(armorPosX, armorPosY, 0, 36, 9, 9);
                        }
                    }
                }
                //minecraft.ingameGUI.drawTexturedModalRect(9+12, 9,12 , , , );
                minecraft.mcProfiler.endStartSection("health");
                minecraft.getTextureManager().bindTexture(new ResourceLocation("cruelars_triforcemod:textures/gui/icons.png"));

            for (int heartsToDraw = MathHelper.ceil((maxHealth + (float)absorption) / 4.0F) - 1; heartsToDraw >= 0; --heartsToDraw)
            {
                int textureX = 0;

                if (entityplayer.isPotionActive(MobEffects.POISON))
                {
                    textureX += 108;
                }
                else if (entityplayer.isPotionActive(MobEffects.WITHER))
                {
                    textureX += 180;
                }

                int textureXOffset = 0;

                if (flag)
                {
                    textureXOffset = 1;
                }

                int posYHelper = MathHelper.ceil((float)(heartsToDraw + 1) / 10.0F) - 1;
                int posX = widthleft + heartsToDraw % 10 * 8;
                int posY = height - posYHelper * rows1;

                if (health <= 6)
                {
                    posY += rand.nextInt(2);
                }

                if (absorption1 <= 0 && heartsToDraw == regen)
                {
                    posY -= 2;
                }

                int hardcore = 0;

                if (entityplayer.world.getWorldInfo().isHardcoreModeEnabled())
                {
                    hardcore = 1;
                }

                ingameGUI.drawTexturedModalRect(posX, posY, textureXOffset * 9, 18 * hardcore, 9, 9);

                if (flag)
                {
                    if (heartsToDraw * 4 + 3 < lastHealth)
                    {
                        ingameGUI.drawTexturedModalRect(posX, posY, textureX + 36, 18 * hardcore, 9, 9);
                    }
                    if (heartsToDraw*4+2<lastHealth)
                    {
                        ingameGUI.drawTexturedModalRect(posX,posY,textureX+45,18*hardcore,9,9);
                    }
                    if (heartsToDraw*4+1<lastHealth)
                    {
                        ingameGUI.drawTexturedModalRect(posX,posY,textureX+54,18*hardcore,9,9);
                    }
                    if (heartsToDraw * 4 + 1 == lastHealth)
                    {
                        ingameGUI.drawTexturedModalRect(posX, posY, textureX + 63, 18 * hardcore, 9, 9);
                    }
                }

                if (absorption1 > 0)
                {
                    if (absorption1 % 4 == 0)
                    {
                        ingameGUI.drawTexturedModalRect(posX, posY, textureX, 18 * hardcore+9, 9, 9);
                        absorption1=absorption1-4;
                    }
                    else if (absorption1 == absorption&&(absorption1-3)%4==0)
                    {
                        ingameGUI.drawTexturedModalRect(posX, posY, textureX+9, 18 * hardcore+9, 9, 9);
                        absorption1=absorption1-3;
                    }
                    else if (absorption1 == absorption&&(absorption1-2)%4==0)
                    {
                        ingameGUI.drawTexturedModalRect(posX, posY, textureX+18, 12 * hardcore+9, 9, 9);
                        absorption1=absorption1-2;
                    }
                    else if (absorption1 == absorption&&(absorption1-1)%4==0)
                    {
                        ingameGUI.drawTexturedModalRect(posX,posY,textureX+27,9*hardcore+9,9,9);
                        absorption1--;
                    }
                }
                else
                {
                    if (heartsToDraw * 4 + 3 < health)
                    {
                        ingameGUI.drawTexturedModalRect(posX, posY, textureX + 36, 18 * hardcore, 9, 9);
                    }
                    if (heartsToDraw*4+2<health)
                    {
                        ingameGUI.drawTexturedModalRect(posX, posY, textureX + 45, 18 * hardcore, 9, 9);
                    }
                    if (heartsToDraw * 4 + 1 < health)
                    {
                        ingameGUI.drawTexturedModalRect(posX, posY, textureX + 54, 18 * hardcore, 9, 9);
                    }
                    if (heartsToDraw *4+1==health)
                    {
                        ingameGUI.drawTexturedModalRect(posX,posY,textureX+63,18*hardcore,9,9);
                    }
                }
            }

                Entity entity = entityplayer.getRidingEntity();

                if (entity == null || !(entity instanceof EntityLivingBase))
                {
                    minecraft.mcProfiler.endStartSection("food");
                    minecraft.getTextureManager().bindTexture(new ResourceLocation("minecraft:textures/gui/icons.png"));

                    for (int foodToDraw = 0; foodToDraw < 10; ++foodToDraw)
                    {
                        int j6 = height;
                        int l6 = 16;
                        int j7 = 0;

                        if (entityplayer.isPotionActive(MobEffects.HUNGER))
                        {
                            l6 += 36;
                            j7 = 13;
                        }

                        if (entityplayer.getFoodStats().getSaturationLevel() <= 0.0F && updateCounter % (foodLevel * 3 + 1) == 0)
                        {
                            j6 = height + (rand.nextInt(3) - 1);
                        }

                        int l7 = widthright - foodToDraw * 8 - 9;
                        ingameGUI.drawTexturedModalRect(l7, j6, 16 + j7 * 9, 27, 9, 9);

                        if (foodToDraw * 2 + 1 < foodLevel)
                        {
                            ingameGUI.drawTexturedModalRect(l7, j6, l6 + 36, 27, 9, 9);
                        }

                        if (foodToDraw * 2 + 1 == foodLevel)
                        {
                            ingameGUI.drawTexturedModalRect(l7, j6, l6 + 45, 27, 9, 9);
                        }
                    }
                }

                minecraft.mcProfiler.endStartSection("air");

                if (entityplayer.isInsideOfMaterial(Material.WATER))
                {
                    int i6 = minecraft.player.getAir();
                    int k6 = MathHelper.ceil((double)(i6 - 2) * 10.0D / 300.0D);
                    int i7 = MathHelper.ceil((double)i6 * 10.0D / 300.0D) - k6;

                    for (int k7 = 0; k7 < k6 + i7; ++k7)
                    {
                        if (k7 < k6)
                        {
                            ingameGUI.drawTexturedModalRect(widthright - k7 * 8 - 9, airPosY, 16, 18, 9, 9);
                        }
                        else
                        {
                            ingameGUI.drawTexturedModalRect(widthright - k7 * 8 - 9, airPosY, 25, 18, 9, 9);
                        }
                    }
                }

                minecraft.mcProfiler.endSection();
            }
        }
    }
}

My Current MaxHealth Setting System:

package com.cruelar.cruelars_triforcemod.util;

import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraftforge.event.entity.living.LivingEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

import java.util.ArrayList;
import java.util.List;

@Mod.EventBusSubscriber
public class AbilityHandler  {
    private static int usedHealthcontainers=0;

    @SubscribeEvent
    public static void updatePlayerAbilityStatus(LivingEvent.LivingUpdateEvent event)
    {
        if(event.getEntityLiving() instanceof EntityPlayer)
        {
            EntityPlayer player = (EntityPlayer)event.getEntityLiving();
            player.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(12.0+(usedHealthcontainers*4));

        }
    }

    public void addHealth(){
        usedHealthcontainers=usedHealthcontainers+1;
    }

    public void reduceHealth(){
        usedHealthcontainers=usedHealthcontainers-1;
    }
}

My CapabilityRegistration(I think):

package com.cruelar.cruelars_triforcemod.util;

import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagInt;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.CapabilityManager;

import javax.annotation.Nullable;

public class HearthContainerCounter {
    @CapabilityInject(IHearthContainerCount.class)
    public static Capability<IHearthContainerCount> HEARTHCOUNTER = null;

    public static void register() {
        CapabilityManager.INSTANCE.register(IHearthContainerCount.class, new Capability.IStorage<IHearthContainerCount>() {
            @Nullable
            @Override
            public NBTBase writeNBT(Capability<IHearthContainerCount> capability, IHearthContainerCount instance, EnumFacing side) {
                NBTTagInt nbtTagInt = new NBTTagInt(instance.getUsedHearthContainer());
                return nbtTagInt;
                // return an NBT tag
            }

            @Override
            public void readNBT(Capability<IHearthContainerCount> capability, IHearthContainerCount instance, EnumFacing side, NBTBase nbt) {
                NBTTagInt nbtTagInt = (NBTTagInt) nbt;
                instance.setUsedHearthContainer(nbtTagInt.getInt());
                // load from the NBT tag
            }
        },new HearthContainerFactory());
    }
}

The Interface:

package com.cruelar.cruelars_triforcemod.util;

public interface IHearthContainerCount {
    public void setUsedHearthContainer(int value);
    public int getUsedHearthContainer();
    public void increaseUsedHearthContainer(int value);
    public void decreaseUsedHearthContainer(int value);
}

The Factory:

package com.cruelar.cruelars_triforcemod.util;

import java.util.concurrent.Callable;

public class HearthContainerFactory implements Callable<IHearthContainerCount> {
    @Override
    public IHearthContainerCount call() throws Exception {
        return new HearthContainerImpl();
    }
}

The Default Implementation:

package com.cruelar.cruelars_triforcemod.util;

public class HearthContainerImpl implements IHearthContainerCount {
    public int usedHearthContainer;

    @Override
    public void setUsedHearthContainer(int value) {
        this.usedHearthContainer=value;
    }

    @Override
    public int getUsedHearthContainer() {
        return usedHearthContainer;
    }

    @Override
    public void increaseUsedHearthContainer(int value) {
        this.usedHearthContainer=this.usedHearthContainer+value;
    }

    @Override
    public void decreaseUsedHearthContainer(int value) {
        this.usedHearthContainer=this.usedHearthContainer-value;
    }
}

The Provider:

package com.cruelar.cruelars_triforcemod.util;

import net.minecraft.nbt.NBTBase;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;


public class HearthContainerCounterProvider implements ICapabilitySerializable<NBTBase> {
    @CapabilityInject(IHearthContainerCount.class)
    public static final Capability<IHearthContainerCount> HEARTHCOUNT = null;

    @Override
    public boolean hasCapability(@Nonnull Capability<?> capability, @Nullable EnumFacing facing) {
        return capability == HearthContainerCounter.HEARTHCOUNTER;

    }

    @Nullable
    @Override
    public <T> T getCapability(@Nonnull Capability<T> capability, @Nullable EnumFacing facing) {
        return capability==HearthContainerCounter.HEARTHCOUNTER?;//don't know what to put here
    }

    @Override
    public NBTBase serializeNBT() {
        return null;
    }

    @Override
    public void deserializeNBT(NBTBase nbt) {

    }
}

Link to the docs if needed

Screenshots of my Health bar with absorption 21 so 21 additional hearts

Spoiler

Without armor

Healthbar1.PNG.121895f569ca5a64704220243690bcb3.PNG

with armor

Healthbar2.PNG.e48472a59f0ed0ad88cc62584b84b11f.PNG

 

My Projects:

Cruelars Triforcemod (1.12 release; 1.14 alpha soon coming)

 

Important:

As my mod is on at least 10 different third party sites without my permission, I want to warn you about that with a link to StopModReposts

Link to comment
Share on other sites

1 minute ago, _Cruelar_ said:

Sadly doesn't work.

What didn't work? Maybe you didn't use it properly. Be more specific please.

That code is working without doubt, so there must be something else.

Edited by Oen44
Link to comment
Share on other sites

Still misplaced like in the screenshot

I added this at the end of my EventHandlers method:

if (event.getType()== RenderGameOverlayEvent.ElementType.ARMOR){
    GuiIngameForge.left_height=ArmorPosY;
}

My Projects:

Cruelars Triforcemod (1.12 release; 1.14 alpha soon coming)

 

Important:

As my mod is on at least 10 different third party sites without my permission, I want to warn you about that with a link to StopModReposts

Link to comment
Share on other sites

It didn't change but not because the statement isn't works. I put your suggestion inside my first if-statement by mistake. So after I changed that it works... almost:

Spoiler

Healthbar3.PNG.2edcc1dfb7e7cca40dd9cf6af2026e29.PNG

I'll need to check better where it should be.

Do you have any experience with capabilities? Cause I would need one and have no idea. See code above.

My Projects:

Cruelars Triforcemod (1.12 release; 1.14 alpha soon coming)

 

Important:

As my mod is on at least 10 different third party sites without my permission, I want to warn you about that with a link to StopModReposts

Link to comment
Share on other sites

7 minutes ago, _Cruelar_ said:

Do you have any experience with capabilities?

Yeah, recently learned about Capabilities. Followed few tutorials (unfortunately not updated) and with some tweaks got it to work.

Here is my code for reference -  https://github.com/Oen44/RPG-Mod-MC

And here you have something about how to make one - http://jabelarminecraft.blogspot.com/p/minecraft-17x.html

  • Thanks 1
Link to comment
Share on other sites

@Oen44

Your PacketAddSkillXP is ServerSide but you use Minecraft.getMinecraft() which is annotated as Side=ClientOnly.

Does this matter?

Are the SkillXp in every World equall or saved per world?

Do they stay the same after reload?

And also I'm interested in the skills the Player gets. later I want to implement custom actions for the player so I I wondered how you made this.

Edited by _Cruelar_

My Projects:

Cruelars Triforcemod (1.12 release; 1.14 alpha soon coming)

 

Important:

As my mod is on at least 10 different third party sites without my permission, I want to warn you about that with a link to StopModReposts

Link to comment
Share on other sites

1 hour ago, _Cruelar_ said:

@Oen44

Your PacketAddSkillXP is ServerSide but you use Minecraft.getMinecraft() which is annotated as Side=ClientOnly.

Does this matter?

Are the SkillXp in every World equall or saved per world?

Do they stay the same after reload?

And also I'm interested in the skills the Player gets. later I want to implement custom actions for the player so I I wondered how you made this.

I should not commit these changes, just wanted to do some cleaning in code, haven't even tested them... Now it's all fixed and working again.

Link to comment
Share on other sites

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

 Share



×
×
  • Create New...

Important Information

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