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:
parent
45da395418
commit
ad1bfc052c
@ -1,8 +1,10 @@
|
|||||||
package io.github.thebusybiscuit.slimefun4.implementation.listeners;
|
package io.github.thebusybiscuit.slimefun4.implementation.listeners;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
import org.bukkit.HeightMap;
|
import org.bukkit.HeightMap;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.entity.Entity;
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
@ -12,6 +14,8 @@ import org.bukkit.inventory.ItemStack;
|
|||||||
import org.bukkit.potion.PotionEffect;
|
import org.bukkit.potion.PotionEffect;
|
||||||
import org.bukkit.potion.PotionEffectType;
|
import org.bukkit.potion.PotionEffectType;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems;
|
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems;
|
||||||
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
|
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
|
||||||
import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils;
|
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.
|
* when nearing the ground while using the Bee Wings.
|
||||||
*
|
*
|
||||||
* @author beSnow
|
* @author beSnow
|
||||||
|
* @author Linox
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class BeeWingListener implements Listener {
|
public class BeeWingListener implements Listener {
|
||||||
|
|
||||||
public BeeWingListener(SlimefunPlugin plugin) {
|
public BeeWingListener(@Nonnull SlimefunPlugin plugin) {
|
||||||
plugin.getServer().getPluginManager().registerEvents(this, plugin);
|
plugin.getServer().getPluginManager().registerEvents(this, plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,37 +39,51 @@ public class BeeWingListener implements Listener {
|
|||||||
public void onApproachGround(PlayerMoveEvent e) {
|
public void onApproachGround(PlayerMoveEvent e) {
|
||||||
Player player = e.getPlayer();
|
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.isGliding()) return;
|
||||||
if (player.isOnGround()) return;
|
if (player.isOnGround()) return;
|
||||||
|
|
||||||
|
if (player.hasPotionEffect(PotionEffectType.SLOW_FALLING)) return;
|
||||||
|
|
||||||
ItemStack chestplate = player.getInventory().getChestplate();
|
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;
|
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)
|
// getDistanceToGround will only fire when playerDistanceToHighestBlock is negative (which happens when a player is flying under an existing structure)
|
||||||
if (playerDistanceToHighestBlock < 0) {
|
if (distanceToHighestBlock < 0) {
|
||||||
if (getDistanceToGround(player) > 6) return;
|
if (getDistanceToGround(loc) > 6) return;
|
||||||
player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_FALLING, 40, 0));
|
player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_FALLING, 40, 0));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (playerDistanceToHighestBlock <= 6) {
|
if (distanceToHighestBlock <= 6) {
|
||||||
player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_FALLING, 40, 0));
|
player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_FALLING, 40, 0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int getDistanceToGround(Entity e) {
|
/*
|
||||||
Location loc = e.getLocation().clone();
|
* Calculates the distance of the given {@link Location} from the ground.
|
||||||
double y = loc.getBlockY();
|
*
|
||||||
int distance = 0;
|
* @param loc
|
||||||
for (double i = y; i >= 0; i--) {
|
* 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);
|
loc.setY(i);
|
||||||
if (loc.getBlock().getType().isSolid()) break;
|
if (loc.getBlock().getType().isSolid()) {
|
||||||
distance++;
|
return y - i;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return distance;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user