--- a/net/minecraft/world/level/GameRules.java +++ b/net/minecraft/world/level/GameRules.java @@ -58,7 +58,7 @@ public static final GameRules.Key RULE_SENDCOMMANDFEEDBACK = GameRules.register("sendCommandFeedback", GameRules.Category.CHAT, GameRules.BooleanValue.create(true)); public static final GameRules.Key RULE_REDUCEDDEBUGINFO = GameRules.register("reducedDebugInfo", GameRules.Category.MISC, GameRules.BooleanValue.create(false, (minecraftserver, gamerules_gameruleboolean) -> { int i = gamerules_gameruleboolean.get() ? 22 : 23; - Iterator iterator = minecraftserver.getPlayerList().getPlayers().iterator(); + Iterator iterator = minecraftserver.players().iterator(); // CraftBukkit - per-world while (iterator.hasNext()) { ServerPlayer entityplayer = (ServerPlayer) iterator.next(); @@ -74,7 +74,7 @@ public static final GameRules.Key RULE_MAX_ENTITY_CRAMMING = GameRules.register("maxEntityCramming", GameRules.Category.MOBS, GameRules.IntegerValue.create(24)); public static final GameRules.Key RULE_WEATHER_CYCLE = GameRules.register("doWeatherCycle", GameRules.Category.UPDATES, GameRules.BooleanValue.create(true)); public static final GameRules.Key RULE_LIMITED_CRAFTING = GameRules.register("doLimitedCrafting", GameRules.Category.PLAYER, GameRules.BooleanValue.create(false, (minecraftserver, gamerules_gameruleboolean) -> { - Iterator iterator = minecraftserver.getPlayerList().getPlayers().iterator(); + Iterator iterator = minecraftserver.players().iterator(); // CraftBukkit - per-world while (iterator.hasNext()) { ServerPlayer entityplayer = (ServerPlayer) iterator.next(); @@ -90,7 +90,7 @@ public static final GameRules.Key RULE_DISABLE_RAIDS = GameRules.register("disableRaids", GameRules.Category.MOBS, GameRules.BooleanValue.create(false)); public static final GameRules.Key RULE_DOINSOMNIA = GameRules.register("doInsomnia", GameRules.Category.SPAWNING, GameRules.BooleanValue.create(true)); public static final GameRules.Key RULE_DO_IMMEDIATE_RESPAWN = GameRules.register("doImmediateRespawn", GameRules.Category.PLAYER, GameRules.BooleanValue.create(false, (minecraftserver, gamerules_gameruleboolean) -> { - Iterator iterator = minecraftserver.getPlayerList().getPlayers().iterator(); + Iterator iterator = minecraftserver.players().iterator(); // CraftBukkit - per-world while (iterator.hasNext()) { ServerPlayer entityplayer = (ServerPlayer) iterator.next(); @@ -123,12 +123,13 @@ public static final GameRules.Key RULE_MINECART_MAX_SPEED = GameRules.register("minecartMaxSpeed", GameRules.Category.MISC, GameRules.IntegerValue.create(8, 1, 1000, FeatureFlagSet.of(FeatureFlags.MINECART_IMPROVEMENTS), (minecraftserver, gamerules_gameruleint) -> { })); public static final GameRules.Key RULE_SPAWN_CHUNK_RADIUS = GameRules.register("spawnChunkRadius", GameRules.Category.MISC, GameRules.IntegerValue.create(2, 0, 32, FeatureFlagSet.of(), (minecraftserver, gamerules_gameruleint) -> { - ServerLevel worldserver = minecraftserver.overworld(); + ServerLevel worldserver = minecraftserver; // CraftBukkit - per-world worldserver.setDefaultSpawnPos(worldserver.getSharedSpawnPos(), worldserver.getSharedSpawnAngle()); })); private final Map, GameRules.Value> rules; private final FeatureFlagSet enabledFeatures; + private final GameRules.Value[] gameruleArray; // Paper - Perf: Use array for gamerule storage private static > GameRules.Key register(String name, GameRules.Category category, GameRules.Type type) { GameRules.Key gamerules_gamerulekey = new GameRules.Key<>(name, category); @@ -161,10 +162,21 @@ private GameRules(Map, GameRules.Value> rules, FeatureFlagSet enabledFeatures) { this.rules = rules; this.enabledFeatures = enabledFeatures; + + // Paper start - Perf: Use array for gamerule storage + int arraySize = GameRules.Key.lastGameRuleIndex + 1; + GameRules.Value[] values = new GameRules.Value[arraySize]; + + for (Entry, GameRules.Value> entry : rules.entrySet()) { + values[entry.getKey().gameRuleIndex] = entry.getValue(); + } + + this.gameruleArray = values; + // Paper end - Perf: Use array for gamerule storage } public > T getRule(GameRules.Key key) { - T t0 = (GameRules.Value) this.rules.get(key); + T t0 = key == null ? null : (T) this.gameruleArray[key.gameRuleIndex]; // Paper - Perf: Use array for gamerule storage if (t0 == null) { throw new IllegalArgumentException("Tried to access invalid game rule"); @@ -184,7 +196,7 @@ private void loadFromTag(DynamicLike values) { this.rules.forEach((gamerules_gamerulekey, gamerules_gamerulevalue) -> { - DataResult dataresult = values.get(gamerules_gamerulekey.id).asString(); + DataResult dataresult = values.get(gamerules_gamerulekey.id).asString(); // CraftBukkit - decompile error Objects.requireNonNull(gamerules_gamerulevalue); dataresult.ifSuccess(gamerules_gamerulevalue::deserialize); @@ -205,22 +217,22 @@ private > void callVisitorCap(GameRules.GameRuleTypeVisitor visitor, GameRules.Key key, GameRules.Type type) { if (type.requiredFeatures.isSubsetOf(this.enabledFeatures)) { - visitor.visit(key, type); - type.callVisitor(visitor, key); + visitor.visit((GameRules.Key) key, (GameRules.Type) type); // CraftBukkit - decompile error + ((GameRules.Type) type).callVisitor(visitor, (GameRules.Key) key); // CraftBukkit - decompile error } } - public void assignFrom(GameRules rules, @Nullable MinecraftServer server) { - rules.rules.keySet().forEach((gamerules_gamerulekey) -> { - this.assignCap(gamerules_gamerulekey, rules, server); + public void assignFrom(GameRules gamerules, @Nullable ServerLevel minecraftserver) { // CraftBukkit - per-world + gamerules.rules.keySet().forEach((gamerules_gamerulekey) -> { + this.assignCap(gamerules_gamerulekey, gamerules, minecraftserver); }); } - private > void assignCap(GameRules.Key key, GameRules rules, @Nullable MinecraftServer server) { - T t0 = rules.getRule(key); + private > void assignCap(GameRules.Key gamerules_gamerulekey, GameRules gamerules, @Nullable ServerLevel minecraftserver) { // CraftBukkit - per-world + T t0 = gamerules.getRule(gamerules_gamerulekey); - this.getRule(key).setFrom(t0, server); + this.getRule(gamerules_gamerulekey).setFrom(t0, minecraftserver); } public boolean getBoolean(GameRules.Key rule) { @@ -232,6 +244,10 @@ } public static final class Key> { + // Paper start - Perf: Use array for gamerule storage + public static int lastGameRuleIndex = 0; + public final int gameRuleIndex = lastGameRuleIndex++; + // Paper end - Perf: Use array for gamerule storage final String id; private final GameRules.Category category; @@ -285,11 +301,11 @@ final Supplier> argument; private final Function, T> constructor; - final BiConsumer callback; + final BiConsumer callback; // CraftBukkit - per-world private final GameRules.VisitorCaller visitorCaller; final FeatureFlagSet requiredFeatures; - Type(Supplier> argumentType, Function, T> ruleFactory, BiConsumer changeCallback, GameRules.VisitorCaller ruleAcceptor, FeatureFlagSet requiredFeatures) { + Type(Supplier> argumentType, Function, T> ruleFactory, BiConsumer changeCallback, GameRules.VisitorCaller ruleAcceptor, FeatureFlagSet requiredFeatures) { // CraftBukkit - per-world this.argument = argumentType; this.constructor = ruleFactory; this.callback = changeCallback; @@ -302,7 +318,7 @@ } public T createRule() { - return (GameRules.Value) this.constructor.apply(this); + return this.constructor.apply(this); // CraftBukkit - decompile error } public void callVisitor(GameRules.GameRuleTypeVisitor consumer, GameRules.Key key) { @@ -322,21 +338,21 @@ 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); - 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 } - public void onChanged(@Nullable MinecraftServer server) { - if (server != null) { - this.type.callback.accept(server, this.getSelf()); + public void onChanged(@Nullable ServerLevel minecraftserver) { // CraftBukkit - per-world + if (minecraftserver != null) { + this.type.callback.accept(minecraftserver, this.getSelf()); } } - protected abstract void deserialize(String value); + public abstract void deserialize(String value); // PAIL - private->public public abstract String serialize(); @@ -350,7 +366,7 @@ protected abstract T copy(); - public abstract void setFrom(T rule, @Nullable MinecraftServer server); + public abstract void setFrom(T t0, @Nullable ServerLevel minecraftserver); // CraftBukkit - per-world } public interface GameRuleTypeVisitor { @@ -366,7 +382,7 @@ private boolean value; - static GameRules.Type create(boolean initialValue, BiConsumer changeCallback) { + static GameRules.Type create(boolean initialValue, BiConsumer changeCallback) { // CraftBukkit - per-world return new GameRules.Type<>(BoolArgumentType::bool, (gamerules_gameruledefinition) -> { return new GameRules.BooleanValue(gamerules_gameruledefinition, initialValue); }, changeCallback, GameRules.GameRuleTypeVisitor::visitBoolean, FeatureFlagSet.of()); @@ -383,17 +399,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; } - public void set(boolean value, @Nullable MinecraftServer server) { - this.value = value; - this.onChanged(server); + public void set(boolean flag, @Nullable ServerLevel minecraftserver) { // CraftBukkit - per-world + this.value = flag; + this.onChanged(minecraftserver); } @Override @@ -402,7 +421,7 @@ } @Override - protected void deserialize(String value) { + public void deserialize(String value) { // PAIL - protected->public this.value = Boolean.parseBoolean(value); } @@ -421,9 +440,9 @@ return new GameRules.BooleanValue(this.type, this.value); } - public void setFrom(GameRules.BooleanValue rule, @Nullable MinecraftServer server) { - this.value = rule.value; - this.onChanged(server); + public void setFrom(GameRules.BooleanValue gamerules_gameruleboolean, @Nullable ServerLevel minecraftserver) { // CraftBukkit - per-world + this.value = gamerules_gameruleboolean.value; + this.onChanged(minecraftserver); } } @@ -431,13 +450,13 @@ private int value; - private static GameRules.Type create(int initialValue, BiConsumer changeCallback) { + private static GameRules.Type create(int initialValue, BiConsumer changeCallback) { // CraftBukkit - per-world return new GameRules.Type<>(IntegerArgumentType::integer, (gamerules_gameruledefinition) -> { return new GameRules.IntegerValue(gamerules_gameruledefinition, initialValue); }, changeCallback, GameRules.GameRuleTypeVisitor::visitInteger, FeatureFlagSet.of()); } - static GameRules.Type create(int initialValue, int min, int max, FeatureFlagSet requiredFeatures, BiConsumer changeCallback) { + static GameRules.Type create(int initialValue, int min, int max, FeatureFlagSet requiredFeatures, BiConsumer changeCallback) { // CraftBukkit - per-world return new GameRules.Type<>(() -> { return IntegerArgumentType.integer(min, max); }, (gamerules_gameruledefinition) -> { @@ -456,17 +475,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; } - public void set(int value, @Nullable MinecraftServer server) { - this.value = value; - this.onChanged(server); + public void set(int i, @Nullable ServerLevel minecraftserver) { // CraftBukkit - per-world + this.value = i; + this.onChanged(minecraftserver); } @Override @@ -475,7 +497,7 @@ } @Override - protected void deserialize(String value) { + public void deserialize(String value) { // PAIL - protected->public this.value = IntegerValue.safeParse(value); } @@ -517,9 +539,9 @@ return new GameRules.IntegerValue(this.type, this.value); } - public void setFrom(GameRules.IntegerValue rule, @Nullable MinecraftServer server) { - this.value = rule.value; - this.onChanged(server); + public void setFrom(GameRules.IntegerValue gamerules_gameruleint, @Nullable ServerLevel minecraftserver) { // CraftBukkit - per-world + this.value = gamerules_gameruleint.value; + this.onChanged(minecraftserver); } }