Skip to content

Commit

Permalink
Improve LocationUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Jan 25, 2024
1 parent d261abc commit 5849ddd
Show file tree
Hide file tree
Showing 23 changed files with 205 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import org.apache.commons.math3.util.FastMath;
import org.bukkit.World;
import org.bukkit.entity.ChestedHorse;
import org.bukkit.entity.LivingEntity;
Expand Down Expand Up @@ -64,8 +63,8 @@ private void onEntityDamage(EntityDamageEvent event) {
) {
if (
livingEntity.getHealth() - event.getDamage() <= 0
&& FastMath.round(livingEntity.getLocation().getX()) == 100
&& FastMath.round(livingEntity.getLocation().getZ()) == 0
&& Math.round(livingEntity.getLocation().getX()) == 100
&& Math.round(livingEntity.getLocation().getZ()) == 0
) {
livingEntity.getScheduler().run(plugin, kill -> livingEntity.remove(), null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ public void disable() {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onPlayerMove(PlayerMoveEvent event) {
Player player = event.getPlayer();
if (!player.isGliding()) return;
if (!ElytraHelper.getInstance().isGliding(player)) return;
if (!event.hasExplicitlyChangedPosition()) return;
if (spawn_shouldCheckPermission && player.hasPermission("anarchyexploitfixes.bypass")) return;
Location playerLoc = player.getLocation();
if (ceilingSettingsEnabled && LocationUtil.isNetherCeiling(playerLoc)) return;
if (LocationUtil.getFlatDistanceTo00(playerLoc) > spawn_Radius) return;
if (LocationUtil.getDistance2DTo00(playerLoc) > spawn_Radius) return;

if (spawn_DenyElytra) {
if (teleportBack) player.teleportAsync(ElytraHelper.getInstance().getFrom(event));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ public void disable() {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onPlayerMove(PlayerMoveEvent event) {
Player player = event.getPlayer();
if (!player.isGliding()) return;
if (!ElytraHelper.getInstance().isGliding(player)) return;
if (!event.hasExplicitlyChangedPosition()) return;
if (global_shouldCheckPermission && player.hasPermission("anarchyexploitfixes.bypass")) return;
Location playerLoc = player.getLocation();
if (ceiling_SettingsEnabled && LocationUtil.isNetherCeiling(playerLoc)) return;
if (spawn_SettingsEnabled && LocationUtil.getFlatDistanceTo00(playerLoc) <= spawn_Radius) return;
if (spawn_SettingsEnabled && LocationUtil.getDistance2DTo00(playerLoc) <= spawn_Radius) return;

if (global_DenyElytra) {
if (teleportBack) player.teleportAsync(ElytraHelper.getInstance().getFrom(event));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@
import me.moomoo.anarchyexploitfixes.config.Config;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import me.moomoo.anarchyexploitfixes.utils.LocationUtil;
import org.apache.commons.math3.util.FastMath;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityToggleGlideEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.util.NumberConversions;

import java.time.Duration;
import java.util.HashSet;
Expand All @@ -26,6 +30,7 @@
public class ElytraHelper implements AnarchyExploitFixesModule, Listener {
private static ElytraHelper instance;
private final AnarchyExploitFixes plugin;
private final Set<UUID> PLAYERS_GLIDING;
private final Set<UUID> PLAYERS_NEAR_NEW_CHUNKS;
private final Cache<UUID, Double> PLAYER_SPEEDS_INTERVAL;
private final Cache<UUID, Location> LAST_GLIDE_POS;
Expand All @@ -37,14 +42,15 @@ public class ElytraHelper implements AnarchyExploitFixesModule, Listener {
public ElytraHelper() {
instance = this;
this.plugin = AnarchyExploitFixes.getInstance();
this.PLAYERS_GLIDING = new HashSet<>();
this.PLAYERS_NEAR_NEW_CHUNKS = new HashSet<>(plugin.getServer().getOnlinePlayers().size());
Config config = AnarchyExploitFixes.getConfiguration();
this.doIntervalCheck = config.getBoolean("elytra.patch-generic-speedhacks.enable", true,
"Patches speed-limit bypass using generic speedhacks (Timer) by additionally checking player position every x ticks");
final int tickInterval = config.getInt("elytra.patch-generic-speedhacks.check-interval-in-ticks", 10,
"Lower value means more accuracy but also more overhead.");
this.checkIntervalTicks = FastMath.max(tickInterval, 1);
final Duration cacheTime = Duration.ofMillis(FastMath.max(1000, (tickInterval * 50L) + 100L));
this.checkIntervalTicks = Math.max(tickInterval, 1);
final Duration cacheTime = Duration.ofMillis(Math.max(1000, (tickInterval * 50L) + 100L));
this.PLAYER_SPEEDS_INTERVAL = Caffeine.newBuilder().expireAfterWrite(cacheTime).build();
this.LAST_GLIDE_POS = Caffeine.newBuilder().expireAfterWrite(cacheTime).build();
}
Expand All @@ -70,11 +76,11 @@ public void enable() {
CHECK_SCHEDULER = plugin.getServer().getGlobalRegionScheduler().runAtFixedRate(plugin, scheduleChecks -> {
for (Player player : plugin.getServer().getOnlinePlayers()) {
player.getScheduler().run(plugin, checkFlight -> {
if (player.isGliding()) {
if (this.isGliding(player)) {
Location currentLocation = player.getLocation().clone();
Location lastLocation = LAST_GLIDE_POS.getIfPresent(player.getUniqueId());
if (lastLocation != null) PLAYER_SPEEDS_INTERVAL.put(player.getUniqueId(),
LocationUtil.getNormalizedDistance(lastLocation, currentLocation) / checkIntervalTicks);
LocationUtil.getRelDistance3D(lastLocation, currentLocation) / checkIntervalTicks);
LAST_GLIDE_POS.put(player.getUniqueId(), currentLocation);
}
}, null);
Expand All @@ -94,14 +100,42 @@ public void disable() {
if (CHECK_SCHEDULER != null) CHECK_SCHEDULER.cancel();
}

private double getFlatDistanceInChunks(Chunk chunk, Location location) {
return FastMath.hypot(chunk.getX() - location.getBlockX() >> 4, chunk.getZ() - location.getBlockZ() >> 4);
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
private void onGlideToggle(EntityToggleGlideEvent event) {
if (event.getEntityType() != EntityType.PLAYER) return;

if (event.isGliding()) {
PLAYERS_GLIDING.add(event.getEntity().getUniqueId());
} else {
PLAYERS_GLIDING.remove(event.getEntity().getUniqueId());
}
}

@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
private void onJoin(PlayerQuitEvent event) {
PLAYERS_GLIDING.remove(event.getPlayer().getUniqueId());
}

@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
private void onJoin(PlayerJoinEvent event) {
if (event.getPlayer().isGliding()) {
PLAYERS_GLIDING.add(event.getPlayer().getUniqueId());
} else {
PLAYERS_GLIDING.remove(event.getPlayer().getUniqueId());
}
}

private double getChunkDistance(Chunk chunk, Location location) {
return Math.sqrt(
NumberConversions.square(chunk.getX() - location.getBlockX() >> 4) +
NumberConversions.square(chunk.getZ() - location.getBlockZ() >> 4)
);
}

@EventHandler(priority = EventPriority.LOW)
private void onChunkLoad(ChunkLoadEvent event) {
for (Player player : event.getWorld().getPlayers()) {
if (this.getFlatDistanceInChunks(event.getChunk(), player.getLocation().clone()) < player.getViewDistance()) {
if (this.getChunkDistance(event.getChunk(), player.getLocation().clone()) < player.getViewDistance()) {
if (event.isNewChunk()) {
PLAYERS_NEAR_NEW_CHUNKS.add(player.getUniqueId());
} else {
Expand All @@ -117,15 +151,19 @@ public Location getFrom(PlayerMoveEvent event) {
}

public double getBlocksPerTick(PlayerMoveEvent event) {
double eventSpeed = LocationUtil.getNormalizedDistance(event.getTo(), event.getFrom());
double eventSpeed = LocationUtil.getRelDistance3D(event.getTo(), event.getFrom());
if (doIntervalCheck) {
Double speedInterval = PLAYER_SPEEDS_INTERVAL.getIfPresent(event.getPlayer().getUniqueId());
if (speedInterval != null) return FastMath.max(speedInterval, eventSpeed);
if (speedInterval != null) return Math.max(speedInterval, eventSpeed);
}
return eventSpeed;
}

public boolean isInNewChunks(UUID playerUniqueId) {
return PLAYERS_NEAR_NEW_CHUNKS.contains(playerUniqueId);
}

public boolean isGliding(Player player) {
return PLAYERS_GLIDING.contains(player.getUniqueId()) || player.isGliding();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void disable() {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onPlayerMove(PlayerMoveEvent event) {
Player player = event.getPlayer();
if (!player.isGliding()) return;
if (!ElytraHelper.getInstance().isGliding(player)) return;
if (!event.hasExplicitlyChangedPosition()) return;
if (ceiling_shouldCheckPermission && player.hasPermission("anarchyexploitfixes.bypass")) return;
Location playerLoc = player.getLocation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import me.moomoo.anarchyexploitfixes.config.Config;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import me.moomoo.anarchyexploitfixes.utils.LogUtil;
import org.apache.commons.math3.util.FastMath;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
Expand Down Expand Up @@ -39,7 +38,7 @@ public InventoryActionLag() {
Prevent lag generated by players quickly moving big items back and forth between inventories.\s
Uses cached counters that auto-reset after the configurable time in milliseconds.""");
this.logIsEnabled = config.getBoolean("lag-preventions.prevent-inventory-action-lag.log", true);
Duration cacheTime = Duration.ofMillis(FastMath.max(config.getInt("lag-preventions.prevent-inventory-action-lag.cache-time-millis", 2000,
Duration cacheTime = Duration.ofMillis(Math.max(config.getInt("lag-preventions.prevent-inventory-action-lag.cache-time-millis", 2000,
"The amount of time in milliseconds an entry is kept after writing."), 1));
this.blockInventoryClicks = Caffeine.newBuilder().expireAfterWrite(cacheTime).build();
this.entityInventoryClicks = Caffeine.newBuilder().expireAfterWrite(cacheTime).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private void tempVanish(Player player) {
private void onTeleport(PlayerTeleportEvent event) {
switch (event.getCause()) {
case ENDER_PEARL, COMMAND, PLUGIN -> {
if (LocationUtil.getFlatDistance(event.getFrom(), event.getTo()) >= minDistanceToVanishPlayers) {
if (LocationUtil.getRelDistance2D(event.getFrom(), event.getTo()) >= minDistanceToVanishPlayers) {
this.tempVanish(event.getPlayer());
}
}
Expand All @@ -84,7 +84,7 @@ private void onTeleport(PlayerTeleportEvent event) {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onSpawn(PlayerRespawnEvent event) {
final Player player = event.getPlayer();
if (LocationUtil.getFlatDistance(player.getLocation(), event.getRespawnLocation()) >= minDistanceToVanishPlayers) {
if (LocationUtil.getRelDistance2D(player.getLocation(), event.getRespawnLocation()) >= minDistanceToVanishPlayers) {
this.tempVanish(player);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import me.moomoo.anarchyexploitfixes.utils.LogUtil;
import me.moomoo.anarchyexploitfixes.utils.models.ExpiringSet;
import org.apache.commons.math3.util.FastMath;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
Expand All @@ -25,7 +24,7 @@ public WorldChangeCrash() {
shouldEnable();
Config config = AnarchyExploitFixes.getConfiguration();
this.recentWorldChangers = new ExpiringSet<>(Duration.ofMillis(
FastMath.max(config.getInt("patches.crash-exploits.prevent-fast-world-teleport-crash.teleport-delay-millis", 1000,
Math.max(config.getInt("patches.crash-exploits.prevent-fast-world-teleport-crash.teleport-delay-millis", 1000,
"Time in milliseconds until an entity can teleport to another world again."), 1)));
config.addComment("patches.crash-exploits.prevent-fast-world-teleport-crash.enable",
"Prevents crash methods that involve very fast teleporting between different worlds in a short time.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ private void onPistonExplode(EntityExplodeEvent event) {

private boolean isWithinEndProtectedRadius(Location location) {
if (!location.getWorld().getEnvironment().equals(World.Environment.THE_END)) return false;
return LocationUtil.getFlatDistanceTo00(location) <= endBedrockProtectRadius;
return LocationUtil.getDistance2DTo00(location) <= endBedrockProtectRadius;
}

private boolean isEndPortal(Material material) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private void onCreatureSpawn(CreatureSpawnEvent event) {

final Integer disabledRadius = worldsAndTheirRadiuses.get(world);
Location witherLocation = wither.getLocation();
if (LocationUtil.getFlatDistanceTo00(witherLocation) > disabledRadius) return;
if (LocationUtil.getDistance2DTo00(witherLocation) > disabledRadius) return;

event.setCancelled(true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
import org.apache.commons.math3.util.FastMath;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.util.NumberConversions;

public class LocationUtil {

public static double getFlatDistanceTo00(Location location) {
return FastMath.hypot(location.x(), location.z());
public static boolean isNetherCeiling(Location location) {
return location.getWorld().getEnvironment() == World.Environment.NETHER
&& location.y() > AnarchyExploitFixes.getConfiguration().nether_ceiling_max_y;
}

public static double getFlatDistance(Location from, Location to) {
public static double getDistance2DTo00(Location location) {
return FastMath.hypot(location.getX(), location.getZ());
}

public static double getRelDistance2D(Location from, Location to) {
double toX = to.x();
double toZ = to.z();
double fromX = from.x();
Expand All @@ -34,7 +38,7 @@ public static double getFlatDistance(Location from, Location to) {
return FastMath.hypot(toX - fromX, toZ - fromZ);
}

public static double getNormalizedDistance(Location from, Location to) {
public static double getRelDistance3D(Location from, Location to) {
double toX = to.x();
double toZ = to.z();
double fromX = from.x();
Expand All @@ -53,13 +57,39 @@ public static double getNormalizedDistance(Location from, Location to) {
}
}

return Math.sqrt(NumberConversions.square(fromX - toX)
+ NumberConversions.square(from.getY() - to.getY())
+ NumberConversions.square(fromZ - toZ));
return getDistance3D(fromX - toX, from.y() - to.y(), fromZ - toZ);
}

public static boolean isNetherCeiling(Location location) {
return location.getWorld().getEnvironment() == World.Environment.NETHER
&& location.y() > AnarchyExploitFixes.getConfiguration().nether_ceiling_max_y;
public static double getDistance3D(final double x, final double y, final double z) {
if (Double.isInfinite(x) || Double.isInfinite(y) || Double.isInfinite(z)) {
return Double.POSITIVE_INFINITY;
} else if (Double.isNaN(x) || Double.isNaN(y) || Double.isNaN(z)) {
return Double.NaN;
} else {
final int expX = FastMath.getExponent(x);
final int expY = FastMath.getExponent(y);
final int expZ = FastMath.getExponent(z);

if (expX > expY + 27 && expX > expZ + 27) {
// x is neglectible with respect to y and z
return FastMath.abs(y) + FastMath.abs(z);
} else if (expY > expX + 27 && expY > expZ + 27) {
// y is neglectible with respect to x and z
return FastMath.abs(x) + FastMath.abs(z);
} else if (expZ > expX + 27 && expZ > expY + 27) {
// z is neglectible with respect to x and y
return FastMath.abs(x) + FastMath.abs(y);
} else {
final int middleExp = (expX + expY + expZ) / 3;

final double scaledX = FastMath.scalb(x, -middleExp);
final double scaledY = FastMath.scalb(y, -middleExp);
final double scaledZ = FastMath.scalb(z, -middleExp);

final double scaledH = Math.sqrt(scaledX * scaledX + scaledY * scaledY + scaledZ * scaledZ);

return FastMath.scalb(scaledH, middleExp);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import org.apache.commons.math3.util.FastMath;
import org.bukkit.World;
import org.bukkit.entity.ChestedHorse;
import org.bukkit.entity.Entity;
Expand Down Expand Up @@ -59,8 +58,8 @@ private void onEntityDamage(EntityDamageEvent event) {
) {
if (
livingEntity.getHealth() - event.getDamage() <= 0
&& FastMath.round(livingEntity.getLocation().getX()) == 100
&& FastMath.round(livingEntity.getLocation().getZ()) == 0
&& Math.round(livingEntity.getLocation().getX()) == 100
&& Math.round(livingEntity.getLocation().getZ()) == 0
) {
livingEntity.remove();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ public boolean shouldEnable() {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onPlayerMove(PlayerMoveEvent event) {
Player player = event.getPlayer();
if (!player.isGliding()) return;
if (!ElytraHelper.getInstance().isGliding(player)) return;
if (spawn_shouldCheckPermission && player.hasPermission("anarchyexploitfixes.bypass")) return;
Location playerLoc = player.getLocation();
if (ceilingSettingsEnabled && LocationUtil.isNetherCeiling(playerLoc)) return;
if (LocationUtil.getFlatDistanceTo00(playerLoc) > spawn_Radius) return;
if (LocationUtil.getDistance2DTo00(playerLoc) > spawn_Radius) return;

if (spawn_DenyElytra) {
if (teleportBack) player.teleport(ElytraHelper.getInstance().getFrom(event));
Expand Down
Loading

0 comments on commit 5849ddd

Please sign in to comment.