Solved Setting DamageCause for player to VOID

Discussion in 'Plugin Development' started by J0R1AN, Aug 29, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    J0R1AN

    SOLVED (Look at EDIT)
    I'm kind of a noob in bukkit development and recently came across a problem I couldn't solve.
    I would like to have a function like `p.setDamageCause(DamageCause.VOID)` to damage the player with a specific cause.
    The end goal I'm trying to achieve is to instantly kill the player when they go below y=0, instead of taking forever to die in the vanilla void, and also to display the right death message `name fell out of the world`, instead of just `name died`. So far I have 2 half working functions, using one of both gives a part of the desired end effect, but they don't work together of course.

    Kills player instantly if below y=0 (wrong death message):
    Code:
    @EventHandler
    public void onMove(PlayerMoveEvent e) {
        Player p = e.getPlayer();
        if (p.getLocation().getY() < 0) {
            // something like p.setDamageCause(DamageCause.VOID);
            p.setHealth(0.0); // name died
        }
    }
    Kills player instantly when they take void damage (only happens at vanilla y < -60):
    Code:
    @EventHandler
    public void onDamage(EntityDamageEvent e) {
        if (e.getCause() == EntityDamageEvent.DamageCause.VOID) {
            e.setDamage(100); // name fell out of the world
        }
    }
    I looked online a lot for answers but couldn't find any that solved my problem. If anyone knows an easy solution that would be great :)

    EDIT: After looking for solutions online and asking friends I came to the conclusion that there is no easy way to set a specific DamageCause in Bukkit. However, I found a solution that works very well in my exact case. Since I only need void deaths, I can use the /kill command to kill the player instead of p.setHealth(0.0). Here's the perfectly working code:
    Code:
    @EventHandler
    public void onMove(PlayerMoveEvent e) {
        Player p = e.getPlayer();
        if (p.getLocation().getY() < 0) {
            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "kill " + p.getName());
        }
    }
    Using Bukkit.dispatchCommand to execute commands as console, will still broadcast the command to all ops. To disable this just change the broadcast-console-to-ops property in server.properties to false
     
    Last edited: Sep 5, 2020
Thread Status:
Not open for further replies.

Share This Page