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

Some refactoring and optimizations.

This commit is contained in:
LinoxGH 2020-09-17 15:16:24 +03:00 committed by GitHub
parent 2bad759178
commit a63f3eb886
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,8 +1,5 @@
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.Player; import org.bukkit.entity.Player;
@ -55,33 +52,35 @@ public class BeeWingListener implements Listener {
} }
Location loc = player.getLocation(); Location loc = player.getLocation();
double distanceToHighestBlock = (loc.getBlockY() - player.getWorld().getHighestBlockYAt(loc, HeightMap.WORLD_SURFACE)); int 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 (distanceToHighestBlock < 0) { if (distanceToHighestBlock < 0) {
if (getDistanceToGround(loc.clone()) > 6) return; int distanceToGround = getDistanceToGround(loc.getBlock(), 6);
if (distanceToGround < 1) return;
player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_FALLING, 40, 0)); player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_FALLING, 40, 0));
return; return;
} }
else if (distanceToHighestBlock <= 6) {
if (distanceToHighestBlock <= 6) {
player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_FALLING, 40, 0)); player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_FALLING, 40, 0));
} }
} }
/** /**
* Calculates the distance of the given {@link Location} from the ground. * Calculates the distance of the given {@link Block} from the ground.
* *
* @param loc * @param b
* The {@link Location} to calculate from. * The {@link Block} to calculate from.
* @param limit
* The limit of {@link Block blocks} to check under the given {@link Block b}.
* *
*/ */
private int getDistanceToGround(@Nonnull Location loc) { private int getDistanceToGround(@Nonnull Block b, int limit) {
int y = loc.getBlockY(); for (int i = 1; i <= limit; i++) {
for (int i = y; i >= 0; i--) { Block relative = b.getRelative(0, -i, 0);
loc.setY(i); if (relative.getType().isSolid()) {
if (loc.getBlock().getType().isSolid()) { return i;
return y - i;
} }
} }
return 0; return 0;