| 1 | package net.minecraft.world.level.gamerules;
|
| 2 |
|
| 3 | import com.mojang.brigadier.arguments.ArgumentType;
|
| 4 | import com.mojang.brigadier.arguments.BoolArgumentType;
|
| 5 | import com.mojang.brigadier.arguments.IntegerArgumentType;
|
| 6 | import com.mojang.logging.LogUtils;
|
| 7 | import com.mojang.serialization.Codec;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Objects;
|
| 10 | import java.util.function.ToIntFunction;
|
| 11 | import java.util.stream.Stream;
|
| 12 | import net.minecraft.SharedConstants;
|
| 13 | import net.minecraft.core.Holder;
|
| 14 | import net.minecraft.core.Registry;
|
| 15 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 16 | import net.minecraft.server.MinecraftServer;
|
| 17 | import net.minecraft.world.flag.FeatureFlagSet;
|
| 18 | import net.minecraft.world.flag.FeatureFlags;
|
| 19 | import org.jspecify.annotations.Nullable;
|
| 20 | import org.slf4j.Logger;
|
| 21 |
|
| 22 | public class GameRules {
|
| 23 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 24 | public static final GameRule<Boolean> ADVANCE_TIME = registerBoolean("advance_time", GameRuleCategory.UPDATES, !SharedConstants.DEBUG_WORLD_RECREATE);
|
| 25 | public static final GameRule<Boolean> ADVANCE_WEATHER = registerBoolean("advance_weather", GameRuleCategory.UPDATES, !SharedConstants.DEBUG_WORLD_RECREATE);
|
| 26 | public static final GameRule<Boolean> ALLOW_ENTERING_NETHER_USING_PORTALS = registerBoolean("allow_entering_nether_using_portals", GameRuleCategory.MISC, true);
|
| 27 | public static final GameRule<Boolean> BLOCK_DROPS = registerBoolean("block_drops", GameRuleCategory.DROPS, true);
|
| 28 | public static final GameRule<Boolean> BLOCK_EXPLOSION_DROP_DECAY = registerBoolean("block_explosion_drop_decay", GameRuleCategory.DROPS, true);
|
| 29 | public static final GameRule<Boolean> COMMAND_BLOCKS_WORK = registerBoolean("command_blocks_work", GameRuleCategory.MISC, true);
|
| 30 | public static final GameRule<Boolean> COMMAND_BLOCK_OUTPUT = registerBoolean("command_block_output", GameRuleCategory.CHAT, true);
|
| 31 | public static final GameRule<Boolean> DROWNING_DAMAGE = registerBoolean("drowning_damage", GameRuleCategory.PLAYER, true);
|
| 32 | public static final GameRule<Boolean> ELYTRA_MOVEMENT_CHECK = registerBoolean("elytra_movement_check", GameRuleCategory.PLAYER, true);
|
| 33 | public static final GameRule<Boolean> ENDER_PEARLS_VANISH_ON_DEATH = registerBoolean("ender_pearls_vanish_on_death", GameRuleCategory.PLAYER, true);
|
| 34 | public static final GameRule<Boolean> ENTITY_DROPS = registerBoolean("entity_drops", GameRuleCategory.DROPS, true);
|
| 35 | public static final GameRule<Boolean> FALL_DAMAGE = registerBoolean("fall_damage", GameRuleCategory.PLAYER, true);
|
| 36 | public static final GameRule<Boolean> FIRE_DAMAGE = registerBoolean("fire_damage", GameRuleCategory.PLAYER, true);
|
| 37 | public static final GameRule<Integer> FIRE_SPREAD_RADIUS_AROUND_PLAYER = registerInteger("fire_spread_radius_around_player", GameRuleCategory.UPDATES, 128, -1);
|
| 38 | public static final GameRule<Boolean> FORGIVE_DEAD_PLAYERS = registerBoolean("forgive_dead_players", GameRuleCategory.MOBS, true);
|
| 39 | public static final GameRule<Boolean> FREEZE_DAMAGE = registerBoolean("freeze_damage", GameRuleCategory.PLAYER, true);
|
| 40 | public static final GameRule<Boolean> GLOBAL_SOUND_EVENTS = registerBoolean("global_sound_events", GameRuleCategory.MISC, true);
|
| 41 | public static final GameRule<Boolean> IMMEDIATE_RESPAWN = registerBoolean("immediate_respawn", GameRuleCategory.PLAYER, false);
|
| 42 | public static final GameRule<Boolean> KEEP_INVENTORY = registerBoolean("keep_inventory", GameRuleCategory.PLAYER, false);
|
| 43 | public static final GameRule<Boolean> LAVA_SOURCE_CONVERSION = registerBoolean("lava_source_conversion", GameRuleCategory.UPDATES, false);
|
| 44 | public static final GameRule<Boolean> LIMITED_CRAFTING = registerBoolean("limited_crafting", GameRuleCategory.PLAYER, false);
|
| 45 | public static final GameRule<Boolean> LOCATOR_BAR = registerBoolean("locator_bar", GameRuleCategory.PLAYER, true);
|
| 46 | public static final GameRule<Boolean> LOG_ADMIN_COMMANDS = registerBoolean("log_admin_commands", GameRuleCategory.CHAT, true);
|
| 47 | public static final GameRule<Integer> MAX_BLOCK_MODIFICATIONS = registerInteger("max_block_modifications", GameRuleCategory.MISC, 32768, 1);
|
| 48 | public static final GameRule<Integer> MAX_COMMAND_FORKS = registerInteger("max_command_forks", GameRuleCategory.MISC, 65536, 0);
|
| 49 | public static final GameRule<Integer> MAX_COMMAND_SEQUENCE_LENGTH = registerInteger("max_command_sequence_length", GameRuleCategory.MISC, 65536, 0);
|
| 50 | public static final GameRule<Integer> MAX_ENTITY_CRAMMING = registerInteger("max_entity_cramming", GameRuleCategory.MOBS, 24, 0);
|
| 51 | public static final GameRule<Integer> MAX_MINECART_SPEED = registerInteger(
|
| 52 | "max_minecart_speed", GameRuleCategory.MISC, 8, 1, 1000, FeatureFlagSet.of(FeatureFlags.MINECART_IMPROVEMENTS)
|
| 53 | );
|
| 54 | public static final GameRule<Integer> MAX_SNOW_ACCUMULATION_HEIGHT = registerInteger("max_snow_accumulation_height", GameRuleCategory.UPDATES, 1, 0, 8);
|
| 55 | public static final GameRule<Boolean> MOB_DROPS = registerBoolean("mob_drops", GameRuleCategory.DROPS, true);
|
| 56 | public static final GameRule<Boolean> MOB_EXPLOSION_DROP_DECAY = registerBoolean("mob_explosion_drop_decay", GameRuleCategory.DROPS, true);
|
| 57 | public static final GameRule<Boolean> MOB_GRIEFING = registerBoolean("mob_griefing", GameRuleCategory.MOBS, true);
|
| 58 | public static final GameRule<Boolean> NATURAL_HEALTH_REGENERATION = registerBoolean("natural_health_regeneration", GameRuleCategory.PLAYER, true);
|
| 59 | public static final GameRule<Boolean> PLAYER_MOVEMENT_CHECK = registerBoolean("player_movement_check", GameRuleCategory.PLAYER, true);
|
| 60 | public static final GameRule<Integer> PLAYERS_NETHER_PORTAL_CREATIVE_DELAY = registerInteger(
|
| 61 | "players_nether_portal_creative_delay", GameRuleCategory.PLAYER, 0, 0
|
| 62 | );
|
| 63 | public static final GameRule<Integer> PLAYERS_NETHER_PORTAL_DEFAULT_DELAY = registerInteger(
|
| 64 | "players_nether_portal_default_delay", GameRuleCategory.PLAYER, 80, 0
|
| 65 | );
|
| 66 | public static final GameRule<Integer> PLAYERS_SLEEPING_PERCENTAGE = registerInteger("players_sleeping_percentage", GameRuleCategory.PLAYER, 100, 0);
|
| 67 | public static final GameRule<Boolean> PROJECTILES_CAN_BREAK_BLOCKS = registerBoolean("projectiles_can_break_blocks", GameRuleCategory.DROPS, true);
|
| 68 | public static final GameRule<Boolean> PVP = registerBoolean("pvp", GameRuleCategory.PLAYER, true);
|
| 69 | public static final GameRule<Boolean> RAIDS = registerBoolean("raids", GameRuleCategory.MOBS, true);
|
| 70 | public static final GameRule<Integer> RANDOM_TICK_SPEED = registerInteger("random_tick_speed", GameRuleCategory.UPDATES, 3, 0);
|
| 71 | public static final GameRule<Boolean> REDUCED_DEBUG_INFO = registerBoolean("reduced_debug_info", GameRuleCategory.MISC, false);
|
| 72 | public static final GameRule<Integer> RESPAWN_RADIUS = registerInteger("respawn_radius", GameRuleCategory.PLAYER, 10, 0);
|
| 73 | public static final GameRule<Boolean> SEND_COMMAND_FEEDBACK = registerBoolean("send_command_feedback", GameRuleCategory.CHAT, true);
|
| 74 | public static final GameRule<Boolean> SHOW_ADVANCEMENT_MESSAGES = registerBoolean("show_advancement_messages", GameRuleCategory.CHAT, true);
|
| 75 | public static final GameRule<Boolean> SHOW_DEATH_MESSAGES = registerBoolean("show_death_messages", GameRuleCategory.CHAT, true);
|
| 76 | public static final GameRule<Boolean> SPAWNER_BLOCKS_WORK = registerBoolean("spawner_blocks_work", GameRuleCategory.MISC, true);
|
| 77 | public static final GameRule<Boolean> SPAWN_MOBS = registerBoolean("spawn_mobs", GameRuleCategory.SPAWNING, true);
|
| 78 | public static final GameRule<Boolean> SPAWN_MONSTERS = registerBoolean("spawn_monsters", GameRuleCategory.SPAWNING, true);
|
| 79 | public static final GameRule<Boolean> SPAWN_PATROLS = registerBoolean("spawn_patrols", GameRuleCategory.SPAWNING, true);
|
| 80 | public static final GameRule<Boolean> SPAWN_PHANTOMS = registerBoolean("spawn_phantoms", GameRuleCategory.SPAWNING, true);
|
| 81 | public static final GameRule<Boolean> SPAWN_WANDERING_TRADERS = registerBoolean("spawn_wandering_traders", GameRuleCategory.SPAWNING, true);
|
| 82 | public static final GameRule<Boolean> SPAWN_WARDENS = registerBoolean("spawn_wardens", GameRuleCategory.SPAWNING, true);
|
| 83 | public static final GameRule<Boolean> SPECTATORS_GENERATE_CHUNKS = registerBoolean("spectators_generate_chunks", GameRuleCategory.PLAYER, true);
|
| 84 | public static final GameRule<Boolean> SPREAD_VINES = registerBoolean("spread_vines", GameRuleCategory.UPDATES, true);
|
| 85 | public static final GameRule<Boolean> TNT_EXPLODES = registerBoolean("tnt_explodes", GameRuleCategory.MISC, true);
|
| 86 | public static final GameRule<Boolean> TNT_EXPLOSION_DROP_DECAY = registerBoolean("tnt_explosion_drop_decay", GameRuleCategory.DROPS, false);
|
| 87 | public static final GameRule<Boolean> UNIVERSAL_ANGER = registerBoolean("universal_anger", GameRuleCategory.MOBS, false);
|
| 88 | public static final GameRule<Boolean> WATER_SOURCE_CONVERSION = registerBoolean("water_source_conversion", GameRuleCategory.UPDATES, true);
|
| 89 | private final GameRuleMap rules;
|
| 90 |
|
| 91 | public static Codec<GameRules> codec(final FeatureFlagSet enabledFeatures) {
|
| 92 | return GameRuleMap.CODEC.xmap(map -> new GameRules(enabledFeatures, map), gameRules -> gameRules.rules);
|
| 93 | }
|
| 94 |
|
| 95 | public GameRules(final FeatureFlagSet enabledFeatures, final GameRuleMap map) {
|
| 96 | BuiltInRegistries.GAME_RULE.stream().forEach(gameRule -> {
|
| 97 | if (gameRule.isEnabled(enabledFeatures)) {
|
| 98 | if (!map.has((GameRule<?>)gameRule)) {
|
| 99 | map.reset((GameRule<?>)gameRule);
|
| 100 | }
|
| 101 | } else if (map.has((GameRule<?>)gameRule)) {
|
| 102 | map.remove((GameRule<?>)gameRule);
|
| 103 | }
|
| 104 | });
|
| 105 | this.rules = map;
|
| 106 | }
|
| 107 |
|
| 108 | public GameRules(final FeatureFlagSet enabledFeatures) {
|
| 109 | this.rules = GameRuleMap.of(BuiltInRegistries.GAME_RULE.filterFeatures(enabledFeatures).listElements().map(Holder::value));
|
| 110 | }
|
| 111 |
|
| 112 | public GameRules(final List<GameRule<?>> rules) {
|
| 113 | this.rules = GameRuleMap.of(rules.stream());
|
| 114 | }
|
| 115 |
|
| 116 | public Stream<GameRule<?>> availableRules() {
|
| 117 | return this.rules.keySet().stream();
|
| 118 | }
|
| 119 |
|
| 120 | public <T> T get(final GameRule<T> gameRule) {
|
| 121 | T value = this.rules.get(gameRule);
|
| 122 | if (value == null) {
|
| 123 | throw new IllegalArgumentException("Tried to access invalid game rule " + gameRule.getIdentifierWithFallback());
|
| 124 | } else {
|
| 125 | return value;
|
| 126 | }
|
| 127 | }
|
| 128 |
|
| 129 | public <T> void set(final GameRule<T> gameRule, final T value, final @Nullable MinecraftServer server) {
|
| 130 | if (!this.rules.has(gameRule)) {
|
| 131 | LOGGER.warn("Tried to set invalid game rule '{}' to value '{}'", gameRule.getIdentifierWithFallback(), value);
|
| 132 | } else {
|
| 133 | this.rules.set(gameRule, value);
|
| 134 | if (server != null) {
|
| 135 | server.onGameRuleChanged(gameRule, value);
|
| 136 | }
|
| 137 | }
|
| 138 | }
|
| 139 |
|
| 140 | public GameRules copy(final FeatureFlagSet enabledFeatures) {
|
| 141 | return new GameRules(enabledFeatures, GameRuleMap.copyOf(this.rules));
|
| 142 | }
|
| 143 |
|
| 144 | public void setAll(final GameRules other, final @Nullable MinecraftServer server) {
|
| 145 | this.setAll(other.rules, server);
|
| 146 | }
|
| 147 |
|
| 148 | public void setAll(final GameRuleMap gameRulesMap, final @Nullable MinecraftServer server) {
|
| 149 | gameRulesMap.keySet().forEach(gameRule -> this.setFromOther(gameRulesMap, (GameRule<?>)gameRule, server));
|
| 150 | }
|
| 151 |
|
| 152 | private <T> void setFromOther(final GameRuleMap gameRulesMap, final GameRule<T> gameRule, final @Nullable MinecraftServer server) {
|
| 153 | this.set(gameRule, Objects.requireNonNull(gameRulesMap.get(gameRule)), server);
|
| 154 | }
|
| 155 |
|
| 156 | public void visitGameRuleTypes(final GameRuleTypeVisitor visitor) {
|
| 157 | this.rules.keySet().forEach(gameRule -> {
|
| 158 | visitor.visit((GameRule<?>)gameRule);
|
| 159 | gameRule.callVisitor(visitor);
|
| 160 | });
|
| 161 | }
|
| 162 |
|
| 163 | private static GameRule<Boolean> registerBoolean(final String id, final GameRuleCategory category, final boolean defaultValue) {
|
| 164 | return register(
|
| 165 | id, category, GameRuleType.BOOL, BoolArgumentType.bool(), Codec.BOOL, defaultValue, FeatureFlagSet.of(), GameRuleTypeVisitor::visitBoolean, b -> b ? 1 : 0
|
| 166 | );
|
| 167 | }
|
| 168 |
|
| 169 | private static GameRule<Integer> registerInteger(final String id, final GameRuleCategory category, final int defaultValue, final int min) {
|
| 170 | return registerInteger(id, category, defaultValue, min, Integer.MAX_VALUE, FeatureFlagSet.of());
|
| 171 | }
|
| 172 |
|
| 173 | private static GameRule<Integer> registerInteger(final String id, final GameRuleCategory category, final int defaultValue, final int min, final int max) {
|
| 174 | return registerInteger(id, category, defaultValue, min, max, FeatureFlagSet.of());
|
| 175 | }
|
| 176 |
|
| 177 | private static GameRule<Integer> registerInteger(
|
| 178 | final String id, final GameRuleCategory category, final int defaultValue, final int min, final int max, final FeatureFlagSet requiredFeatures
|
| 179 | ) {
|
| 180 | return register(
|
| 181 | id,
|
| 182 | category,
|
| 183 | GameRuleType.INT,
|
| 184 | IntegerArgumentType.integer(min, max),
|
| 185 | Codec.intRange(min, max),
|
| 186 | defaultValue,
|
| 187 | requiredFeatures,
|
| 188 | GameRuleTypeVisitor::visitInteger,
|
| 189 | i -> i
|
| 190 | );
|
| 191 | }
|
| 192 |
|
| 193 | private static <T> GameRule<T> register(
|
| 194 | final String id,
|
| 195 | final GameRuleCategory category,
|
| 196 | final GameRuleType typeHint,
|
| 197 | final ArgumentType<T> argumentType,
|
| 198 | final Codec<T> codec,
|
| 199 | final T defaultValue,
|
| 200 | final FeatureFlagSet requiredFeatures,
|
| 201 | final GameRules.VisitorCaller<T> visitorCaller,
|
| 202 | final ToIntFunction<T> commandResultFunction
|
| 203 | ) {
|
| 204 | return Registry.register(
|
| 205 | BuiltInRegistries.GAME_RULE,
|
| 206 | id,
|
| 207 | new GameRule<>(category, typeHint, argumentType, visitorCaller, codec, commandResultFunction, defaultValue, requiredFeatures)
|
| 208 | );
|
| 209 | }
|
| 210 |
|
| 211 | public static GameRule<?> bootstrap(final Registry<GameRule<?>> registry) {
|
| 212 | return ADVANCE_TIME;
|
| 213 | }
|
| 214 |
|
| 215 | public <T> String getAsString(final GameRule<T> gameRule) {
|
| 216 | return gameRule.serialize(this.get(gameRule));
|
| 217 | }
|
| 218 |
|
| 219 | public interface VisitorCaller<T> {
|
| 220 | void call(GameRuleTypeVisitor visitor, GameRule<T> key);
|
| 221 | }
|
| 222 | }
|
| 223 |
|