package net.minecraft.world.level; import com.mojang.brigadier.arguments.BoolArgumentType; import com.mojang.brigadier.arguments.IntegerArgumentType; import javax.annotation.Nullable; import com.mojang.brigadier.context.CommandContext; import net.minecraft.commands.Commands; import net.minecraft.commands.CommandSourceStack; import com.mojang.brigadier.builder.RequiredArgumentBuilder; import java.util.function.BiConsumer; import com.mojang.brigadier.arguments.ArgumentType; import java.util.function.Supplier; import net.minecraft.server.network.ServerGamePacketListenerImpl; import com.google.common.collect.Maps; import java.util.Comparator; import org.apache.logging.log4j.LogManager; import net.minecraft.world.entity.Entity; import net.minecraft.network.protocol.game.ClientboundEntityEventPacket; import java.util.Iterator; import net.minecraft.network.protocol.Packet; import net.minecraft.network.protocol.game.ClientboundGameEventPacket; import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.MinecraftServer; import net.minecraft.nbt.CompoundTag; import java.util.stream.Collector; import java.util.function.Function; import com.google.common.collect.ImmutableMap; import java.util.Map; import org.apache.logging.log4j.Logger; public class GameRules { private static final Logger LOGGER; private static final Map, Type> GAME_RULE_TYPES; public static final Key RULE_DOFIRETICK; public static final Key RULE_MOBGRIEFING; public static final Key RULE_KEEPINVENTORY; public static final Key RULE_DOMOBSPAWNING; public static final Key RULE_DOMOBLOOT; public static final Key RULE_DOBLOCKDROPS; public static final Key RULE_DOENTITYDROPS; public static final Key RULE_COMMANDBLOCKOUTPUT; public static final Key RULE_NATURAL_REGENERATION; public static final Key RULE_DAYLIGHT; public static final Key RULE_LOGADMINCOMMANDS; public static final Key RULE_SHOWDEATHMESSAGES; public static final Key RULE_RANDOMTICKING; public static final Key RULE_SENDCOMMANDFEEDBACK; public static final Key RULE_REDUCEDDEBUGINFO; public static final Key RULE_SPECTATORSGENERATECHUNKS; public static final Key RULE_SPAWN_RADIUS; public static final Key RULE_DISABLE_ELYTRA_MOVEMENT_CHECK; public static final Key RULE_MAX_ENTITY_CRAMMING; public static final Key RULE_WEATHER_CYCLE; public static final Key RULE_LIMITED_CRAFTING; public static final Key RULE_MAX_COMMAND_CHAIN_LENGTH; public static final Key RULE_ANNOUNCE_ADVANCEMENTS; public static final Key RULE_DISABLE_RAIDS; public static final Key RULE_DOINSOMNIA; public static final Key RULE_DO_IMMEDIATE_RESPAWN; public static final Key RULE_DROWNING_DAMAGE; public static final Key RULE_FALL_DAMAGE; public static final Key RULE_FIRE_DAMAGE; private final Map, Value> rules; private static > Key register(final String string, final Type e) { final Key d3 = new Key(string); final Type e2 = GameRules.GAME_RULE_TYPES.put(d3, e); if (e2 != null) { throw new IllegalStateException("Duplicate game rule registration for " + string); } return d3; } public GameRules() { this.rules = GameRules.GAME_RULE_TYPES.entrySet().stream().collect(ImmutableMap.toImmutableMap(Map.Entry::getKey, entry -> entry.getValue().createRule())); } public > T getRule(final Key d) { return (T)this.rules.get(d); } public CompoundTag createTag() { final CompoundTag jt2 = new CompoundTag(); this.rules.forEach((d, f) -> jt2.putString(d.id, f.serialize())); return jt2; } public void loadFromTag(final CompoundTag jt) { this.rules.forEach((d, f) -> { if (jt.contains(d.id)) { f.deserialize(jt.getString(d.id)); } }); } public static void visitGameRuleTypes(final GameRuleTypeVisitor b) { GameRules.GAME_RULE_TYPES.forEach((d, e) -> GameRules.cap(b, d, e)); } private static > void cap(final GameRuleTypeVisitor b, final Key d, final Type e) { final Key d2 = (Key)d; final Type e2 = (Type)e; b.visit(d2, e2); } public boolean getBoolean(final Key d) { return this.getRule(d).get(); } public int getInt(final Key d) { return this.getRule(d).get(); } static { LOGGER = LogManager.getLogger(); GAME_RULE_TYPES = Maps.newTreeMap(Comparator.comparing(d -> d.id)); RULE_DOFIRETICK = GameRules.register("doFireTick", create(true)); RULE_MOBGRIEFING = GameRules.register("mobGriefing", create(true)); RULE_KEEPINVENTORY = GameRules.register("keepInventory", create(false)); RULE_DOMOBSPAWNING = GameRules.register("doMobSpawning", create(true)); RULE_DOMOBLOOT = GameRules.register("doMobLoot", create(true)); RULE_DOBLOCKDROPS = GameRules.register("doTileDrops", create(true)); RULE_DOENTITYDROPS = GameRules.register("doEntityDrops", create(true)); RULE_COMMANDBLOCKOUTPUT = GameRules.register("commandBlockOutput", create(true)); RULE_NATURAL_REGENERATION = GameRules.register("naturalRegeneration", create(true)); RULE_DAYLIGHT = GameRules.register("doDaylightCycle", create(true)); RULE_LOGADMINCOMMANDS = GameRules.register("logAdminCommands", create(true)); RULE_SHOWDEATHMESSAGES = GameRules.register("showDeathMessages", create(true)); RULE_RANDOMTICKING = GameRules.register("randomTickSpeed", create(3)); RULE_SENDCOMMANDFEEDBACK = GameRules.register("sendCommandFeedback", create(true)); final int byte3; final Iterator iterator; ServerPlayer xe5; RULE_REDUCEDDEBUGINFO = GameRules.register("reducedDebugInfo", create(false, (minecraftServer, a) -> { byte3 = (a.get() ? 22 : 23); minecraftServer.getPlayerList().getPlayers().iterator(); while (iterator.hasNext()) { xe5 = iterator.next(); xe5.connection.send(new ClientboundEntityEventPacket(xe5, (byte)byte3)); } return; })); RULE_SPECTATORSGENERATECHUNKS = GameRules.register("spectatorsGenerateChunks", create(true)); RULE_SPAWN_RADIUS = GameRules.register("spawnRadius", create(10)); RULE_DISABLE_ELYTRA_MOVEMENT_CHECK = GameRules.register("disableElytraMovementCheck", create(false)); RULE_MAX_ENTITY_CRAMMING = GameRules.register("maxEntityCramming", create(24)); RULE_WEATHER_CYCLE = GameRules.register("doWeatherCycle", create(true)); RULE_LIMITED_CRAFTING = GameRules.register("doLimitedCrafting", create(false)); RULE_MAX_COMMAND_CHAIN_LENGTH = GameRules.register("maxCommandChainLength", create(65536)); RULE_ANNOUNCE_ADVANCEMENTS = GameRules.register("announceAdvancements", create(true)); RULE_DISABLE_RAIDS = GameRules.register("disableRaids", create(false)); RULE_DOINSOMNIA = GameRules.register("doInsomnia", create(true)); final Iterator iterator2; ServerPlayer xe6; ServerGamePacketListenerImpl connection; final ClientboundGameEventPacket lt; RULE_DO_IMMEDIATE_RESPAWN = GameRules.register("doImmediateRespawn", create(false, (minecraftServer, a) -> { minecraftServer.getPlayerList().getPlayers().iterator(); while (iterator2.hasNext()) { xe6 = iterator2.next(); connection = xe6.connection; new ClientboundGameEventPacket(11, a.get() ? 1.0f : 0.0f); connection.send(lt); } return; })); RULE_DROWNING_DAMAGE = GameRules.register("drowningDamage", create(true)); RULE_FALL_DAMAGE = GameRules.register("fallDamage", create(true)); RULE_FIRE_DAMAGE = GameRules.register("fireDamage", create(true)); } public static final class Key> { private final String id; public Key(final String string) { this.id = string; } @Override public String toString() { return this.id; } @Override public boolean equals(final Object object) { return this == object || (object instanceof Key && ((Key)object).id.equals(this.id)); } @Override public int hashCode() { return this.id.hashCode(); } public String getId() { return this.id; } } public static class Type> { private final Supplier> argument; private final Function, T> constructor; private final BiConsumer callback; private Type(final Supplier> supplier, final Function, T> function, final BiConsumer biConsumer) { this.argument = supplier; this.constructor = function; this.callback = biConsumer; } public RequiredArgumentBuilder createArgument(final String string) { return Commands.argument(string, this.argument.get()); } public T createRule() { return this.constructor.apply(this); } } public abstract static class Value> { private final Type type; public Value(final Type e) { this.type = e; } protected abstract void updateFromArgument(final CommandContext commandContext, final String string); public void setFromArgument(final CommandContext commandContext, final String string) { this.updateFromArgument(commandContext, string); this.onChanged(((CommandSourceStack)commandContext.getSource()).getServer()); } protected void onChanged(@Nullable final MinecraftServer minecraftServer) { if (minecraftServer != null) { ((Type)this.type).callback.accept(minecraftServer, this.getSelf()); } } protected abstract void deserialize(final String string); protected abstract String serialize(); @Override public String toString() { return this.serialize(); } public abstract int getCommandResult(); protected abstract T getSelf(); } public static class IntegerValue extends Value { private int value; private static Type create(final int integer, final BiConsumer biConsumer) { return new Type((Supplier)IntegerArgumentType::integer, e -> new IntegerValue(e, integer), (BiConsumer)biConsumer); } private static Type create(final int integer) { return create(integer, (minecraftServer, c) -> {}); } public IntegerValue(final Type e, final int integer) { super(e); this.value = integer; } @Override protected void updateFromArgument(final CommandContext commandContext, final String string) { this.value = IntegerArgumentType.getInteger((CommandContext)commandContext, string); } public int get() { return this.value; } @Override protected String serialize() { return Integer.toString(this.value); } @Override protected void deserialize(final String string) { this.value = safeParse(string); } private static int safeParse(final String string) { if (!string.isEmpty()) { try { return Integer.parseInt(string); } catch (NumberFormatException numberFormatException2) { GameRules.LOGGER.warn("Failed to parse integer {}", string); } } return 0; } @Override public int getCommandResult() { return this.value; } @Override protected IntegerValue getSelf() { return this; } } public static class BooleanValue extends Value { private boolean value; private static Type create(final boolean boolean1, final BiConsumer biConsumer) { return new Type((Supplier)BoolArgumentType::bool, e -> new BooleanValue(e, boolean1), (BiConsumer)biConsumer); } private static Type create(final boolean boolean1) { return create(boolean1, (minecraftServer, a) -> {}); } public BooleanValue(final Type e, final boolean boolean2) { super(e); this.value = boolean2; } @Override protected void updateFromArgument(final CommandContext commandContext, final String string) { this.value = BoolArgumentType.getBool((CommandContext)commandContext, string); } public boolean get() { return this.value; } public void set(final boolean boolean1, @Nullable final MinecraftServer minecraftServer) { this.value = boolean1; this.onChanged(minecraftServer); } @Override protected String serialize() { return Boolean.toString(this.value); } @Override protected void deserialize(final String string) { this.value = Boolean.parseBoolean(string); } @Override public int getCommandResult() { return this.value ? 1 : 0; } @Override protected BooleanValue getSelf() { return this; } } @FunctionalInterface public interface GameRuleTypeVisitor { > void visit(final Key d, final Type e); } }