Skip to content

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.

  1. Register the rule and keep the key or handle it returns.
  2. Call your register method from the mod’s startup so the class loads.
  3. Read the rule from the level’s game rules where your feature runs (getBoolean(RULE), or get(rule) on 1.21.11 and newer).
  4. Try /gamerule in a world; tab completion lists the rule.

The example uses these values. Change them in the code.

Setting In the example What it does
start true Whether the rule starts on.

The example mod is examplemod in the package com.example.examplemod. Use your own mod id and package.

Uses:

  • GameRuleRegistry.register
  • GameRuleFactory.createBooleanRule
  • GameRules.Category
  • GameRules.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.register
  • GameRules.BooleanValue.create
  • GameRules.Category
  • GameRules.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();

Uses:

  • GameRuleRegistry.register
  • GameRuleFactory.createBooleanRule
  • GameRules.Category
  • GameRules.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();

Uses:

  • GameRules.register
  • GameRules.BooleanValue.create
  • GameRules.Category
  • GameRules.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();

Uses:

  • GameRules.registerBoolean
  • GameRule
  • GameRuleCategory
  • GameRules.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();

Uses:

  • GameRuleBuilder.forBoolean
  • GameRuleBuilder.buildAndRegister
  • GameRuleCategory
  • GameRules.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();

Uses:

  • GameRules.register
  • GameRules.BooleanValue.create
  • GameRules.Category
  • GameRules.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; # create

Uses:

  • GameRules.register
  • GameRules.BooleanValue.create
  • GameRules.Category
  • GameRules.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; # create

Uses:

  • GameRules.addGameRule
  • GameRules.getBoolean
  • GameRules.ValueType
  • WorldEvent.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; # create

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.