diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BeeWingListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BeeWingListener.java index cf36ec47f..6063add5a 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BeeWingListener.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/BeeWingListener.java @@ -1,8 +1,10 @@ package io.github.thebusybiscuit.slimefun4.implementation.listeners; +import java.util.HashMap; +import java.util.UUID; + import org.bukkit.HeightMap; import org.bukkit.Location; -import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -12,6 +14,8 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; +import javax.annotation.Nonnull; + import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils; @@ -22,11 +26,12 @@ import me.mrCookieSlime.Slimefun.api.Slimefun; * when nearing the ground while using the Bee Wings. * * @author beSnow + * @author Linox * */ public class BeeWingListener implements Listener { - public BeeWingListener(SlimefunPlugin plugin) { + public BeeWingListener(@Nonnull SlimefunPlugin plugin) { plugin.getServer().getPluginManager().registerEvents(this, plugin); } @@ -34,37 +39,51 @@ public class BeeWingListener implements Listener { public void onApproachGround(PlayerMoveEvent e) { Player player = e.getPlayer(); + // Checking if the player is even falling. + if (e.getTo().getBlockY() == e.getFrom().getBlockY()) { + return; + } + if (!player.isGliding()) return; if (player.isOnGround()) return; + if (player.hasPotionEffect(PotionEffectType.SLOW_FALLING)) return; + ItemStack chestplate = player.getInventory().getChestplate(); - if (!SlimefunUtils.isItemSimilar(chestplate, SlimefunItems.BEE_WINGS, true) && !Slimefun.hasUnlocked(player, chestplate, true)) { + if (!SlimefunUtils.isItemSimilar(chestplate, SlimefunItems.BEE_WINGS, true) || !Slimefun.hasUnlocked(player, chestplate, true)) { return; } - double playerDistanceToHighestBlock = (player.getLocation().getY() - player.getWorld().getHighestBlockYAt(player.getLocation(), HeightMap.WORLD_SURFACE)); + Location loc = player.getLocation(); + double distanceToHighestBlock = (loc.getBlockY() - player.getWorld().getHighestBlockYAt(loc, HeightMap.WORLD_SURFACE)); // getDistanceToGround will only fire when playerDistanceToHighestBlock is negative (which happens when a player is flying under an existing structure) - if (playerDistanceToHighestBlock < 0) { - if (getDistanceToGround(player) > 6) return; + if (distanceToHighestBlock < 0) { + if (getDistanceToGround(loc) > 6) return; player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_FALLING, 40, 0)); return; } - if (playerDistanceToHighestBlock <= 6) { + if (distanceToHighestBlock <= 6) { player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_FALLING, 40, 0)); } } - private static int getDistanceToGround(Entity e) { - Location loc = e.getLocation().clone(); - double y = loc.getBlockY(); - int distance = 0; - for (double i = y; i >= 0; i--) { + /* + * Calculates the distance of the given {@link Location} from the ground. + * + * @param loc + * The {@link Location} to calculate from. + * + */ + private int getDistanceToGround(@Nonnull Location loc) { + int y = loc.getBlockY(); + for (int i = y; i >= 0; i--) { loc.setY(i); - if (loc.getBlock().getType().isSolid()) break; - distance++; + if (loc.getBlock().getType().isSolid()) { + return y - i; + } } - return distance; + return 0; } }