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
Last revisionBoth sides next revision
tutorial:gamerule [2021/01/30 19:51] mysterious_devtutorial:gamerule [2021/12/24 22:00] – old revision restored (2021/09/23 16:10) modmuss50
Line 1: Line 1:
-====== Custom Gamerule ======+====== Adding a Gamerule ======
  
-==== Creating your gamerule ====+==== Registering a Gamerule ==== 
 + 
 +To create a gamerule, register it using the ''GameRuleRegistry'' of the Fabric API: 
 + 
 +<code java [enable_line_numbers="true"]> 
 +// Create and register a boolean game rule with the name "shouldPigsFly" which is true by default. 
 +public static final GameRules.Key<GameRules.BooleanRule> SHOULD_PIGS_FLY = 
 +GameRuleRegistry.register("shouldPigsFly", Category.MOBS, GameRuleFactory.createBooleanRule(true)); 
 +</code> 
 + 
 +==== Using the gamerule ==== 
 + 
 +To get access to the value of a game rule you need to get the server world: 
 + 
 +<code java [enable_line_numbers="true"]> 
 +world.getGameRules().getBoolean(SHOULD_PIGS_FLY) 
 +</code> 
 + 
 +==== Translation ==== 
 + 
 +The registered gamerule now shows up untranslated in the game rule 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): 
 + 
 +<code json> 
 +
 +  "gamerule.shouldPigsFly": "Pigs should fly" 
 +
 +</code>
tutorial/gamerule.txt · Last modified: 2021/12/24 22:00 by 127.0.0.1