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:09] 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"]>
-// Create and register a boolean game rule with the name "shouldPigsFly" which is true by default. +// Create and register a boolean gamerule, enabled by default 
-public static final GameRules.Key<GameRules.BooleanRuleSHOULD_PIGS_FLY +private static final Identifier        GAMERULE_IDENTIFIER     = Identifier.of("custom_mod_name","gamerule_name"); 
-GameRuleRegistry.register("shouldPigsFly", Category.MOBS, GameRuleFactory.createBooleanRule(true));+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(SHOULD_PIGS_FLY)+serverWorld.getGameRules().getValue(CUSTOM_GAMERULE);
 </code> </code>
  
 ==== Translation ==== ==== Translation ====
  
-The registered gamerule now shows up untranslated in the game rule list, which can be opened in the world creation screen. +The registered gamerule shows up untranslated in the gamerule list, which can be opened in the world creation screen. 
-To translate the name, add a ''gamerule.gameruleName'' entry (see [[tutorial:lang|lang]] for more info):+To translate the name, add an entry in the lang-file (see [[tutorial:lang|lang]] for more info):
  
 <code json> <code json>
 { {
-  "gamerule.shouldPigsFly": "Pigs should fly"+  "gamerule.custom_mod_name.gamerule_name": "Gamerule Name"
 } }
 </code> </code>
tutorial/gamerule.1632413362.txt.gz · Last modified: 2021/09/23 16:09 by jummit