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

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

  • 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;
            }
        }

 

  • Author
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;
    }

 

  • Author

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? 

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.

  • Author

Thank you so much. Also, is it possible to make the name of the sword constantly changing colors and show "+infinity attack damage "?

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.