-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add lectern-crash patch improve elytrafly old-new chunk calculation performance
- Loading branch information
Showing
22 changed files
with
635 additions
and
332 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 0 additions & 169 deletions
169
...itFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/elytra/ElytraHelper.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...olia/src/main/java/me/moomoo/anarchyexploitfixes/modules/elytra/helper/ChunkListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
...Folia/src/main/java/me/moomoo/anarchyexploitfixes/modules/elytra/helper/ElytraHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...olia/src/main/java/me/moomoo/anarchyexploitfixes/modules/elytra/helper/GlideListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
Oops, something went wrong.