From 9e32dc5c2594b663a143f7e4af998b30ce95c7c2 Mon Sep 17 00:00:00 2001 From: kaboom <58372747+kaboombot@users.noreply.github.com> Date: Sat, 21 May 2022 00:31:39 +0300 Subject: [PATCH] Replace existing gamerule checks with WorldGameRuleChangeEvent --- src/main/java/pw/kaboom/extras/Main.java | 2 ++ .../extras/modules/server/ServerCommand.java | 9 --------- .../extras/modules/server/ServerGameRule.java | 20 +++++++++++++++++++ .../extras/modules/server/ServerTick.java | 12 ----------- 4 files changed, 22 insertions(+), 21 deletions(-) create mode 100644 src/main/java/pw/kaboom/extras/modules/server/ServerGameRule.java diff --git a/src/main/java/pw/kaboom/extras/Main.java b/src/main/java/pw/kaboom/extras/Main.java index f8083e9..3df1071 100644 --- a/src/main/java/pw/kaboom/extras/Main.java +++ b/src/main/java/pw/kaboom/extras/Main.java @@ -40,6 +40,7 @@ import pw.kaboom.extras.modules.player.PlayerInteract; import pw.kaboom.extras.modules.player.PlayerRecipe; import pw.kaboom.extras.modules.player.PlayerTeleport; import pw.kaboom.extras.modules.server.ServerCommand; +import pw.kaboom.extras.modules.server.ServerGameRule; import pw.kaboom.extras.modules.server.ServerTabComplete; import pw.kaboom.extras.modules.server.ServerTick; @@ -109,6 +110,7 @@ public final class Main extends JavaPlugin { /* Server-related modules */ this.getServer().getPluginManager().registerEvents(new ServerCommand(), this); + this.getServer().getPluginManager().registerEvents(new ServerGameRule(), this); this.getServer().getPluginManager().registerEvents(new ServerTabComplete(), this); this.getServer().getPluginManager().registerEvents(new ServerTick(), this); diff --git a/src/main/java/pw/kaboom/extras/modules/server/ServerCommand.java b/src/main/java/pw/kaboom/extras/modules/server/ServerCommand.java index 46f7ba8..b53be62 100644 --- a/src/main/java/pw/kaboom/extras/modules/server/ServerCommand.java +++ b/src/main/java/pw/kaboom/extras/modules/server/ServerCommand.java @@ -128,15 +128,6 @@ public final class ServerCommand implements Listener { return "cancel"; } break; - case "/minecraft:gamerule": - case "/gamerule": - if (arr.length >= 3) { - if ("randomTickSpeed".equalsIgnoreCase(arr[1]) - && Double.parseDouble(arr[2]) > 6) { - return command.replaceFirst(arr[2], "6"); - } - } - break; case "/minecraft:give": case "/give": if (Double.parseDouble(arr[arr.length - 1]) > 64) { diff --git a/src/main/java/pw/kaboom/extras/modules/server/ServerGameRule.java b/src/main/java/pw/kaboom/extras/modules/server/ServerGameRule.java new file mode 100644 index 0000000..742c746 --- /dev/null +++ b/src/main/java/pw/kaboom/extras/modules/server/ServerGameRule.java @@ -0,0 +1,20 @@ +package pw.kaboom.extras.modules.server; + +import io.papermc.paper.event.world.WorldGameRuleChangeEvent; +import org.bukkit.GameRule; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; + +public final class ServerGameRule implements Listener { + @EventHandler + void onGameRuleChange(final WorldGameRuleChangeEvent event) { + final GameRule gameRule = event.getGameRule(); + + if ((gameRule == GameRule.RANDOM_TICK_SPEED + && Integer.parseInt(event.getValue()) > 6) + || (event.getGameRule() == GameRule.SPAWN_RADIUS + && Integer.parseInt(event.getValue()) > 100)) { + event.setCancelled(true); + } + } +} diff --git a/src/main/java/pw/kaboom/extras/modules/server/ServerTick.java b/src/main/java/pw/kaboom/extras/modules/server/ServerTick.java index c100385..f5b38b6 100644 --- a/src/main/java/pw/kaboom/extras/modules/server/ServerTick.java +++ b/src/main/java/pw/kaboom/extras/modules/server/ServerTick.java @@ -1,6 +1,5 @@ package pw.kaboom.extras.modules.server; -import org.bukkit.GameRule; import org.bukkit.World; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -10,17 +9,6 @@ public final class ServerTick implements Listener { @EventHandler void onPlayerStatisticIncrement(final PlayerStatisticIncrementEvent event) { final World world = event.getPlayer().getWorld(); - final Integer randomTickSpeed = world.getGameRuleValue(GameRule.RANDOM_TICK_SPEED); - - if (randomTickSpeed > 6) { - world.setGameRule(GameRule.RANDOM_TICK_SPEED, 6); - } - - final Integer spawnRadius = world.getGameRuleValue(GameRule.SPAWN_RADIUS); - - if (spawnRadius > 100) { - world.setGameRule(GameRule.SPAWN_RADIUS, 100); - } if (!world.isAutoSave()) { world.setAutoSave(true);