GameRules class has a private field storing all types of game rules.
public class GameRules {
//...
private final TreeMap<String, GameRules.Value> rules = new TreeMap<>();
//...
}
If I use access transformer to make it public then I can add a new game rule.
But in GameRuleCommand class:
public class GameRuleCommand {
public static void register(CommandDispatcher<CommandSource> dispatcher) {
LiteralArgumentBuilder<CommandSource> literalargumentbuilder = Commands.literal("gamerule").requires((p_198491_0_) -> {
return p_198491_0_.hasPermissionLevel(2);
});
for(Entry<String, GameRules.ValueDefinition> entry : GameRules.getDefinitions().entrySet()) {
literalargumentbuilder.then(Commands.literal(entry.getKey()).executes((p_198489_1_) -> {
return queryRule(p_198489_1_.getSource(), entry.getKey());
}).then(entry.getValue().getType().createArgument("value").executes((p_198490_1_) -> {
return setRule(p_198490_1_.getSource(), entry.getKey(), p_198490_1_);
})));
}
dispatcher.register(literalargumentbuilder);
}
//...
}
It reads all the game rule types and creates the command format structure. If I add the game rule after the command registered, the /gamerule command will not work on my custom game rule.