User Tools

Site Tools


tutorial:gamerule

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tutorial:gamerule [2021/09/23 16:01] – initial work on gamerule tutorial jummittutorial:gamerule [2025/12/09 19:10] (current) – 1.21.11 lang-file change for gamerules' identifier(s) solacekairos
Line 3: Line 3:
 ==== Registering a Gamerule ==== ==== Registering a Gamerule ====
  
-To create a gamerule you have to register it using the ''GameRuleRegistry'' of the Fabric API:+To create a gamerule, build and register it using the ''GameRuleBuilder'' of the Fabric API:
  
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
-public static final GameRules.Key<GameRules.BooleanRuleDO_SLEEP_CLEAR_WEATHER +// Create and register a boolean gamerule, enabled by default 
-GameRuleRegistry.register("clearWeatherAfterSleep", Category.PLAYER, GameRuleFactory.createBooleanRule(false));+private static final Identifier        GAMERULE_IDENTIFIER     = Identifier.of("custom_mod_name","gamerule_name"); 
 +public  static final GameRule<BooleanCUSTOM_BOOLEAN_GAMERULE GameRuleBuilder 
 +    .forBoolean(true) //default value declaration 
 +    .category(GameRuleCategory.MISC) 
 +    .buildAndRegister(GAMERULE_IDENTIFIER) 
 +;
 </code> </code>
  
 ==== Using the gamerule ==== ==== Using the gamerule ====
  
-To get access to the value of a game rule you need to get the server world:+To get access to the value of a gamerule, use a server-world:
  
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
-world.getGameRules().getBoolean(KeepTheRain.DO_SLEEP_CLEAR_WEATHER)+serverWorld.getGameRules().getValue(CUSTOM_GAMERULE);
 </code> </code>
  
 +==== Translation ====
 +
 +The registered gamerule shows up untranslated in the gamerule list, which can be opened in the world creation screen.
 +To translate the name, add an entry in the lang-file (see [[tutorial:lang|lang]] for more info):
 +
 +<code json>
 +{
 +  "gamerule.custom_mod_name.gamerule_name": "Gamerule Name"
 +}
 +</code>
tutorial/gamerule.1632412868.txt.gz · Last modified: 2021/09/23 16:01 by jummit