From 4ccee91c64b006b88372bf10b9a2a8b63ccd05c7 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Wed, 16 Jun 2021 15:45:10 +0100 Subject: [PATCH] Added PlayerJumpEvent --- .../eco/core/events/PlayerJumpEvent.java | 40 ++++++++++++ .../willfp/eco/spigot/EcoSpigotPlugin.java | 2 + .../eventlisteners/PlayerJumpListeners.java | 62 +++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 eco-api/src/main/java/com/willfp/eco/core/events/PlayerJumpEvent.java create mode 100644 eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/eventlisteners/PlayerJumpListeners.java diff --git a/eco-api/src/main/java/com/willfp/eco/core/events/PlayerJumpEvent.java b/eco-api/src/main/java/com/willfp/eco/core/events/PlayerJumpEvent.java new file mode 100644 index 00000000..518a77e2 --- /dev/null +++ b/eco-api/src/main/java/com/willfp/eco/core/events/PlayerJumpEvent.java @@ -0,0 +1,40 @@ +package com.willfp.eco.core.events; + +import org.bukkit.event.HandlerList; +import org.bukkit.event.player.PlayerMoveEvent; +import org.jetbrains.annotations.NotNull; + +public class PlayerJumpEvent extends PlayerMoveEvent { + /** + * Internal bukkit. + */ + private static final HandlerList HANDLERS = new HandlerList(); + + /** + * Create a new PlayerJumpEvent. + * + * @param event The PlayerMoveEvent. + */ + public PlayerJumpEvent(@NotNull final PlayerMoveEvent event) { + super(event.getPlayer(), event.getFrom(), event.getTo()); + } + + /** + * Internal bukkit. + * + * @return The handlers. + */ + @Override + public @NotNull HandlerList getHandlers() { + return HANDLERS; + } + + /** + * Internal bukkit. + * + * @return The handlers. + */ + public static @NotNull HandlerList getHandlerList() { + return HANDLERS; + } +} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/EcoSpigotPlugin.java b/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/EcoSpigotPlugin.java index 93d69690..956f559d 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/EcoSpigotPlugin.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/EcoSpigotPlugin.java @@ -25,6 +25,7 @@ import com.willfp.eco.spigot.eventlisteners.ArmorListener; import com.willfp.eco.spigot.eventlisteners.DispenserArmorListener; import com.willfp.eco.spigot.eventlisteners.EntityDeathByEntityListeners; import com.willfp.eco.spigot.eventlisteners.NaturalExpGainListeners; +import com.willfp.eco.spigot.eventlisteners.PlayerJumpListeners; import com.willfp.eco.spigot.gui.GUIListener; import com.willfp.eco.spigot.integrations.anticheat.AnticheatAAC; import com.willfp.eco.spigot.integrations.anticheat.AnticheatMatrix; @@ -168,6 +169,7 @@ public class EcoSpigotPlugin extends EcoPlugin { new DispenserArmorListener(), new EntityDeathByEntityListeners(this), new ShapedRecipeListener(), + new PlayerJumpListeners(), new GUIListener(this) ); } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/eventlisteners/PlayerJumpListeners.java b/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/eventlisteners/PlayerJumpListeners.java new file mode 100644 index 00000000..121db444 --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/eventlisteners/PlayerJumpListeners.java @@ -0,0 +1,62 @@ +package com.willfp.eco.spigot.eventlisteners; + +import com.willfp.eco.core.events.PlayerJumpEvent; +import com.willfp.eco.core.integrations.mcmmo.McmmoManager; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerMoveEvent; +import org.bukkit.potion.PotionEffectType; +import org.jetbrains.annotations.NotNull; + +import java.text.DecimalFormat; +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; + +@SuppressWarnings("deprecation") +public class PlayerJumpListeners implements Listener { + /** + * For jump listeners. + */ + private static final Set PREVIOUS_PLAYERS_ON_GROUND = new HashSet<>(); + + /** + * For jump listeners. + */ + private static final DecimalFormat FORMAT = new DecimalFormat("0.00"); + + /** + * Called when a player jumps. + * + * @param event The event to listen for. + */ + @EventHandler(ignoreCancelled = true) + public void onJump(@NotNull final PlayerMoveEvent event) { + if (McmmoManager.isFake(event)) { + return; + } + + Player player = event.getPlayer(); + if (player.getVelocity().getY() > 0) { + float jumpVelocity = 0.42f; + if (player.hasPotionEffect(PotionEffectType.JUMP)) { + jumpVelocity += ((float) player.getPotionEffect(PotionEffectType.JUMP).getAmplifier() + 1) * 0.1F; + } + jumpVelocity = Float.parseFloat(FORMAT.format(jumpVelocity).replace(',', '.')); + if (event.getPlayer().getLocation().getBlock().getType() != Material.LADDER + && PREVIOUS_PLAYERS_ON_GROUND.contains(player.getUniqueId()) + && !player.isOnGround() + && Float.compare((float) player.getVelocity().getY(), jumpVelocity) == 0) { + Bukkit.getPluginManager().callEvent(new PlayerJumpEvent(event)); + } + } + if (player.isOnGround()) { + PREVIOUS_PLAYERS_ON_GROUND.add(player.getUniqueId()); + } else { + PREVIOUS_PLAYERS_ON_GROUND.remove(player.getUniqueId()); + } + } +}