Jump to content

How to change a player's health


iYM8jXCs3pcvblTE

Recommended Posts

Hello, guys.
My goal is to change the health of all other players after listening to the health change of one player.
I am currently listening to the progress of the entity's health change with the help of all of you.
The code is as follows

    @SubscribeEvent
    public static void demo04(LivingHurtEvent event){
    }

In this listener event, I can use 'setAmount' to change the damage caused by this event. I can also use 'getAmount' to get the value of the damage caused by this event. But I've searched the source code of this event/class for a long time, but I still can't find a way to change the health of myself or other players.

I only found a way to change health in the 'minecraft' game.(net.minecraft.world.entity.LivingEntity)

setHealth
public void setHealth(float p_21154_)

But when I want to call it, I find that I don't have the parameter needed for this method. The parameter could be the player's parameter or the target entity's parameter. I'm not sure, I'm still double checking this source code.

Ultimately, my question is whether there is a more convenient class/event/method for 'forge' to change the player's health. If this idea doesn't fit, are there other ways to achieve the same goal.

Link to comment
Share on other sites

9 hours ago, Luis_ST said:

you need add a AttributeModifier with Attributes.MAX_HEALTH in this case

I'm sorry, I just read the following and I may not have expressed myself clearly.
I see that 'Attributes.MAX_HEALTH' is the maximum health, can this method/parameter be used to set the current health of a player?
If combined with my usage scenario. If a player takes damage (this listener I have implemented to listen to all entities taking damage), then the other players will take the same damage (no change in max health). When a player health reverts (this listener I have implemented to listen for all entities health reverting), then the other players will also revert the same health.
Above is the first question: How should I change the current health of other players/self?
I get the 'event.getEntity()' injured entity in the event that listens to an entity that is injured, which could also be the entity that caused the injury, I'm still testing that at the moment. Since not all entities need to be injured, I need to execute my code. For example, if I inflict damage on a zombie, I don't need to execute this code.
So I added a judgment below the event, but I don't know how to determine an 'Entity' parameter. My current idea is that I listen for events where a player enters the world, and then when a player enters the world, I get that player's entity. Then I put the entity data into an array, and then I determine in the judgment whether the injured entity data is the same as a member of the array.
The specific code is as follows

        for (int i = 0;i < PlayerListArrays.length;i++){
            if (event.getEntity() == PlayerListArrays[i]){
                
            }
        }

I know this code may not execute for various reasons, as this is just a rough idea that I have not implemented yet.

Edited by iYM8jXCs3pcvblTE
Correction of incorrect code
Link to comment
Share on other sites

1 hour ago, iYM8jXCs3pcvblTE said:

I'm sorry, I just read the following and I may not have expressed myself clearly.
I see that 'Attributes.MAX_HEALTH' is the maximum health, can this method/parameter be used to set the current health of a player?
If combined with my usage scenario. If a player takes damage (this listener I have implemented to listen to all entities taking damage), then the other players will take the same damage (no change in max health). When a player health reverts (this listener I have implemented to listen for all entities health reverting), then the other players will also revert the same health.
Above is the first question: How should I change the current health of other players/self?
I get the 'event.getEntity()' injured entity in the event that listens to an entity that is injured, which could also be the entity that caused the injury, I'm still testing that at the moment. Since not all entities need to be injured, I need to execute my code. For example, if I inflict damage on a zombie, I don't need to execute this code.
So I added a judgment below the event, but I don't know how to determine an 'Entity' parameter. My current idea is that I listen for events where a player enters the world, and then when a player enters the world, I get that player's entity. Then I put the entity data into an array, and then I determine in the judgment whether the injured entity data is the same as a member of the array.
The specific code is as follows

        for (int i = 0,event.getEntity() == PlayerListArrays[i],i++){

        }

I know this code may not execute for various reasons, as this is just a rough idea that I have not implemented yet.

This code seems to have generated an error when I entered the translation device, please wait while I correct it.

Link to comment
Share on other sites

3 hours ago, diesieben07 said:

LivingEntity#setHealth is what you need.

Hi, The 'LivingEntity' class I tried to instantiate before and then I found out that it is an abstract class. I tried to create a new class to inherit from 'LivingEntity' but I found that it seems that the class I inherit from cannot modify/get data about 'LivingEntity'. It is possible that I am trying the wrong way, I will try again later to create a new class and inherit the class from 'LivingEntity'. I will get back to you with the results.

Link to comment
Share on other sites

3 hours ago, Luis_ST said:

What are you talking about?

I thought I could instantiate 'LivingEntity', but I realized later that 'LivingEntity' is an abstract class that I can't instantiate.' LivingEntity' seems to require me to provide an entity's data for 'LivingEntity' to work. Currently I've got the player entity data by listening to the player login. But currently I'm looking for a way to change their properties through this data.
The code is as follows

  • main.java
package com.AJ.LifeShareing;;

import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod(Utils.MOD_ID)
@Mod.EventBusSubscriber()
public class Main {
    public static Entity [] playerList = new Entity[10];
    public static int length = 0;
    @SubscribeEvent
    public static void getPlayerList(PlayerEvent.PlayerLoggedInEvent event){
        GetPlayerList getPlayerList = new GetPlayerList();
        length = GetPlayerList.getPlayerList(event.getEntity(),playerList,length);
    }
    @SubscribeEvent
    public static void setHealth(LivingHurtEvent event){

    }
}

  • getPlayerList.java
package com.AJ.LifeShareing;

import net.minecraft.world.entity.Entity;

public class GetPlayerList {
    public static int getPlayerList(Entity player,Entity [] playerList,int length) {
        for (int i = 0; i == length; i++) {
            System.out.println("aaaaaaaaaaaaaaaaaaaa" + i);
            if (player != playerList[i]) {
                if (i == length) {
                    playerList[i] = player;
                    System.out.println("jjjjjjjjjjjjjj" + playerList[i]);
                    length++;
                    break;
                }
            } else {
                break;
            }
        }
        return length;
    }
}

 

Link to comment
Share on other sites

3 hours ago, diesieben07 said:

You need to call setHealth on the entity instance you want to set the health of - e.g. the player. Do not try to make a new entity, that won't help you here.

When I get the player's entity data, I don't seem to find the 'sethealth' method, and I'm not sure if this is because I'm overlooking him. When I try to look at 'LivingEntity', I find that it is an abstract class and I may not be able to instantiate 'LivingEntity' directly. But currently I hold the entity data provided by the player when he enters the game, I can try to inherit 'LivingEntity' and use the entity data as a parameter to try to modify the target's health.

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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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 Terms of Use.