How to add a game rule in a Minecraft mod
A game rule is a setting saved with one world and changed in game with /gamerule. Register it once at startup: GameRules.register(name, category, type) with BooleanValue.create(default) for a true or false rule and IntegerValue.create(default) for a number (Fabric API has GameRuleRegistry for this). From 1.21.11 game rules are registry entries with a namespaced id instead. Read it anywhere you have a level through its game rules. Config files are for the whole game; game rules are per world.
Works on Minecraft 1.20.1 to 26.3 on NeoForge; Minecraft 1.16.5 to 26.3 on Fabric; Minecraft 1.16.5 to 1.20.1 on Forge; Minecraft 1.12.2 on Forge. Each example was compiled against its loader and version before it shipped in Linen.
- Register the rule and keep the key or handle it returns.
- Call your register method from the mod’s startup so the class loads.
- Read the rule from the level’s game rules where your feature runs (getBoolean(RULE), or get(rule) on 1.21.11 and newer).
- Try /gamerule in a world; tab completion lists the rule.
What you can change
Section titled “What you can change”The example uses these values. Change them in the code.
| Setting | In the example | What it does |
|---|---|---|
| start | true |
Whether the rule starts on. |
For each loader and version
Section titled “For each loader and version”The example mod is examplemod in the package com.example.examplemod. Use your own mod id and package.
Fabric 1.16.5 to 1.21.1
Section titled “Fabric 1.16.5 to 1.21.1”Uses:
GameRuleRegistry.registerGameRuleFactory.createBooleanRuleGameRules.CategoryGameRules.getBoolean
src/main/java/com/example/examplemod/registry/MyRules.java
package com.example.examplemod.registry;
import net.fabricmc.fabric.api.gamerule.v1.GameRuleFactory;import net.fabricmc.fabric.api.gamerule.v1.GameRuleRegistry;import net.minecraft.world.level.GameRules;import net.minecraft.world.level.Level;
// The game rule "my_rule", true by default. Change it in a world with /gamerule my_rule true or false.public final class MyRules { public static final GameRules.Key<GameRules.BooleanValue> RULE = GameRuleRegistry.register("my_rule", GameRules.Category.MISC, GameRuleFactory.createBooleanRule(true));
private MyRules() { }
// Use this wherever your code should obey the rule. public static boolean enabled(Level level) { return level.getGameRules().getBoolean(RULE); }
// Called from the mod's startup code; loading this class registers the rule above. public static void register() { }}In your main mod class, inside onInitialize:
MyRules.register();NeoForge 1.20.1 to 1.21.1, Forge 1.19.4 to 1.20.1
Section titled “NeoForge 1.20.1 to 1.21.1, Forge 1.19.4 to 1.20.1”Uses:
GameRules.registerGameRules.BooleanValue.createGameRules.CategoryGameRules.getBoolean
src/main/java/com/example/examplemod/registry/MyRules.java
package com.example.examplemod.registry;
import net.minecraft.world.level.GameRules;import net.minecraft.world.level.Level;
// The game rule "my_rule", true by default. Change it in a world with /gamerule my_rule true or false.public final class MyRules { public static final GameRules.Key<GameRules.BooleanValue> RULE = GameRules.register("my_rule", GameRules.Category.MISC, GameRules.BooleanValue.create(true));
private MyRules() { }
// Use this wherever your code should obey the rule. public static boolean enabled(Level level) { return level.getGameRules().getBoolean(RULE); }
// Called from the mod's startup code; loading this class registers the rule above. public static void register() { }}In your main mod class, inside <init>:
MyRules.register();Fabric 1.21.2 to 1.21.10
Section titled “Fabric 1.21.2 to 1.21.10”Uses:
GameRuleRegistry.registerGameRuleFactory.createBooleanRuleGameRules.CategoryGameRules.getBoolean
src/main/java/com/example/examplemod/registry/MyRules.java
package com.example.examplemod.registry;
import net.fabricmc.fabric.api.gamerule.v1.GameRuleFactory;import net.fabricmc.fabric.api.gamerule.v1.GameRuleRegistry;import net.minecraft.world.level.GameRules;import net.minecraft.server.level.ServerLevel;
// The game rule "my_rule", true by default. Change it in a world with /gamerule my_rule true or false.public final class MyRules { public static final GameRules.Key<GameRules.BooleanValue> RULE = GameRuleRegistry.register("my_rule", GameRules.Category.MISC, GameRuleFactory.createBooleanRule(true));
private MyRules() { }
// Use this wherever your code should obey the rule. public static boolean enabled(ServerLevel level) { return level.getGameRules().getBoolean(RULE); }
// Called from the mod's startup code; loading this class registers the rule above. public static void register() { }}In your main mod class, inside onInitialize:
MyRules.register();NeoForge 1.21.2 to 1.21.10
Section titled “NeoForge 1.21.2 to 1.21.10”Uses:
GameRules.registerGameRules.BooleanValue.createGameRules.CategoryGameRules.getBoolean
src/main/java/com/example/examplemod/registry/MyRules.java
package com.example.examplemod.registry;
import net.minecraft.server.level.ServerLevel;import net.minecraft.world.level.GameRules;
// The game rule "my_rule", true by default. Change it in a world with /gamerule my_rule true or false.public final class MyRules { public static final GameRules.Key<GameRules.BooleanValue> RULE = GameRules.register("my_rule", GameRules.Category.MISC, GameRules.BooleanValue.create(true));
private MyRules() { }
// Use this wherever your code should obey the rule. public static boolean enabled(ServerLevel level) { return level.getGameRules().getBoolean(RULE); }
// Called from the mod's startup code; loading this class registers the rule above. public static void register() { }}In your main mod class, inside <init>:
MyRules.register();NeoForge 1.21.11 to 26.3
Section titled “NeoForge 1.21.11 to 26.3”Uses:
GameRules.registerBooleanGameRuleGameRuleCategoryGameRules.get
Since 1.21.11 game rules are registry entries (GameRule<T>): a modded rule takes a namespaced id, so the command is /gamerule <mod_id>:<name>.
src/main/java/com/example/examplemod/registry/MyRules.java
package com.example.examplemod.registry;
import net.minecraft.server.level.ServerLevel;import net.minecraft.world.level.gamerules.GameRule;import net.minecraft.world.level.gamerules.GameRuleCategory;import net.minecraft.world.level.gamerules.GameRules;
// The game rule "examplemod:my_rule", true by default. Change it in a world with /gamerule examplemod:my_rule true or false.public final class MyRules { public static final GameRule<Boolean> RULE = GameRules.registerBoolean("examplemod:my_rule", GameRuleCategory.MISC, true);
private MyRules() { }
// Use this wherever your code should obey the rule. public static boolean enabled(ServerLevel level) { return level.getGameRules().get(RULE); }
// Called from the mod's startup code; loading this class registers the rule above. public static void register() { }}In your main mod class, inside <init>:
MyRules.register();Fabric 1.21.11 to 26.3
Section titled “Fabric 1.21.11 to 26.3”Uses:
GameRuleBuilder.forBooleanGameRuleBuilder.buildAndRegisterGameRuleCategoryGameRules.get
From 1.21.11 a game rule is a GameRule<T> in a registry with a namespaced id, so the command is /gamerule <namespace>:<name>; it is read with level.getGameRules().get(rule) on a ServerLevel.
src/main/java/com/example/examplemod/registry/MyRules.java
package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;import net.fabricmc.fabric.api.gamerule.v1.GameRuleBuilder;import net.minecraft.server.level.ServerLevel;import net.minecraft.world.level.gamerules.GameRule;import net.minecraft.world.level.gamerules.GameRuleCategory;
// The game rule "examplemod:my_rule", true by default. Change it in a world with /gamerule examplemod:my_rule true or false.public final class MyRules { // Since 1.21.11 game rules are GameRule objects in a registry; Fabric API's GameRuleBuilder makes and registers one. public static final GameRule<Boolean> RULE = GameRuleBuilder.forBoolean(true) .category(GameRuleCategory.MISC) .buildAndRegister(ExampleMod.id("my_rule"));
private MyRules() { }
// Use this wherever your code should obey the rule. public static boolean enabled(ServerLevel level) { return level.getGameRules().get(RULE); }
// Called from the mod's startup code; loading this class registers the rule above. public static void register() { }}In your main mod class, inside onInitialize:
MyRules.register();Forge 1.17.1 to 1.19.3
Section titled “Forge 1.17.1 to 1.19.3”Uses:
GameRules.registerGameRules.BooleanValue.createGameRules.CategoryGameRules.getBoolean
GameRules.BooleanValue.create is package-private in 1.19.3 (public from 1.19.4): the access transformer line opens it up (m_46250_ is its SRG name).
src/main/java/com/example/examplemod/registry/MyRules.java
package com.example.examplemod.registry;
import net.minecraft.world.level.GameRules;import net.minecraft.world.level.Level;
// The game rule "my_rule", true by default. Change it in a world with /gamerule my_rule true or false.public final class MyRules { public static final GameRules.Key<GameRules.BooleanValue> RULE = GameRules.register("my_rule", GameRules.Category.MISC, GameRules.BooleanValue.create(true));
private MyRules() { }
// Use this wherever your code should obey the rule. public static boolean enabled(Level level) { return level.getGameRules().getBoolean(RULE); }
// Called from the mod's startup code; loading this class registers the rule above. public static void register() { }}In your main mod class, inside <init>:
MyRules.register();Add this line to src/main/resources/META-INF/accesstransformer.cfg:
public net.minecraft.world.level.GameRules$BooleanValue m_46250_(Z)Lnet/minecraft/world/level/GameRules$Type; # createForge 1.16.5
Section titled “Forge 1.16.5”Uses:
GameRules.registerGameRules.BooleanValue.createGameRules.CategoryGameRules.getBoolean
GameRules.BooleanValue.create is package-private in 1.16.5: the access transformer line opens it up (func_223568_b is its SRG name).
src/main/java/com/example/examplemod/registry/MyRules.java
package com.example.examplemod.registry;
import net.minecraft.world.GameRules;import net.minecraft.world.World;
// The game rule "my_rule", true by default. Change it in a world with /gamerule my_rule true or false.public final class MyRules { public static final GameRules.RuleKey<GameRules.BooleanValue> RULE = GameRules.register("my_rule", GameRules.Category.MISC, GameRules.BooleanValue.create(true));
private MyRules() { }
// Use this wherever your code should obey the rule. public static boolean enabled(World world) { return world.getGameRules().getBoolean(RULE); }
// Called from the mod's startup code; loading this class registers the rule above. public static void register() { }}In your main mod class, inside <init>:
MyRules.register();Add this line to src/main/resources/META-INF/accesstransformer.cfg:
public net.minecraft.world.GameRules$BooleanValue func_223568_b(Z)Lnet/minecraft/world/GameRules$RuleType; # createForge 1.12.2
Section titled “Forge 1.12.2”Uses:
GameRules.addGameRuleGameRules.getBooleanGameRules.ValueTypeWorldEvent.Load
In 1.12.2 the rule is added to the world’s rules with world.getGameRules().addGameRule(name, default, GameRules.ValueType.BOOLEAN_VALUE) when a world loads (WorldEvent.Load), and read with getBoolean(name).
src/main/java/com/example/examplemod/registry/MyRules.java
package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;import net.minecraft.world.GameRules;import net.minecraft.world.World;import net.minecraftforge.event.world.WorldEvent;import net.minecraftforge.fml.common.Mod;import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
// The game rule "my_rule", true by default. Change it in a world with /gamerule my_rule true or false. In 1.12.2 a rule is a name and a string value, added to each world's rules when it loads.@Mod.EventBusSubscriber(modid = ExampleMod.MOD_ID)public final class MyRules { public static final String RULE = "my_rule";
private MyRules() { }
@SubscribeEvent public static void onWorldLoad(WorldEvent.Load event) { GameRules rules = event.getWorld().getGameRules(); if (!rules.hasRule(RULE)) { rules.addGameRule(RULE, "true", GameRules.ValueType.BOOLEAN_VALUE); } }
// Use this wherever your code should obey the rule. public static boolean enabled(World world) { return world.getGameRules().getBoolean(RULE); }
// Called from the mod's startup code; the event handler above does the work. public static void register() { }}Add this line to src/main/resources/META-INF/accesstransformer.cfg:
public net.minecraft.world.GameRules$BooleanValue func_223568_b(Z)Lnet/minecraft/world/GameRules$RuleType; # createSources
Section titled “Sources”- https://docs.neoforged.net/docs/1.21.1/concepts/registries
- https://docs.fabricmc.net/develop/
- https://maven.fabricmc.net/docs/fabric-api-0.116.17+1.21.1/net/fabricmc/fabric/api/gamerule/v1/GameRuleFactory.html
Related how-tos
Section titled “Related how-tos”- An item that explodes where you aim
- An item that sets the time and weather
- Save data with the world
- A config value players can change
- Publish my mod on Modrinth or CurseForge
In Linen
Section titled “In Linen”Linen’s How-tos library has this page too, with the example for your own mod’s loader and Minecraft version. Hover a name in your code and Linen points to the how-tos that explain it. Both are free. Download Linen.