Jump to content

setHealth(0) not killing


PlainPlaying

Recommended Posts

I am trying to make it so that when you eat an item, after 5 seconds you die, I use the TickEvent for it, when i do event.player.setHealth(0), the death screen comes up, but the player doesn't die, meaning the respawn button doesn't work and the player can die whilst in the death menu, here's the code that I am using (I added a food item called hog_meat, hogCurse is a global boolean that turns on when you eat the hog_meat, and hog_timer is a global timer that starts when you eat hog_meat):
 

    @SubscribeEvent
    public static void onTick(TickEvent.PlayerTickEvent event){
        if (event.player.getEntityWorld().isRemote) {
            if (hogCurse) {
                if (hogCurseTimer < 5 * 40) {
                    hogCurseTimer++;
                    if (hogCurseTimer % 40 == 0) {
                        String msg = TextFormatting.RED + "You have " + (6 - hogCurseTimer / 40) + " Seconds left until you DIE!";
                        event.player.sendMessage(new StringTextComponent(msg), event.player.getUniqueID());
                    }
                } else {
                    if (event.player.isAlive()) {
                        String msg = TextFormatting.RED + "You didn't eat bread, now DIE!";
                        event.player.sendMessage(new StringTextComponent(msg), event.player.getUniqueID());
                        event.player.setHealth(0);
                        hogCurse = false;
                        hogCurseTimer = 0;
                    }
                }
            }
        }
    }

What can I do to make it work?

Link to comment
Share on other sites

28 minutes ago, PlainPlaying said:

What can I do to make it work?

The kill command uses this to kill an entity:

event.player.attackEntityFrom(DamageSource.OUT_OF_WORLD, Float.MAX_VALUE)

However, there might be other problems with the code. You should not use global variables like this, for example what would happen if several players eat the item at the same time? You should use capabilities for these kinds of things

Edited by vemerion
Add note about capabilities
Link to comment
Share on other sites

2 minutes ago, PlainPlaying said:

i define all the variables at the start of the class, but wouldn't it say <PLAYER> fell out of the world?

You can create a custom DamageSource if you want a more appropriate death message. However, note that you should not use global variables like this, since they will be shared across all players. You should instead use capabilities.

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.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.