Plugin Detect Player Damage

Discussion in 'Plugin Development' started by Lukaos187, Feb 14, 2024.

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


    Hi, I have a question...

    I'm relatively new to coding plugins and wanted to make a plugin where if the player takes any damage the player is thrown into the air. Everything works except detecting the damage. The player is somehow only launched if he takes fall damage, but not if he was hit by a zombie, for example. Can someone help me with that? Thanks:)
    Here is the code:
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByBlockEvent;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.util.Vector;
    public class SchadenGleichLaunchChallenge extends JavaPlugin {
            public class SchadenListener implements Listener {
             @EventHandler(priority = EventPriority.HIGHEST)
                     public void onEvent(final EntityDamageEvent event) {
                              Entity e = event.getEntity();
                               if (e instanceof Player || event instanceof EntityDamageByEntityEvent || event instanceof     EntityDamageByBlockEvent) {
                               Player player = (Player)e;
                               player.setVelocity(new Vector(0, 70, 0));
    public void onEnable() {
            PluginManager pluginManager = this.getServer().getPluginManager();
            SchadenListener listener = new SchadenListener();
            pluginManager.registerEvents(listener, this);
    public void onDisable() {
  2. Offline


    @Lukaos187 According to the code you provided, I'd think the player was launched upon taking any kind of damage. Are you sure this is the exact code you're testing?
    Also, || means "or", so you're checking whether the victim of the damage event is a player OR the event is a <class>. Afterwards, you're casting the entity to Player, which will throw an exception if, for example, a zombie took damage.
  3. Offline


    -> Hmm. I am testing the exact same code.
    But thanks for your help with the || :)
Thread Status:
Not open for further replies.

Share This Page