Skip to content

Commit

Permalink
helper improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Jan 21, 2024
1 parent 8285c13 commit fa18f91
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public class Config {
public final long max_tps_check_interval_millis;
public final int nether_ceiling_max_y;
public final boolean auto_lang, protocolLib_IsDisabled, connectionMsgsAreOnByDefault,
cmd_say_enabled, cmd_help_enabled, cmd_toggleConMsgs_enabled;
cmd_say_enabled, cmd_help_enabled, cmd_toggleConMsgs_enabled,
elytra_enable_at_spawn, elytra_enable_global, elytra_enable_netherceiling;

public Config() throws Exception {
// Create plugin folder first if it does not exist yet
Expand Down Expand Up @@ -63,6 +64,9 @@ public Config() throws Exception {
"Plays XP pickup sound to alert players when theyre going above the limit.");
config.addDefault("elytra.elytra-speed.teleport-instead-of-canceling-movement", false,
"Recommended to leave false if you dont experience any issues.");
this.elytra_enable_global = getBoolean("elytra.elytra-speed.Global-Settings.enable", true);
this.elytra_enable_at_spawn = getBoolean("elytra.elytra-speed.At-Spawn.enable", false);
this.elytra_enable_netherceiling = getBoolean("elytra.elytra-speed.Nether-Ceiling.enable", true);

// Misc
config.addDefault("misc.join-leave-messages.enable", true); // add default here so enable option shows up first.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void enable() {

@Override
public boolean shouldEnable() {
return AnarchyExploitFixes.getConfiguration().getBoolean("elytra.elytra-speed.At-Spawn.enable", false);
return AnarchyExploitFixes.getConfiguration().elytra_enable_at_spawn;
}

@Override
Expand Down Expand Up @@ -204,6 +204,4 @@ private void onPlayerMove(PlayerMoveEvent event) {
}
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void enable() {

@Override
public boolean shouldEnable() {
return AnarchyExploitFixes.getConfiguration().getBoolean("elytra.elytra-speed.Global-Settings.enable", true);
return AnarchyExploitFixes.getConfiguration().elytra_enable_global;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public class ElytraHelper implements AnarchyExploitFixesModule, Listener {
private static ElytraHelper instance;
private final AnarchyExploitFixes plugin;
private final Set<UUID> PLAYERS_NEAR_NEW_CHUNKS;
private final Cache<UUID, Double> PLAYER_SPEEDS_MOVE_EVENT;
private final Cache<UUID, Double> PLAYER_SPEEDS_INTERVAL;
private final Cache<UUID, Location> LAST_GLIDE_POS;
private ScheduledTask CHECK_SCHEDULER;
Expand All @@ -44,9 +43,8 @@ public ElytraHelper() {
"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 = tickInterval > 0 ? tickInterval : 1;
this.checkIntervalTicks = FastMath.max(tickInterval, 1);
final Duration cacheTime = Duration.ofMillis(FastMath.max(1000, (tickInterval * 50L) + 100L));
this.PLAYER_SPEEDS_MOVE_EVENT = Caffeine.newBuilder().expireAfterWrite(cacheTime).build();
this.PLAYER_SPEEDS_INTERVAL = Caffeine.newBuilder().expireAfterWrite(cacheTime).build();
this.LAST_GLIDE_POS = Caffeine.newBuilder().expireAfterWrite(cacheTime).build();
}
Expand All @@ -72,13 +70,13 @@ 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()) return;
Location currentLocation = player.getLocation().clone();
Location lastLocation = LAST_GLIDE_POS.getIfPresent(player.getUniqueId());
if (lastLocation == null)
lastLocation = currentLocation;
PLAYER_SPEEDS_INTERVAL.put(player.getUniqueId(), LocationUtil.getFlatDistance(lastLocation, currentLocation) / checkIntervalTicks);
LAST_GLIDE_POS.put(player.getUniqueId(), currentLocation);
if (player.isGliding()) {
Location currentLocation = player.getLocation().clone();
Location lastLocation = LAST_GLIDE_POS.getIfPresent(player.getUniqueId());
if (lastLocation == null) lastLocation = currentLocation;
PLAYER_SPEEDS_INTERVAL.put(player.getUniqueId(), LocationUtil.getFlatDistance(lastLocation, currentLocation) / checkIntervalTicks);
LAST_GLIDE_POS.put(player.getUniqueId(), currentLocation);
}
}, null);
}
}, checkIntervalTicks, checkIntervalTicks);
Expand All @@ -87,9 +85,7 @@ public void enable() {
@Override
public boolean shouldEnable() {
Config config = AnarchyExploitFixes.getConfiguration();
return config.getBoolean("elytra.elytra-speed.Global-Settings.enable", true)
|| config.getBoolean("elytra.elytra-speed.At-Spawn.enable", false)
|| config.getBoolean("elytra.elytra-speed.Nether-Ceiling.enable", true);
return config.elytra_enable_at_spawn || config.elytra_enable_global || config.elytra_enable_netherceiling;
}

@Override
Expand All @@ -98,13 +94,6 @@ public void disable() {
if (CHECK_SCHEDULER != null) CHECK_SCHEDULER.cancel();
}

@EventHandler(priority = EventPriority.LOWEST)
private void onPlayerMove(PlayerMoveEvent event) {
if (event.getPlayer().isGliding()) {
PLAYER_SPEEDS_MOVE_EVENT.put(event.getPlayer().getUniqueId(), LocationUtil.getFlatDistance(event.getFrom(), event.getTo()));
}
}

private double getFlatDistanceInChunks(Chunk chunk, Location location) {
return FastMath.hypot(chunk.getX() - location.getBlockX() >> 4, chunk.getZ() - location.getBlockZ() >> 4);
}
Expand All @@ -128,14 +117,12 @@ public Location getFrom(PlayerMoveEvent event) {
}

public double getBlocksPerTick(PlayerMoveEvent event) {
final Double speedMoveEvent = PLAYER_SPEEDS_MOVE_EVENT.getIfPresent(event.getPlayer().getUniqueId());
double eventSpeed = LocationUtil.getFlatDistance(event.getFrom(), event.getTo());
if (doIntervalCheck) {
final Double speedInterval = PLAYER_SPEEDS_INTERVAL.getIfPresent(event.getPlayer().getUniqueId());
if (speedInterval != null) {
return speedMoveEvent != null ? FastMath.max(speedInterval, speedMoveEvent) : speedInterval;
}
Double speedInterval = PLAYER_SPEEDS_INTERVAL.getIfPresent(event.getPlayer().getUniqueId());
if (speedInterval != null) return FastMath.max(speedInterval, eventSpeed);
}
return speedMoveEvent != null ? speedMoveEvent : LocationUtil.getFlatDistance(event.getFrom(), event.getTo());
return eventSpeed;
}

public boolean isInNewChunks(UUID playerUniqueId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void enable() {

@Override
public boolean shouldEnable() {
return AnarchyExploitFixes.getConfiguration().getBoolean("elytra.elytra-speed.Nether-Ceiling.enable", true);
return AnarchyExploitFixes.getConfiguration().elytra_enable_netherceiling;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import me.moomoo.anarchyexploitfixes.utils.LogUtil;
import org.bukkit.World;
import org.bukkit.entity.FallingBlock;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;

import java.util.logging.Level;

Expand Down Expand Up @@ -58,15 +59,17 @@ public void disable() {

private void run() {
for (World world : plugin.getServer().getWorlds()) {
for (FallingBlock fallingBlock : world.getEntitiesByClass(FallingBlock.class)) {
fallingBlock.getScheduler().run(plugin, killIfOld -> {
if (fallingBlock.getTicksLived() > max_alive_time) {
fallingBlock.remove();
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(), "Removed falling block at x:"
+ fallingBlock.getLocation().getX() + " y:" + fallingBlock.getLocation().getY() + " z:" + fallingBlock.getLocation().getZ()
+ " in " + fallingBlock.getWorld().getName() + " because it was alive for more than " + max_alive_time + " ticks.");
}
}, null);
for (Entity entity : world.getEntities()) {
if (entity.getType() == EntityType.FALLING_BLOCK) {
entity.getScheduler().run(plugin, killIfOld -> {
if (entity.getTicksLived() > max_alive_time) {
entity.remove();
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(), "Removed falling block at x:"
+ entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ()
+ " in " + entity.getWorld().getName() + " because it was alive for more than " + max_alive_time + " ticks.");
}
}, null);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public class Config {
public final long max_tps_check_interval_millis;
public final int nether_ceiling_max_y, nether_floor_min_y, overworld_floor_min_y;
public final boolean auto_lang, protocolLib_IsDisabled, connectionMsgsAreOnByDefault,
cmd_say_enabled, cmd_help_enabled, cmd_toggleConMsgs_enabled;
cmd_say_enabled, cmd_help_enabled, cmd_toggleConMsgs_enabled, elytra_enable_global,
elytra_enable_at_spawn, elytra_enable_netherceiling;

public Config() throws Exception {
// Create plugin folder first if it does not exist yet
Expand Down Expand Up @@ -64,6 +65,9 @@ public Config() throws Exception {
"Plays XP pickup sound to alert players when theyre going above the limit.");
config.addDefault("elytra.elytra-speed.teleport-instead-of-canceling-movement", false,
"Recommended to leave false if you dont experience any issues.");
this.elytra_enable_global = getBoolean("elytra.elytra-speed.Global-Settings.enable", true);
this.elytra_enable_at_spawn = getBoolean("elytra.elytra-speed.At-Spawn.enable", false);
this.elytra_enable_netherceiling = getBoolean("elytra.elytra-speed.Nether-Ceiling.enable", true);

// Misc
config.addDefault("misc.join-leave-messages.enable", true); // add default here so enable option shows up first.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void enable() {

@Override
public boolean shouldEnable() {
return AnarchyExploitFixes.getConfiguration().getBoolean("elytra.elytra-speed.At-Spawn.enable", false);
return AnarchyExploitFixes.getConfiguration().elytra_enable_at_spawn;
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void enable() {

@Override
public boolean shouldEnable() {
return AnarchyExploitFixes.getConfiguration().getBoolean("elytra.elytra-speed.Global-Settings.enable", true);
return AnarchyExploitFixes.getConfiguration().elytra_enable_global;
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
public class ElytraHelper implements AnarchyExploitFixesModule, Listener, Runnable {
private static ElytraHelper instance;
private final AnarchyExploitFixes plugin;
private final Cache<UUID, Double> PLAYER_SPEEDS_MOVE_EVENT;
private final Cache<UUID, Double> PLAYER_SPEEDS_INTERVAL;
private final Cache<UUID, Location> LAST_GLIDE_POS;
private final Set<UUID> PLAYERS_NEAR_NEW_CHUNKS;
Expand All @@ -41,9 +40,8 @@ public ElytraHelper() {
"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 = tickInterval > 0 ? tickInterval : 1;
this.checkIntervalTicks = FastMath.max(tickInterval, 1);
final Duration cacheTime = Duration.ofMillis(FastMath.max(1000, (tickInterval * 50L) + 100L));
this.PLAYER_SPEEDS_MOVE_EVENT = Caffeine.newBuilder().expireAfterWrite(cacheTime).build();
this.PLAYER_SPEEDS_INTERVAL = Caffeine.newBuilder().expireAfterWrite(cacheTime).build();
this.LAST_GLIDE_POS = Caffeine.newBuilder().expireAfterWrite(cacheTime).build();
}
Expand Down Expand Up @@ -71,9 +69,7 @@ public void enable() {
@Override
public boolean shouldEnable() {
Config config = AnarchyExploitFixes.getConfiguration();
return config.getBoolean("elytra.elytra-speed.Global-Settings.enable", true)
|| config.getBoolean("elytra.elytra-speed.At-Spawn.enable", false)
|| config.getBoolean("elytra.elytra-speed.Nether-Ceiling.enable", true);
return config.elytra_enable_at_spawn || config.elytra_enable_global || config.elytra_enable_netherceiling;
}

@Override
Expand All @@ -89,13 +85,6 @@ public void run() {
}
}

@EventHandler(priority = EventPriority.LOWEST)
private void onPlayerMove(PlayerMoveEvent event) {
if (event.getPlayer().isGliding()) {
PLAYER_SPEEDS_MOVE_EVENT.put(event.getPlayer().getUniqueId(), LocationUtil.getFlatDistance(event.getFrom(), event.getTo()));
}
}

private double getFlatDistanceInChunks(Chunk chunk, Location location) {
return FastMath.hypot(chunk.getX() - location.getBlockX() >> 4, chunk.getZ() - location.getBlockZ() >> 4);
}
Expand All @@ -119,14 +108,12 @@ public Location getFrom(PlayerMoveEvent event) {
}

public double getBlocksPerTick(PlayerMoveEvent event) {
final Double speedMoveEvent = PLAYER_SPEEDS_MOVE_EVENT.getIfPresent(event.getPlayer().getUniqueId());
double eventSpeed = LocationUtil.getFlatDistance(event.getFrom(), event.getTo());
if (doIntervalCheck) {
final Double speedInterval = PLAYER_SPEEDS_INTERVAL.getIfPresent(event.getPlayer().getUniqueId());
if (speedInterval != null) {
return speedMoveEvent != null ? FastMath.max(speedInterval, speedMoveEvent) : speedInterval;
}
Double speedInterval = PLAYER_SPEEDS_INTERVAL.getIfPresent(event.getPlayer().getUniqueId());
if (speedInterval != null) return FastMath.max(speedInterval, eventSpeed);
}
return speedMoveEvent != null ? speedMoveEvent : LocationUtil.getFlatDistance(event.getFrom(), event.getTo());
return eventSpeed;
}

public boolean isInNewChunks(UUID playerUniqueId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void enable() {

@Override
public boolean shouldEnable() {
return AnarchyExploitFixes.getConfiguration().getBoolean("elytra.elytra-speed.Nether-Ceiling.enable", true);
return AnarchyExploitFixes.getConfiguration().elytra_enable_netherceiling;
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
Expand Down

0 comments on commit fa18f91

Please sign in to comment.