Skip to content

Commit

Permalink
revisit a bunch of modules for some improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Feb 17, 2024
1 parent bc9e7dd commit 4c61acf
Show file tree
Hide file tree
Showing 70 changed files with 531 additions and 565 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.stream.Collectors;

public class CommandWhitelist implements AnarchyExploitFixesModule, Listener {

Expand All @@ -28,8 +29,7 @@ public class CommandWhitelist implements AnarchyExploitFixesModule, Listener {
* Code was mainly only implemented into AEF by xGinko.
*/

private final Set<String> allowedCommands = new HashSet<>();
private final Set<String> bannedSubCommands;
private final Set<String> allowedCommands, bannedSubCommands;
private final boolean shouldUseProtocolLib, shouldLog;

public CommandWhitelist() {
Expand All @@ -42,12 +42,14 @@ public CommandWhitelist() {
"Will show logs when a command was denied.");
this.shouldUseProtocolLib = config.getBoolean("chat.command-whitelist.use-protocollib", false,
"Enable only if you have problems with the regular commandwhitelist. Otherwise not recommended to use.");
config.getList("chat.command-whitelist.whitelisted-commands", List.of(
"help", "vote", "kill", "discord", "togglechat", "toggleconnectionmsgs", "toggletells", "togglewhispering", "toggleprivatemsgs",
"ignore", "ignorelist", "ignorehard", "toggledeathmsg", "dmt", "worldstats", "stats", "tps", "msg", "whisper", "w", "m", "t",
"pm", "tell", "r", "reply", "last"
), "Add all commands you want your players to be able to access (without the '/'). Not case sensitive."
).forEach(configuredAllowedCmd -> this.allowedCommands.add(configuredAllowedCmd.toLowerCase()));
this.allowedCommands = config.getList("chat.command-whitelist.whitelisted-commands", List.of(
"help", "vote", "kill", "discord", "togglechat", "toggleconnectionmsgs", "toggletells", "togglewhispering", "toggleprivatemsgs",
"ignore", "ignorelist", "ignorehard", "toggledeathmsg", "dmt", "worldstats", "stats", "tps", "msg", "whisper", "w", "m", "t",
"pm", "tell", "r", "reply", "last"
), "Add all commands you want your players to be able to access (without the '/'). Not case sensitive.")
.stream()
.map(String::toLowerCase)
.collect(Collectors.toSet());
bannedSubCommands = new HashSet<>(config.getList("chat.command-whitelist.blacklisted-subcommands", List.of(
"help about", "vote List", "vote Best", "vote Total", "worldstats reload", "stats reload"
)));
Expand Down Expand Up @@ -123,7 +125,7 @@ private void onCommandPreProcess(PlayerCommandPreprocessEvent event) {
@EventHandler(priority = EventPriority.NORMAL)
private void onInitialTabCompleteListSend(PlayerCommandSendEvent event) {
if (event.getPlayer().hasPermission("anarchyexploitfixes.commandwhitelistbypass")) return;
event.getCommands().removeIf((cmd) -> !allowedCommands.contains(cmd));
event.getCommands().removeIf(cmd -> !allowedCommands.contains(cmd));
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
import io.github.thatsmusic99.configurationmaster.api.ConfigSection;
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import me.moomoo.anarchyexploitfixes.utils.ChunkUtil;
import me.moomoo.anarchyexploitfixes.utils.LogUtil;
import org.bukkit.Chunk;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockCanBuildEvent;
import org.bukkit.event.block.BlockPlaceEvent;

import java.util.HashMap;
Expand Down Expand Up @@ -166,20 +165,28 @@ public void disable() {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onBlockPlace(BlockPlaceEvent event) {
final Material placedType = event.getBlock().getType();
if (!blockLimits.containsKey(placedType)) return;

if (ChunkUtil.getMaterialCount(event.getBlock().getChunk(), placedType) > blockLimits.get(placedType)) {
if (
blockLimits.containsKey(placedType)
&& exceedsPerChunkLimit(placedType, blockLimits.get(placedType), event.getBlock().getChunk())
) {
event.setCancelled(true);
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onBlockPlace(BlockCanBuildEvent event) {
final Material placedType = event.getMaterial();
if (!blockLimits.containsKey(placedType)) return;

if (ChunkUtil.getMaterialCount(event.getBlock().getChunk(), placedType) > blockLimits.get(placedType)) {
event.setBuildable(false);
private boolean exceedsPerChunkLimit(Material material, int limit, Chunk chunk) {
final int minY = chunk.getWorld().getMinHeight();
final int maxY = chunk.getWorld().getMaxHeight();
int count = 0;
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
for (int y = minY; y < maxY; y++) {
if (chunk.getBlock(x, y, z).getType() == material) {
count++;
if (count > limit) return true;
}
}
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.config.Config;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import me.moomoo.anarchyexploitfixes.utils.LocationUtil;
import me.moomoo.anarchyexploitfixes.utils.LogUtil;
import org.bukkit.Chunk;
import org.bukkit.World;
Expand Down Expand Up @@ -173,9 +174,9 @@ private void onSpawn(EntitySpawnEvent event) {

entity.getScheduler().run(plugin, kill -> {
entity.remove();
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(), "Removed entity " + entity.getType()
+ " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ()
+ " in "+entity.getWorld().getName()+" because reached limit of " + maxAllowedPerChunk);
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(), "Removed entity " +
entity.getType() + " at " + LocationUtil.toString(entity.getLocation()) +
" because reached limit of " + maxAllowedPerChunk);
}, null);
}
}
Expand All @@ -201,9 +202,9 @@ private void run() {

entity.getScheduler().run(plugin, kill -> {
entity.remove();
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(), "Removed entity " + entity.getType()
+ " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ()
+ " in "+entity.getWorld().getName()+" because reached limit of " + maxAllowedPerChunk);
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(), "Removed entity " +
entity.getType() + " at " + LocationUtil.toString(entity.getLocation()) +
" because reached limit of " + maxAllowedPerChunk);
}, null);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.config.Config;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import me.moomoo.anarchyexploitfixes.utils.LocationUtil;
import me.moomoo.anarchyexploitfixes.utils.LogUtil;
import org.bukkit.Chunk;
import org.bukkit.Material;
Expand Down Expand Up @@ -100,9 +101,8 @@ private void onChunkLoad(ChunkLoadEvent event) {

entity.getScheduler().run(plugin, kill -> {
entity.remove();
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(), "Removed dropped item at"
+ " x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ()
+ " in world " + entity.getWorld().getName() + ", because reached limit of " + maxDroppedItemsPerChunk);
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(), "Removed dropped item at " +
LocationUtil.toString(entity.getLocation()) + " because reached limit of " + maxDroppedItemsPerChunk);
}, null);
}
}
Expand All @@ -120,9 +120,8 @@ private void onItemDrop(ItemSpawnEvent event) {

entity.getScheduler().run(plugin, kill -> {
entity.remove();
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(), "Removed dropped item at"
+ " x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ()
+ " in world " + entity.getWorld().getName() + ", because reached limit of " + maxDroppedItemsPerChunk);
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(), "Removed dropped item at " +
LocationUtil.toString(entity.getLocation()) + " because reached limit of " + maxDroppedItemsPerChunk);
}, null);
}
}
Expand All @@ -144,9 +143,8 @@ private void run() {

entity.getScheduler().run(plugin, kill -> {
entity.remove();
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(), "Removed dropped item at"
+ " x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ()
+ " in world " + entity.getWorld().getName() + ", because reached limit of " + maxDroppedItemsPerChunk);
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(), "Removed dropped item at " +
LocationUtil.toString(entity.getLocation()) + " because reached limit of " + maxDroppedItemsPerChunk);
}, null);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.config.Config;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import me.moomoo.anarchyexploitfixes.utils.LocationUtil;
import me.moomoo.anarchyexploitfixes.utils.LogUtil;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
Expand Down Expand Up @@ -68,9 +69,8 @@ private void onExpBottle(ExpBottleEvent event) {
if (expBottleCount > maxExpBottlePerChunk) {
entity.getScheduler().run(plugin, kill -> {
entity.remove();
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(), "Removed XP-Bottle "
+ " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY()
+ " z:" + entity.getLocation().getZ() + ", because reached limit of " + maxExpBottlePerChunk);
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(), "Removed XP-Bottle at "
+ LocationUtil.toString(entity.getLocation()) + " because reached limit of " + maxExpBottlePerChunk);
}, null);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.config.Config;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import me.moomoo.anarchyexploitfixes.utils.LocationUtil;
import me.moomoo.anarchyexploitfixes.utils.LogUtil;
import me.moomoo.anarchyexploitfixes.utils.models.ExpiringSet;
import org.bukkit.Chunk;
Expand Down Expand Up @@ -34,9 +35,9 @@ public FallingBlockLimit() {
this.logIsEnabled = config.getBoolean("chunk-limits.falling-block-limit.log", false);
this.maxFallingGravityBlockPerChunk = config.getInt("chunk-limits.falling-block-limit.max-falling-gravitiy-blocks-per-chunk", 60,
"Removes any falling block if there is more than x blocks (actively) falling in a chunk.");
final long chunkCheckDelay = config.getInt("chunk-limits.falling-block-limit.chunk-check-delay-in-ticks", 20, """
final long chunkCheckDelay = Math.max(config.getInt("chunk-limits.falling-block-limit.chunk-check-delay-in-ticks", 20, """
Delay in ticks until the same chunk can be checked again.\s
Prevents overchecking, because a physics event can be called multiple times for the same chunk.""") * 50L;
Prevents overchecking, because a physics event can be called multiple times for the same chunk."""), 1) * 50L;
this.checkedChunks = new ExpiringSet<>(Duration.ofMillis(chunkCheckDelay));
}

