Resource PlayerJumpEvent

Discussion in 'Spigot Plugin Development' started by Enecildn, Aug 12, 2018.

  1. Code (Java):
    import org.bukkit.Bukkit;
    import org.bukkit.Statistic;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Cancellable;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.HandlerList;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerEvent;
    import org.bukkit.event.player.PlayerStatisticIncrementEvent;
    import org.bukkit.util.Vector;

    public class PlayerJumpEvent extends PlayerEvent implements Cancellable {
        private static final HandlerList handlers = new HandlerList();
        private static final Listener listener = new PlayerJumpEventListener();
        private PlayerStatisticIncrementEvent playerStatisticIncrementEvent;
        private boolean isCancelled = false;

        static {
            Bukkit.getServer().getPluginManager().registerEvents(listener, plugin);
        }

        public PlayerJumpEvent(Player player, PlayerStatisticIncrementEvent playerStatisticIncrementEvent) {
            super(player);
            this.playerStatisticIncrementEvent = playerStatisticIncrementEvent;
        }

        @Override
        public boolean isCancelled() {
            return isCancelled;
        }

        @Override
        public void setCancelled(boolean cancel) {
            isCancelled = cancel;
            if (cancel) {
                player.setVelocity(new Vector());
                playerStatisticIncrementEvent.setCancelled(cancel);
            }
        }

        @Override
        public HandlerList getHandlers() {
            return handlers;
        }

        public static HandlerList getHandlerList() {
            return handlers;
        }

        private static class PlayerJumpEventListener implements Listener {
            @EventHandler
            public void onPlayerStatisticIncrement(PlayerStatisticIncrementEvent event) {
                if (event.getStatistic() == Statistic.JUMP) {
                    Bukkit.getServer().getPluginManager().callEvent(new PlayerJumpEvent(event.getPlayer(), event));
                }
            }
        }
    }
    This PlayerJumpEvent listens to PlayerStatisticIncrementEvent and is called whenever a player's jump statistic increases. In my opinion, this is the most accurate way to listen to and cancel a player jumping. Yet it is still called when a player starts to climb up a ladder or a vine because climbing up a ladder or a vine is technically jumping. In order to use this in your plugin, change the plugin variable in the static initialization block to your plugin instance. Feel free to use this and leave a comment if you have any questions or need any help.
     
    • Like Like x 2
  2. You can actually remove the Cancellable implementation because canceling this is useless, it won't do anything. The best thing what you could do is deny the statistic to increment.
    Even more Paper does have such event (PlayerJumpEvent).

    Not to ruin your resource, but are just facts.

    EDIT: Even more, registering your event in that way with static block is not safe at all.
     
    • Agree Agree x 3
  3. @Parozzz, how is the fact that Paper has a jump event relevant here? That's not gonna help anyone building against Bukkit or Spigot.

    And could you please explain why it's "not safe" to register listeners in a static initializer block? It's really unhelpful to just say "don't do that". You have an opportunity to teach someone something - seize it :)
     
    • Agree Agree x 2
  4. a lot of us run paperspigot, because why wouldnt you
     
    • Agree Agree x 1
  5. Thanks this is useful
     
  6. Yeah, maybe I was too vague in my answer.
    1. Initializing the listener in a static block could lead to problem. What if the class is initialized (Then the static block is called) before Bukkit has had the time to load? Not good times.
    2. The reason because I suggested to use Paper is because if something has been already done, why re-invent the wheel?
    3. Using the Cancellable interface here is useless, since he/she does not check anywhere in the listener for the PlayerStatisticIncrementEvent if the event called is canceled or not.

    EDIT: I just what you do in the setCancelled method (I am blind maybe?). Yet, what if the player is running and jumping at the same time? It will negate the speed he has while running other than the jump.
     
    #6 Parozzz, Aug 13, 2018
    Last edited: Aug 13, 2018
  7. a lot of us don't, because why would we
     
    • Like Like x 3
    • Agree Agree x 3
    • Optimistic Optimistic x 1
  8. Well, seeing as plugins are dependent on Bukkit being active to run, I don't see how this could ever be achieved. The JRE doesn't just load plugin jars when it feels like it -- Bukkit has to load them itself. Therefore, if Bukkit hasn't had time to load, then neither have your plugins.
     
    • Agree Agree x 1
    • Informative Informative x 1
  9. you dont want the optimizations paper has?
     
  10. He is really not forced to use PaperSpigot since Spigot already provides OP what really wants. Also, like OP said above. There are many people who use Spigot than PaperSpigot, and do not forget, this section called Spigot Plugin Development.

    For the code, I really would recommend a better way to register a custom event, like others mentioned.
     

Share This Page