From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Sun, 20 Dec 2020 16:41:44 -0800 Subject: [PATCH] Add WorldGameRuleChangeEvent diff --git a/src/main/java/net/minecraft/server/commands/GameRuleCommand.java b/src/main/java/net/minecraft/server/commands/GameRuleCommand.java index c8c358531dbc167e249bac2af246c5e34fbdd4df..10c1790226e25da3b9b599c9a40de57d5727ddc4 100644 --- a/src/main/java/net/minecraft/server/commands/GameRuleCommand.java +++ b/src/main/java/net/minecraft/server/commands/GameRuleCommand.java @@ -33,7 +33,7 @@ public class GameRuleCommand { CommandSourceStack commandlistenerwrapper = (CommandSourceStack) context.getSource(); T t0 = commandlistenerwrapper.getLevel().getGameRules().getRule(key); // CraftBukkit - 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); diff --git a/src/main/java/net/minecraft/world/level/GameRules.java b/src/main/java/net/minecraft/world/level/GameRules.java index ba825ff3df79903aefe1a4cb14efab9a302397d7..ac7a5410b01a6741e3b548d153f37ea1d8c1a4cb 100644 --- a/src/main/java/net/minecraft/world/level/GameRules.java +++ b/src/main/java/net/minecraft/world/level/GameRules.java @@ -286,10 +286,10 @@ public class GameRules { this.type = type; } - 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); + 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 } @@ -347,8 +347,11 @@ public class GameRules { } @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() { @@ -412,8 +415,11 @@ public class GameRules { } @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() { diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java index afb65de5fbff44b4ca455c778030c3387c2dafc8..3893b70926140f3658f6bab68c5c4f7c41dc603a 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java @@ -1884,8 +1884,13 @@ public class CraftWorld extends CraftRegionAccessor implements World { 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(CraftWorld.getGameRulesNMS().get(rule)); - handle.deserialize(value); + handle.deserialize(event.getValue()); // Paper - Add WorldGameRuleChangeEvent handle.onChanged(this.getHandle()); return true; } @@ -1921,8 +1926,12 @@ public class CraftWorld extends CraftRegionAccessor implements World { 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(CraftWorld.getGameRulesNMS().get(rule.getName())); - handle.deserialize(newValue.toString()); + handle.deserialize(event.getValue()); // Paper - Add WorldGameRuleChangeEvent handle.onChanged(this.getHandle()); return true; }