Jump to content

Need help on .setHealth [1.20.1]


ANtonYL

Recommended Posts

Posted (edited)

Hi, can anybody tell me what's wrong?

public class SoulDrinkerItem extends Item {
    public SoulDrinkerItem(Properties pProperties) {
        super(pProperties);
    }




    @Override
    public boolean onLeftClickEntity(ItemStack stack, Player player, Entity entity) {
        if (entity instanceof Player){
            Player victim = (Player) entity;
            if(!victim.isDeadOrDying() && victim.getHealth()>0){
                victim.setHealth(0);
                return true;
            }
        }
        return false;
    }



    @Override
    public void setDamage(ItemStack stack, int damage) {
        super.setDamage(stack, 0);
    }
}

The code above is the custom item class. I'm trying to make a Sword-of-the-Cosmos-like item that basically does .setHealth(0) for every single entity it hits. When I go into the game, the attack damage is just 1.

Edited by ANtonYL
Link to comment
Share on other sites

  • ANtonYL changed the title to Need help on .setHealth [1.20.1]

It looks like you're only setting the health if the thing you are hitting is a player.

8 hours ago, ANtonYL said:
if (entity instanceof Player){
            Player victim = (Player) entity;
            if(!victim.isDeadOrDying() && victim.getHealth()>0){
                victim.setHealth(0);
                return true;
            }
        }

 

Link to comment
Share on other sites

2 hours ago, scientistknight1 said:

It looks like you're only setting the health if the thing you are hitting is a player.

 

Ok. Thanks. by the way, will this crash in any circumstances?

public boolean onLeftClickEntity(ItemStack stack, Player player, Entity entity) {
        if (entity instanceof LivingEntity){
            LivingEntity victim = (LivingEntity) entity;
            if(!victim.isDeadOrDying() && victim.getHealth()>0){
                victim.setHealth(0);
                return true;
            }
        }
        return false;
    }

 

Link to comment
Share on other sites

How can I add drops when killing an entity? Now there are no drops. How can I add an @override to change the attack speed to 1.6 and show "when in main hand...attack damage,...attack speed"? also, how can I make the item enchantable? 

Link to comment
Share on other sites

To ensure that the entity drops resources in the usual way, try using the .kill() method instead of .setHealth(int); that should work better. Try making your class extend SwordItem to use all the standard sword infrastructure. Just make sure to add super calls at the top of all methods you override if you want to keep the existing behavior as well.

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.