From 1e050722327ce22b864537bdf00cfaf185ca6b6d Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Sun, 20 Dec 2020 16:41:44 -0800 Subject: [PATCH] Add WorldGameRuleChangeEvent --- .../commands/GameRuleCommand.java.patch | 7 ++- .../world/level/GameRules.java.patch | 53 +++++++++++++++---- .../org/bukkit/craftbukkit/CraftWorld.java | 13 ++++- 3 files changed, 59 insertions(+), 14 deletions(-) diff --git a/paper-server/patches/sources/net/minecraft/server/commands/GameRuleCommand.java.patch b/paper-server/patches/sources/net/minecraft/server/commands/GameRuleCommand.java.patch index e36706662a..66f9df2b3b 100644 --- a/paper-server/patches/sources/net/minecraft/server/commands/GameRuleCommand.java.patch +++ b/paper-server/patches/sources/net/minecraft/server/commands/GameRuleCommand.java.patch @@ -1,14 +1,17 @@ --- a/net/minecraft/server/commands/GameRuleCommand.java +++ b/net/minecraft/server/commands/GameRuleCommand.java -@@ -34,7 +34,7 @@ +@@ -34,9 +34,9 @@ static > int setRule(CommandContext context, GameRules.Key key) { CommandSourceStack commandlistenerwrapper = (CommandSourceStack) context.getSource(); - T t0 = commandlistenerwrapper.getServer().getGameRules().getRule(key); + T t0 = commandlistenerwrapper.getLevel().getGameRules().getRule(key); // CraftBukkit - t0.setFromArgument(context, "value"); +- t0.setFromArgument(context, "value"); ++ t0.setFromArgument(context, "value", key); // Paper - Add WorldGameRuleChangeEvent commandlistenerwrapper.sendSuccess(() -> { + return Component.translatable("commands.gamerule.set", key.getId(), t0.toString()); + }, true); @@ -44,7 +44,7 @@ } diff --git a/paper-server/patches/sources/net/minecraft/world/level/GameRules.java.patch b/paper-server/patches/sources/net/minecraft/world/level/GameRules.java.patch index fb6733373b..f392ebb247 100644 --- a/paper-server/patches/sources/net/minecraft/world/level/GameRules.java.patch +++ b/paper-server/patches/sources/net/minecraft/world/level/GameRules.java.patch @@ -108,11 +108,18 @@ } public void callVisitor(GameRules.GameRuleTypeVisitor consumer, GameRules.Key key) { -@@ -326,17 +326,17 @@ +@@ -322,21 +322,21 @@ + this.type = type; + } - public void setFromArgument(CommandContext context, String name) { - this.updateFromArgument(context, name); +- protected abstract void updateFromArgument(CommandContext context, String name); ++ protected abstract void updateFromArgument(CommandContext context, String name, GameRules.Key gameRuleKey); // Paper - Add WorldGameRuleChangeEvent + +- public void setFromArgument(CommandContext context, String name) { +- this.updateFromArgument(context, name); - this.onChanged(((CommandSourceStack) context.getSource()).getServer()); ++ public void setFromArgument(CommandContext context, String name, GameRules.Key gameRuleKey) { // Paper - Add WorldGameRuleChangeEvent ++ this.updateFromArgument(context, name, gameRuleKey); // Paper - Add WorldGameRuleChangeEvent + this.onChanged(((CommandSourceStack) context.getSource()).getLevel()); // CraftBukkit - per-world } @@ -149,7 +156,20 @@ return new GameRules.Type<>(BoolArgumentType::bool, (gamerules_gameruledefinition) -> { return new GameRules.BooleanValue(gamerules_gameruledefinition, initialValue); }, changeCallback, GameRules.GameRuleTypeVisitor::visitBoolean, FeatureFlagSet.of()); -@@ -391,9 +391,9 @@ +@@ -383,17 +383,20 @@ + } + + @Override +- protected void updateFromArgument(CommandContext context, String name) { +- this.value = BoolArgumentType.getBool(context, name); ++ protected void updateFromArgument(CommandContext context, String name, GameRules.Key gameRuleKey) { // Paper start - Add WorldGameRuleChangeEvent ++ io.papermc.paper.event.world.WorldGameRuleChangeEvent event = new io.papermc.paper.event.world.WorldGameRuleChangeEvent(context.getSource().getBukkitWorld(), context.getSource().getBukkitSender(), (org.bukkit.GameRule) org.bukkit.GameRule.getByName(gameRuleKey.toString()), String.valueOf(BoolArgumentType.getBool(context, name))); ++ if (!event.callEvent()) return; ++ this.value = Boolean.parseBoolean(event.getValue()); ++ // Paper end - Add WorldGameRuleChangeEvent + } + + public boolean get() { return this.value; } @@ -162,7 +182,7 @@ } @Override -@@ -402,7 +402,7 @@ +@@ -402,7 +405,7 @@ } @Override @@ -171,7 +191,7 @@ this.value = Boolean.parseBoolean(value); } -@@ -421,9 +421,9 @@ +@@ -421,9 +424,9 @@ return new GameRules.BooleanValue(this.type, this.value); } @@ -184,7 +204,7 @@ } } -@@ -431,13 +431,13 @@ +@@ -431,13 +434,13 @@ private int value; @@ -200,7 +220,20 @@ return new GameRules.Type<>(() -> { return IntegerArgumentType.integer(min, max); }, (gamerules_gameruledefinition) -> { -@@ -464,9 +464,9 @@ +@@ -456,17 +459,20 @@ + } + + @Override +- protected void updateFromArgument(CommandContext context, String name) { +- this.value = IntegerArgumentType.getInteger(context, name); ++ protected void updateFromArgument(CommandContext context, String name, GameRules.Key gameRuleKey) { // Paper start - Add WorldGameRuleChangeEvent ++ io.papermc.paper.event.world.WorldGameRuleChangeEvent event = new io.papermc.paper.event.world.WorldGameRuleChangeEvent(context.getSource().getBukkitWorld(), context.getSource().getBukkitSender(), (org.bukkit.GameRule) org.bukkit.GameRule.getByName(gameRuleKey.toString()), String.valueOf(IntegerArgumentType.getInteger(context, name))); ++ if (!event.callEvent()) return; ++ this.value = Integer.parseInt(event.getValue()); ++ // Paper end - Add WorldGameRuleChangeEvent + } + + public int get() { return this.value; } @@ -213,7 +246,7 @@ } @Override -@@ -475,7 +475,7 @@ +@@ -475,7 +481,7 @@ } @Override @@ -222,7 +255,7 @@ this.value = IntegerValue.safeParse(value); } -@@ -517,9 +517,9 @@ +@@ -517,9 +523,9 @@ return new GameRules.IntegerValue(this.type, this.value); } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftWorld.java index b877904bd1..69464a4fe4 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftWorld.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftWorld.java @@ -1884,9 +1884,14 @@ public class CraftWorld extends CraftRegionAccessor implements World { if (rule == null || value == null) return false; if (!this.isGameRule(rule)) return false; + // Paper start - Add WorldGameRuleChangeEvent + GameRule gameRule = GameRule.getByName(rule); + io.papermc.paper.event.world.WorldGameRuleChangeEvent event = new io.papermc.paper.event.world.WorldGameRuleChangeEvent(this, null, gameRule, value); + if (!event.callEvent()) return false; + // Paper end - Add WorldGameRuleChangeEvent GameRules.Value handle = this.getHandle().getGameRules().getRule(this.getGameRulesNMS().get(rule)); - handle.deserialize(value); + handle.deserialize(event.getValue()); // Paper - Add WorldGameRuleChangeEvent handle.onChanged(this.getHandle()); return true; } @@ -1921,9 +1926,13 @@ public class CraftWorld extends CraftRegionAccessor implements World { Preconditions.checkArgument(newValue != null, "GameRule value cannot be null"); if (!this.isGameRule(rule.getName())) return false; + // Paper start - Add WorldGameRuleChangeEvent + io.papermc.paper.event.world.WorldGameRuleChangeEvent event = new io.papermc.paper.event.world.WorldGameRuleChangeEvent(this, null, rule, String.valueOf(newValue)); + if (!event.callEvent()) return false; + // Paper end - Add WorldGameRuleChangeEvent GameRules.Value handle = this.getHandle().getGameRules().getRule(this.getGameRulesNMS().get(rule.getName())); - handle.deserialize(newValue.toString()); + handle.deserialize(event.getValue()); // Paper - Add WorldGameRuleChangeEvent handle.onChanged(this.getHandle()); return true; }