Expand Down Expand Up @@ -83,12 +84,11 @@ private void onBlockPhysics(BlockPhysicsEvent event) {
}
}

if (logIsEnabled && removed_falling) LogUtil.moduleLog(Level.INFO, name(),
"Removed falling blocks at " + event.getSourceBlock().getLocation()
+ ", because reached limit of " + maxFallingGravityBlockPerChunk + " falling gravity blocks per chunk"
);

checkedChunks.add(chunk);

if (logIsEnabled && removed_falling) LogUtil.moduleLog(Level.INFO, name(),
"Removed falling blocks at " + LocationUtil.toString(event.getSourceBlock().getLocation())
+ " because reached limit of " + maxFallingGravityBlockPerChunk + " falling gravity blocks per chunk");
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
Expand All @@ -110,11 +110,10 @@ private void onChangeBlock(EntityChangeBlockEvent event) {
}
}

if (logIsEnabled && removed_falling) LogUtil.moduleLog(Level.INFO, name(),
"Removed falling blocks at " + event.getBlock().getLocation()
+ ", because reached limit of " + maxFallingGravityBlockPerChunk + " falling gravity blocks per chunk"
);

checkedChunks.add(chunk);

if (logIsEnabled && removed_falling) LogUtil.moduleLog(Level.INFO, name(),
"Removed falling blocks at " + LocationUtil.toString(event.getBlock().getLocation())
+ " because reached limit of " + maxFallingGravityBlockPerChunk + " falling gravity blocks per chunk");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.config.Config;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import me.moomoo.anarchyexploitfixes.utils.LocationUtil;
import me.moomoo.anarchyexploitfixes.utils.LogUtil;
import org.bukkit.Chunk;
import org.bukkit.World;
Expand Down Expand Up @@ -34,8 +35,8 @@ public MinecartLimit() {
"Limit the amount of minecarts to prevent lag.");
this.logIsEnabled = config.getBoolean("chunk-limits.minecart-limit.log-removals", false);
this.maxMinecartsPerChunk = config.getInt("chunk-limits.minecart-limit.max-minecarts-per-chunk", 25);
this.checkPeriod = config.getInt("chunk-limits.minecart-limit.check-period-in-ticks", 400,
"200 ticks = 10 seconds.");
this.checkPeriod = Math.max(config.getInt("chunk-limits.minecart-limit.check-period-in-ticks", 400,
"200 ticks = 10 seconds."), 1);
}

@Override
Expand Down Expand Up @@ -80,8 +81,7 @@ private void onCreate(VehicleCreateEvent event) {
entity.getScheduler().run(plugin, kill -> {
entity.remove();
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(), "Removed minecart " + entity.getType()
+ " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY()
+ " z:" + entity.getLocation().getZ() + ", because reached limit of " + maxMinecartsPerChunk);
+ " at " + LocationUtil.toString(entity.getLocation()) + " because reached limit of " + maxMinecartsPerChunk);
}, null);
}
}
Expand All @@ -103,8 +103,7 @@ private void run() {
entity.getScheduler().run(plugin, kill -> {
entity.remove();
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(), "Removed minecart " + entity.getType()
+ " at x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY()
+ " z:" + entity.getLocation().getZ() + ", because reached limit of " + maxMinecartsPerChunk);
+ " at " + LocationUtil.toString(entity.getLocation()) + " because reached limit of " + maxMinecartsPerChunk);
}, null);
}
});
Expand Down
Loading

0 comments on commit 4c61acf

Please sign in to comment.