From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: MartijnMuijsers Date: Wed, 23 Nov 2022 21:52:25 +0100 Subject: [PATCH] Use array for gamerule storage License: GPL-3.0 (https://www.gnu.org/licenses/gpl-3.0.html) This patch is based on the following patch: "Use array for gamerule storage" By: Paul Sauve As part of: Airplane (https://github.com/TECHNOVE/Airplane) Licensed under: GPL-3.0 (https://www.gnu.org/licenses/gpl-3.0.html) * Airplane copyright * Airplane Copyright (C) 2020 Technove LLC This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . diff --git a/src/main/java/net/minecraft/world/level/GameRules.java b/src/main/java/net/minecraft/world/level/GameRules.java index 2c500cfdd594e24b039d698bced1f9f9537722e3..e660a84251bc67435e905aacf628a84156b5d689 100644 --- a/src/main/java/net/minecraft/world/level/GameRules.java +++ b/src/main/java/net/minecraft/world/level/GameRules.java @@ -91,6 +91,7 @@ public class GameRules { public static final GameRules.Key RULE_UNIVERSAL_ANGER = GameRules.register("universalAnger", GameRules.Category.MOBS, GameRules.BooleanValue.create(false)); public static final GameRules.Key RULE_PLAYERS_SLEEPING_PERCENTAGE = GameRules.register("playersSleepingPercentage", GameRules.Category.PLAYER, GameRules.IntegerValue.create(100)); private final Map, GameRules.Value> rules; + private final GameRules.Value[] gameruleArray; // Gale - Airplane - 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); @@ -109,17 +110,33 @@ public class GameRules { } public GameRules() { - this.rules = (Map) GameRules.GAME_RULE_TYPES.entrySet().stream().collect(ImmutableMap.toImmutableMap(Entry::getKey, (entry) -> { + // Gale start - Airplane - use array for gamerule storage - use this to ensure gameruleArray is initialized + this((Map) GameRules.GAME_RULE_TYPES.entrySet().stream().collect(ImmutableMap.toImmutableMap(Entry::getKey, (entry) -> { return ((GameRules.Type) entry.getValue()).createRule(); - })); + }))); + // Gale end - Airplane - use array for gamerule storage - use this to ensure gameruleArray is initialized } private GameRules(Map, GameRules.Value> rules) { this.rules = rules; + + // Gale start - Airplane - use array for gamerule storage + int arraySize = rules.keySet().stream().mapToInt(key -> key.gameRuleIndex).max().orElse(-1) + 1; + GameRules.Value[] values = new GameRules.Value[arraySize]; + + for (Entry, GameRules.Value> entry : rules.entrySet()) { + values[entry.getKey().gameRuleIndex] = entry.getValue(); + } + + this.gameruleArray = values; + // Gale end - Airplane - use array for gamerule storage } public > T getRule(GameRules.Key key) { - return (T) this.rules.get(key); // CraftBukkit - decompile error + // Gale start - Airplane - use array for gamerule storage + return key == null ? null : (T) this.gameruleArray[key.gameRuleIndex]; + //return (T) this.rules.get(key); // CraftBukkit - decompile error + // Gale end - Airplane - use array for gamerule storage } public CompoundTag createTag() { @@ -178,6 +195,10 @@ public class GameRules { } public static final class Key> { + // Gale start - Airplane - use array for gamerule storage + private static int lastGameRuleIndex = 0; + public final int gameRuleIndex = lastGameRuleIndex++; + // Gale end - Airplane - use array for gamerule storage final String id; private final GameRules.Category category;