From eb1a1cd9cdbe4fd95f96548a51bb6df86eee5a7b Mon Sep 17 00:00:00 2001 From: Moulberry Date: Tue, 7 Nov 2023 13:23:17 +0800 Subject: [PATCH] Implement whitelist-world-regex and blacklist-world-regex config options --- .../java/com/moulberry/axiom/AxiomPaper.java | 17 +++++++++++++++++ .../axiom/event/AxiomModifyWorldEvent.java | 1 + .../packet/RequestChunkDataPacketListener.java | 5 +---- .../packet/SetBlockBufferPacketListener.java | 14 ++++++-------- .../axiom/packet/SetBlockPacketListener.java | 7 +++---- .../axiom/packet/SetTimePacketListener.java | 6 +++--- src/main/resources/config.yml | 8 ++++++++ 7 files changed, 39 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/moulberry/axiom/AxiomPaper.java b/src/main/java/com/moulberry/axiom/AxiomPaper.java index 880199f..ee38eb0 100644 --- a/src/main/java/com/moulberry/axiom/AxiomPaper.java +++ b/src/main/java/com/moulberry/axiom/AxiomPaper.java @@ -2,6 +2,7 @@ package com.moulberry.axiom; import com.moulberry.axiom.buffer.CompressedBlockEntity; import com.moulberry.axiom.event.AxiomCreateWorldPropertiesEvent; +import com.moulberry.axiom.event.AxiomModifyWorldEvent; import com.moulberry.axiom.packet.*; import com.moulberry.axiom.world_properties.server.ServerWorldPropertiesRegistry; import com.moulberry.axiom.world_properties.server.ServerWorldProperty; @@ -175,6 +176,22 @@ public class AxiomPaper extends JavaPlugin implements Listener { } } + public boolean canModifyWorld(Player player, World world) { + String whitelist = this.configuration.getString("whitelist-world-regex"); + if (whitelist != null && !world.getName().matches(whitelist)) { + return false; + } + + String blacklist = this.configuration.getString("blacklist-world-regex"); + if (blacklist != null && world.getName().matches(blacklist)) { + return false; + } + + AxiomModifyWorldEvent modifyWorldEvent = new AxiomModifyWorldEvent(player, world); + Bukkit.getPluginManager().callEvent(modifyWorldEvent); + return !modifyWorldEvent.isCancelled(); + } + @EventHandler public void onFailMove(PlayerFailMoveEvent event) { if (event.getPlayer().hasPermission("axiom.*")) { diff --git a/src/main/java/com/moulberry/axiom/event/AxiomModifyWorldEvent.java b/src/main/java/com/moulberry/axiom/event/AxiomModifyWorldEvent.java index 405948f..01f4a42 100644 --- a/src/main/java/com/moulberry/axiom/event/AxiomModifyWorldEvent.java +++ b/src/main/java/com/moulberry/axiom/event/AxiomModifyWorldEvent.java @@ -1,5 +1,6 @@ package com.moulberry.axiom.event; +import com.moulberry.axiom.AxiomPaper; import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; diff --git a/src/main/java/com/moulberry/axiom/packet/RequestChunkDataPacketListener.java b/src/main/java/com/moulberry/axiom/packet/RequestChunkDataPacketListener.java index 1692bb5..3b10061 100644 --- a/src/main/java/com/moulberry/axiom/packet/RequestChunkDataPacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/RequestChunkDataPacketListener.java @@ -51,10 +51,7 @@ public class RequestChunkDataPacketListener implements PluginMessageListener { return; } - // Call AxiomModifyWorldEvent event - AxiomModifyWorldEvent modifyWorldEvent = new AxiomModifyWorldEvent(bukkitPlayer, bukkitPlayer.getWorld()); - Bukkit.getPluginManager().callEvent(modifyWorldEvent); - if (modifyWorldEvent.isCancelled()) { + if (!this.plugin.canModifyWorld(bukkitPlayer, bukkitPlayer.getWorld())) { sendEmptyResponse(player, id); return; } diff --git a/src/main/java/com/moulberry/axiom/packet/SetBlockBufferPacketListener.java b/src/main/java/com/moulberry/axiom/packet/SetBlockBufferPacketListener.java index 9733e53..64f1911 100644 --- a/src/main/java/com/moulberry/axiom/packet/SetBlockBufferPacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/SetBlockBufferPacketListener.java @@ -96,10 +96,9 @@ public class SetBlockBufferPacketListener { return; } - // Call AxiomModifyWorldEvent event - AxiomModifyWorldEvent modifyWorldEvent = new AxiomModifyWorldEvent(player.getBukkitEntity(), world.getWorld()); - Bukkit.getPluginManager().callEvent(modifyWorldEvent); - if (modifyWorldEvent.isCancelled()) return; + if (!this.plugin.canModifyWorld(player.getBukkitEntity(), world.getWorld())) { + return; + } // Allowed, apply buffer BlockPos.MutableBlockPos blockPos = new BlockPos.MutableBlockPos(); @@ -257,10 +256,9 @@ public class SetBlockBufferPacketListener { return; } - // Call AxiomModifyWorldEvent event - AxiomModifyWorldEvent modifyWorldEvent = new AxiomModifyWorldEvent(player.getBukkitEntity(), world.getWorld()); - Bukkit.getPluginManager().callEvent(modifyWorldEvent); - if (modifyWorldEvent.isCancelled()) return; + if (!this.plugin.canModifyWorld(player.getBukkitEntity(), world.getWorld())) { + return; + } Set changedChunks = new HashSet<>(); diff --git a/src/main/java/com/moulberry/axiom/packet/SetBlockPacketListener.java b/src/main/java/com/moulberry/axiom/packet/SetBlockPacketListener.java index cb9ee48..c674d29 100644 --- a/src/main/java/com/moulberry/axiom/packet/SetBlockPacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/SetBlockPacketListener.java @@ -65,10 +65,9 @@ public class SetBlockPacketListener implements PluginMessageListener { return; } - // Check if player is allowed to modify this world - AxiomModifyWorldEvent modifyWorldEvent = new AxiomModifyWorldEvent(bukkitPlayer, bukkitPlayer.getWorld()); - Bukkit.getPluginManager().callEvent(modifyWorldEvent); - if (modifyWorldEvent.isCancelled()) return; + if (!this.plugin.canModifyWorld(bukkitPlayer, bukkitPlayer.getWorld())) { + return; + } // Read packet FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.wrappedBuffer(message)); diff --git a/src/main/java/com/moulberry/axiom/packet/SetTimePacketListener.java b/src/main/java/com/moulberry/axiom/packet/SetTimePacketListener.java index 6b858fc..9ec6def 100644 --- a/src/main/java/com/moulberry/axiom/packet/SetTimePacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/SetTimePacketListener.java @@ -40,9 +40,9 @@ public class SetTimePacketListener implements PluginMessageListener { if (!level.dimension().equals(key)) return; // Call modify world - AxiomModifyWorldEvent modifyWorldEvent = new AxiomModifyWorldEvent(player, player.getWorld()); - Bukkit.getPluginManager().callEvent(modifyWorldEvent); - if (modifyWorldEvent.isCancelled()) return; + if (!this.plugin.canModifyWorld(player, player.getWorld())) { + return; + } // Call time change event AxiomTimeChangeEvent timeChangeEvent = new AxiomTimeChangeEvent(player, time, freezeTime); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index b79da88..094b641 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -20,6 +20,14 @@ unsupported-axiom-version: "kick" # Maximum packet size. Must not be less than 32767 max-block-buffer-packet-size: 0x100000 +# Regex for whitelisting worlds +# The world can only be modified if the regex matches the world's name +whitelist-world-regex: null + +# Regex for blacklisting worlds +# If the regex matches the world's name, the world can't be modified +blacklist-world-regex: null + # Toggles for individual packet handlers. May break certain features packet-handlers: hello: true