Skip to content

Commit

Permalink
improve window-click-crash patch
Browse files Browse the repository at this point in the history
add lectern-crash patch
improve elytrafly old-new chunk calculation performance
  • Loading branch information
xGinko committed Jan 26, 2024
1 parent ab0c2ac commit ac6a267
Show file tree
Hide file tree
Showing 22 changed files with 635 additions and 332 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import me.moomoo.anarchyexploitfixes.modules.combat.*;
import me.moomoo.anarchyexploitfixes.modules.dupepreventions.*;
import me.moomoo.anarchyexploitfixes.modules.elytra.*;
import me.moomoo.anarchyexploitfixes.modules.elytra.helper.ElytraHelper;
import me.moomoo.anarchyexploitfixes.modules.illegals.items.RemoveSpecificItemNames;
import me.moomoo.anarchyexploitfixes.modules.illegals.items.RevertOverstacked;
import me.moomoo.anarchyexploitfixes.modules.illegals.items.RevertUnbreakables;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import me.moomoo.anarchyexploitfixes.config.Config;
import me.moomoo.anarchyexploitfixes.config.LanguageCache;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import me.moomoo.anarchyexploitfixes.modules.elytra.helper.ElytraHelper;
import me.moomoo.anarchyexploitfixes.utils.LocationUtil;
import me.moomoo.anarchyexploitfixes.utils.MaterialUtil;
import net.kyori.adventure.text.Component;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import me.moomoo.anarchyexploitfixes.config.Config;
import me.moomoo.anarchyexploitfixes.config.LanguageCache;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import me.moomoo.anarchyexploitfixes.modules.elytra.helper.ElytraHelper;
import me.moomoo.anarchyexploitfixes.utils.LocationUtil;
import me.moomoo.anarchyexploitfixes.utils.MaterialUtil;
import net.kyori.adventure.text.Component;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import me.moomoo.anarchyexploitfixes.config.Config;
import me.moomoo.anarchyexploitfixes.config.LanguageCache;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import me.moomoo.anarchyexploitfixes.modules.elytra.helper.ElytraHelper;
import me.moomoo.anarchyexploitfixes.utils.LocationUtil;
import me.moomoo.anarchyexploitfixes.utils.MaterialUtil;
import net.kyori.adventure.text.Component;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package me.moomoo.anarchyexploitfixes.modules.elytra.helper;

import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.util.NumberConversions;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

public class ChunkListener extends ElytraHelper implements Listener {

public final Set<UUID> PLAYERS_NEAR_NEW_CHUNKS;

public ChunkListener() {
this.PLAYERS_NEAR_NEW_CHUNKS = new HashSet<>();
}

@EventHandler(priority = EventPriority.LOWEST)
private void onChunkLoad(ChunkLoadEvent event) {
for (Player player : event.getWorld().getPlayers()) {
if (this.getChunkDistanceSquared(event.getChunk(), player.getLocation().clone()) < NumberConversions.square(player.getViewDistance())) {
if (event.isNewChunk()) {
this.PLAYERS_NEAR_NEW_CHUNKS.add(player.getUniqueId());
} else {
this.PLAYERS_NEAR_NEW_CHUNKS.remove(player.getUniqueId());
}
}
}
}

/**
* Since the distance here is only used to see whether a chunk is loaded roughly within the player's view distance,
* we can resort to comparing squared distances.
* This saves cpu usage as we don't have to use {@link Math#sqrt(double)} to get the accurate distance in chunks.
*/
private double getChunkDistanceSquared(Chunk chunk, Location location) {
return NumberConversions.square(chunk.getX() - location.getBlockX() >> 4) + NumberConversions.square(chunk.getZ() - location.getBlockZ() >> 4);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package me.moomoo.anarchyexploitfixes.modules.elytra.helper;

import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.config.Config;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import me.moomoo.anarchyexploitfixes.utils.LocationUtil;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerMoveEvent;

import java.util.UUID;

public class ElytraHelper implements AnarchyExploitFixesModule {
private static ElytraHelper instance;
public static final double SPEED_TOLERANCE = 0.02;

protected final AnarchyExploitFixes plugin;
private GlideListener glideListener;
private ChunkListener chunkListener;
private IntervalCheck intervalCheck;

public ElytraHelper() {
instance = this;
this.plugin = AnarchyExploitFixes.getInstance();
}

public static ElytraHelper getInstance() {
return instance;
}

@Override
public String name() {
return "elytra-helper";
}

@Override
public String category() {
return "elytra";
}

@Override
public void enable() {
glideListener = new GlideListener();
plugin.getServer().getPluginManager().registerEvents(glideListener, plugin);
chunkListener = new ChunkListener();
plugin.getServer().getPluginManager().registerEvents(chunkListener, plugin);
intervalCheck = new IntervalCheck();
intervalCheck.startIfEnabled();
}

@Override
public boolean shouldEnable() {
Config config = AnarchyExploitFixes.getConfiguration();
return config.elytra_enable_at_spawn || config.elytra_enable_global || config.elytra_enable_netherceiling;
}

@Override
public void disable() {
HandlerList.unregisterAll(glideListener);
HandlerList.unregisterAll(chunkListener);
intervalCheck.stopIfEnabled();
}

public Location getFrom(PlayerMoveEvent event) {
final Location lastGlidePos = intervalCheck.LAST_GLIDE_POS.getIfPresent(event.getPlayer().getUniqueId());
return lastGlidePos != null ? lastGlidePos : event.getFrom();
}

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

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

public boolean isGliding(Player player) {
return glideListener.PLAYERS_GLIDING.contains(player.getUniqueId()) || player.isGliding();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package me.moomoo.anarchyexploitfixes.modules.elytra.helper;

import org.bukkit.Bukkit;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityToggleGlideEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

public class GlideListener extends ElytraHelper implements Listener {

public final Set<UUID> PLAYERS_GLIDING;

public GlideListener() {
this.PLAYERS_GLIDING = Bukkit.getOnlinePlayers().stream()
.filter(LivingEntity::isGliding)
.map(Player::getUniqueId)
.collect(Collectors.toCollection(HashSet::new));
}

@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
private void onGlideToggle(EntityToggleGlideEvent event) {
if (event.getEntityType() == EntityType.PLAYER) {
if (event.isGliding()) {
this.PLAYERS_GLIDING.add(event.getEntity().getUniqueId());
} else {
this.PLAYERS_GLIDING.remove(event.getEntity().getUniqueId());
}
}
}

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

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

0 comments on commit ac6a267

Please sign in to comment.