Skip to content

Commit

Permalink
another round of improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Mar 6, 2024
1 parent 777b7e3 commit b306e55
Show file tree
Hide file tree
Showing 80 changed files with 681 additions and 457 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
import me.moomoo.anarchyexploitfixes.config.Config;
import me.moomoo.anarchyexploitfixes.config.LanguageCache;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import me.moomoo.anarchyexploitfixes.utils.models.TPSCache;
import me.moomoo.anarchyexploitfixes.utils.models.CachedTickData;
import net.kyori.adventure.text.logger.slf4j.ComponentLogger;
import org.bstats.bukkit.Metrics;
import org.bukkit.NamespacedKey;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.time.Duration;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
Expand All @@ -31,7 +31,7 @@ public class AnarchyExploitFixes extends JavaPlugin {
private static AnarchyExploitFixes instance;
private static HashMap<String, LanguageCache> languageCacheMap;
private static Config config;
private static TPSCache tpsCache;
private static CachedTickData cachedTickData;
private static ComponentLogger prefixedLogger, unPrefixedLogger;
private static Metrics metrics;
private static boolean isServerFolia, foundProtocolLib;
Expand Down Expand Up @@ -106,26 +106,20 @@ public void onDisable() {
instance = null;
config = null;
languageCacheMap = null;
tpsCache = null;
cachedTickData = null;
prefixedLogger = null;
isServerFolia = foundProtocolLib = false;
}

public static AnarchyExploitFixes getInstance() {
return instance;
}
public static HashMap<String, LanguageCache> getLanguageCacheMap() {
return languageCacheMap;
}
public static TPSCache getTpsCache() {
return tpsCache;
public static CachedTickData getTickData() {
return cachedTickData;
}
public static Config getConfiguration() {
return config;
}
public static NamespacedKey getKey(String key) {
return new NamespacedKey(instance, key);
}
public static ComponentLogger getPrefixedLogger() {
return prefixedLogger;
}
Expand Down Expand Up @@ -157,7 +151,7 @@ public void reloadPlugin() {
private void reloadConfiguration() {
try {
config = new Config();
tpsCache = TPSCache.create(config.max_tps_check_interval_millis);
cachedTickData = CachedTickData.create(Duration.ofMillis(config.max_tps_check_interval_millis));
AnarchyExploitFixesModule.reloadModules();
config.saveConfig();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public boolean shouldRegister() {
return true;
}

@SuppressWarnings("UnstableApiUsage")
@CommandMethod("aef version")
@CommandDescription("Get the plugin version.")
@CommandPermission("anarchyexploitfixes.cmd.version")
Expand Down Expand Up @@ -82,7 +83,7 @@ public void lagCommand(
Thread.sleep(millis);
} catch (InterruptedException e) {
sender.sendMessage(Component.text("Operation was interrupted! - " + e.getLocalizedMessage()).color(NamedTextColor.RED));
e.printStackTrace();
AnarchyExploitFixes.getPrefixedLogger().error("Lag command encountered an error!", e);
}
}

Expand Down Expand Up @@ -137,7 +138,7 @@ public void gearedCommand(
int ungearedPlayerCount = 0;

for (Player player : plugin.getServer().getOnlinePlayers()) {
if (Arrays.stream(player.getInventory().getArmorContents()).noneMatch(Objects::isNull)) {
if (Arrays.stream(player.getInventory().getArmorContents()).anyMatch(Objects::nonNull)) {
geared.append(player.getName()).append(", ");
gearedPlayerCount++;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import cloud.commandframework.annotations.CommandMethod;
import cloud.commandframework.annotations.CommandPermission;
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.enums.NamespacedKeys;
import me.moomoo.anarchyexploitfixes.enums.AEFKey;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.command.CommandSender;
Expand All @@ -27,16 +27,16 @@ public void onConMsgToggle(final CommandSender sender) {
return;
}

final boolean msgsWereVisible;
boolean msgsWereVisible;
PersistentDataContainer dataContainer = player.getPersistentDataContainer();

if (!dataContainer.has(NamespacedKeys.CONNECT_MSG_TOGGLE.key(), PersistentDataType.BOOLEAN)) {
if (!dataContainer.has(AEFKey.CONNECT_MSG_TOGGLE.getKey(), PersistentDataType.BOOLEAN)) {
msgsWereVisible = AnarchyExploitFixes.getConfiguration().connectionMsgsAreOnByDefault;
} else {
msgsWereVisible = dataContainer.get(NamespacedKeys.CONNECT_MSG_TOGGLE.key(), PersistentDataType.BOOLEAN);
msgsWereVisible = dataContainer.get(AEFKey.CONNECT_MSG_TOGGLE.getKey(), PersistentDataType.BOOLEAN);
}

dataContainer.set(NamespacedKeys.CONNECT_MSG_TOGGLE.key(), PersistentDataType.BOOLEAN, !msgsWereVisible);
dataContainer.set(AEFKey.CONNECT_MSG_TOGGLE.getKey(), PersistentDataType.BOOLEAN, !msgsWereVisible);

if (msgsWereVisible) {
player.sendMessage(AnarchyExploitFixes.getLang(player.locale()).misc_disabledConnectionMsgs);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package me.moomoo.anarchyexploitfixes.enums;

import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import org.bukkit.Keyed;
import org.bukkit.NamespacedKey;
import org.intellij.lang.annotations.Pattern;
import org.jetbrains.annotations.NotNull;

public enum AEFKey implements Keyed {

CONNECT_MSG_TOGGLE("show-connection-msgs");

private final NamespacedKey key;

AEFKey(@Pattern("[a-z0-9_\\-.]+") String key) {
this.key = new NamespacedKey(AnarchyExploitFixes.getInstance(), key);
}

@Override
public @NotNull NamespacedKey getKey() {
return key;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package me.moomoo.anarchyexploitfixes.enums;

public enum AEFPermission {

BYPASS_ELYTRA("bypass.elytra"),
BYPASS_NETHER_ROOF("bypass.netherroof"),
BYPASS_CMD_WHITELIST("bypass.commandwhitelist"),
BYPASS_CHAT("bypass.chat"),
BYPASS_ILLEGAL_ITEMS("bypass.illegalitems"),
SILENT_JOIN("silentJoin"),
SILENT_LEAVE("silentLeave");

private final String permission;

AEFPermission(String perm) {
this.permission = "aef." + perm;
}

public String get() {
return permission;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private void onChunkLoad(ChunkLoadEvent event) {
Chunk chunk = event.getChunk();

plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(), fixBedrock -> {
if (checkShouldPauseOnLowTPS && (AnarchyExploitFixes.getTpsCache().getTPS() <= pauseTPS)) return;
if (checkShouldPauseOnLowTPS && (AnarchyExploitFixes.getTickData().getTPS() <= pauseTPS)) return;
ChunkUtil.createBedrockLayer(chunk, ceilingY);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private void onChunkLoad(ChunkLoadEvent event) {
Chunk chunk = event.getChunk();

plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(), fixBedrock -> {
if (checkShouldPauseOnLowTPS && (AnarchyExploitFixes.getTpsCache().getTPS() <= pauseTPS)) return;
if (checkShouldPauseOnLowTPS && (AnarchyExploitFixes.getTickData().getTPS() <= pauseTPS)) return;
ChunkUtil.createBedrockLayer(chunk, world.getMinHeight());
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private void onChunkLoad(ChunkLoadEvent event) {
Chunk chunk = event.getChunk();

plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(), fixBedrock -> {
if (checkShouldPauseOnLowTPS && (AnarchyExploitFixes.getTpsCache().getTPS() <= pauseTPS)) return;
if (checkShouldPauseOnLowTPS && (AnarchyExploitFixes.getTickData().getTPS() <= pauseTPS)) return;
ChunkUtil.createBedrockLayer(chunk, world.getMinHeight());
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private void run() {
for (Chunk chunk : world.getLoadedChunks()) {
plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(), fixChunk -> {
if (!chunk.isEntitiesLoaded()) return;
if (pauseOnLowTPS && (AnarchyExploitFixes.getTpsCache().getTPS() <= pauseTPS)) return;
if (pauseOnLowTPS && (AnarchyExploitFixes.getTickData().getTPS() <= pauseTPS)) return;
ChunkUtil.createBedrockLayer(chunk, ceilingY);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private void run() {
for (Chunk chunk : world.getLoadedChunks()) {
plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(), fixBedrock -> {
if (!chunk.isEntitiesLoaded()) return;
if (checkShouldPauseOnLowTPS && (AnarchyExploitFixes.getTpsCache().getTPS() <= pauseTPS)) return;
if (checkShouldPauseOnLowTPS && (AnarchyExploitFixes.getTickData().getTPS() <= pauseTPS)) return;
ChunkUtil.createBedrockLayer(chunk, world.getMinHeight());
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private void run() {
for (Chunk chunk : world.getLoadedChunks()) {
plugin.getServer().getRegionScheduler().run(plugin, world, chunk.getX(), chunk.getZ(), fixBedrock -> {
if (!chunk.isEntitiesLoaded()) return;
if (pauseOnLowTPS && (AnarchyExploitFixes.getTpsCache().getTPS() <= pauseTPS)) return;
if (pauseOnLowTPS && (AnarchyExploitFixes.getTickData().getTPS() <= pauseTPS)) return;
ChunkUtil.createBedrockLayer(chunk, world.getMinHeight());
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.destroystokyo.paper.event.server.AsyncTabCompleteEvent;
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.enums.AEFPermission;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
Expand Down Expand Up @@ -52,17 +52,15 @@ private boolean isSuspectedScanPacket(String buffer) {

@EventHandler(priority = EventPriority.HIGHEST)
private void onAsyncCommandTabComplete(AsyncTabCompleteEvent event) {
if (event.getSender().hasPermission("anarchyexploitfixes.chatbypass")) return;
if (!(event.getSender() instanceof Player)) return;
if (event.getSender().hasPermission(AEFPermission.BYPASS_CHAT.get())) return;
if (this.isSuspectedScanPacket(event.getBuffer())) {
event.setCancelled(true);
}
}

@EventHandler(priority = EventPriority.HIGHEST)
private void onCommandTabComplete(TabCompleteEvent event) {
if (event.getSender().hasPermission("anarchyexploitfixes.chatbypass")) return;
if (!(event.getSender() instanceof Player)) return;
if (event.getSender().hasPermission(AEFPermission.BYPASS_CHAT.get())) return;
if (this.isSuspectedScanPacket(event.getBuffer())) {
event.setCancelled(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketEvent;
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.enums.AEFPermission;
import me.moomoo.anarchyexploitfixes.utils.CommandUtil;
import org.bukkit.entity.Player;

Expand Down Expand Up @@ -35,10 +36,10 @@ protected void register() {

@Override
public void onPacketReceiving(PacketEvent event) {
String message = event.getPacket().getStrings().read(0);
final String message = event.getPacket().getStrings().read(0);
if (!message.startsWith("/")) return;
Player player = event.getPlayer();
if (player.hasPermission("anarchyexploitfixes.commandwhitelistbypass")) return;
final Player player = event.getPlayer();
if (player.hasPermission(AEFPermission.BYPASS_CMD_WHITELIST.get())) return;

if (!allowedCommands.contains(CommandUtil.getCommandLabel(message).toLowerCase())) {
event.setCancelled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.destroystokyo.paper.event.server.AsyncTabCompleteEvent;
import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes;
import me.moomoo.anarchyexploitfixes.config.Config;
import me.moomoo.anarchyexploitfixes.enums.AEFPermission;
import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule;
import me.moomoo.anarchyexploitfixes.utils.CommandUtil;
import me.moomoo.anarchyexploitfixes.utils.LogUtil;
Expand Down Expand Up @@ -49,7 +50,7 @@ public CommandWhitelist() {
), "Add all commands you want your players to be able to access (without the '/'). Not case sensitive.")
.stream()
.map(String::toLowerCase)
.collect(Collectors.toSet());
.collect(Collectors.toCollection(HashSet::new));
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 @@ -94,8 +95,8 @@ public void disable() {

@EventHandler(priority = EventPriority.HIGHEST)
private void onCommandPreProcess(PlayerCommandPreprocessEvent event) {
Player player = event.getPlayer();
if (player.hasPermission("anarchyexploitfixes.commandwhitelistbypass")) return;
final Player player = event.getPlayer();
if (player.hasPermission(AEFPermission.BYPASS_CMD_WHITELIST.get())) return;

String message = event.getMessage();
String commandLabel = CommandUtil.getCommandLabel(message).toLowerCase();
Expand All @@ -122,25 +123,28 @@ private void onCommandPreProcess(PlayerCommandPreprocessEvent event) {
}
}

@EventHandler(priority = EventPriority.NORMAL)
@EventHandler(priority = EventPriority.HIGH)
private void onInitialTabCompleteListSend(PlayerCommandSendEvent event) {
if (event.getPlayer().hasPermission("anarchyexploitfixes.commandwhitelistbypass")) return;
if (event.getPlayer().hasPermission(AEFPermission.BYPASS_CMD_WHITELIST.get())) return;

event.getCommands().removeIf(cmd -> !allowedCommands.contains(cmd));
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onAsyncCommandTabComplete(AsyncTabCompleteEvent event) {
if (!(event.getSender() instanceof Player player)) return;
if (player.hasPermission("anarchyexploitfixes.commandwhitelistbypass")) return;
if (event.getCompletions().isEmpty()) return;
if (!(event.getSender() instanceof Player)) return;
if (event.getSender().hasPermission(AEFPermission.BYPASS_CMD_WHITELIST.get())) return;

event.setCompletions(getFilteredTabCompletions(event.getBuffer(), event.getCompletions()));
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
private void onCommandTabComplete(TabCompleteEvent event) {
if (!(event.getSender() instanceof Player player)) return;
if (player.hasPermission("anarchyexploitfixes.commandwhitelistbypass")) return;
if (event.getCompletions().isEmpty()) return;
if (!(event.getSender() instanceof Player)) return;
if (event.getSender().hasPermission(AEFPermission.BYPASS_CMD_WHITELIST.get())) return;

event.setCompletions(getFilteredTabCompletions(event.getBuffer(), event.getCompletions()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,23 @@ public void disable() {
private void onBedBreak(PlayerBedFailEnterEvent event) {
if (breakDelayMillis <= 0 || !event.getWillExplode()) return;

final UUID playerUniqueId = event.getPlayer().getUniqueId();
if (breakCooldowns.contains(playerUniqueId)) {
if (breakCooldowns.contains(event.getPlayer().getUniqueId())) {
event.setCancelled(true);
} else {
breakCooldowns.add(playerUniqueId);
breakCooldowns.add(event.getPlayer().getUniqueId());
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onBedPlace(BlockPlaceEvent event) {
if (placeDelayMillis <= 0) return;
Block placed = event.getBlockPlaced();
final Block placed = event.getBlockPlaced();
if (!MaterialTags.BEDS.isTagged(placed) || placed.getWorld().isBedWorks()) return;

final UUID playerUniqueId = event.getPlayer().getUniqueId();
if (placeCooldowns.contains(playerUniqueId)) {
if (placeCooldowns.contains(event.getPlayer().getUniqueId())) {
event.setCancelled(true);
} else {
placeCooldowns.add(playerUniqueId);
placeCooldowns.add(event.getPlayer().getUniqueId());
}
}
}
Loading

0 comments on commit b306e55

Please sign in to comment.