1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 11:45:51 +00:00

Refactoring, requested changes and lots of improvement.

This commit is contained in:
LinoxGH 2020-09-16 21:16:47 +03:00 committed by GitHub
parent 45da395418
commit ad1bfc052c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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;
}
}