Minecraft Java Edition – How to Create and Use Custom Gamerules?

minecraft-commandsminecraft-java-edition

Recently, there were a few posts about creating custom gamerules, but no question or answer covered the creation and usage of custom gamerules. This post tries to uncover this, but it's just a link-only answer to a tutorial I didn't understand.

From version 1.9 (or maybe earlier), you can create custom gamerules, eg.

/gamerule customGamerule false

This will create a gamerule named customGamerule, currently set to false. It can be changed to true and Bach, but without effect. How can one add an effect to a custom gamerule?

Best Answer

In order to detect the value of any gamerule (both default and custom), CommandStats must be used, which will set the value of the gamerule as a score. Only the QueryResult trigger will be provided with a return value when running /gamerule <name>.

Prerequisites

Objective to hold the value.

/scoreboard objectives add Gamerules dummy

The target of CommandStats must be tracked in the objective in order to obtain the return value. In this case, a fake player named "$customGamerule" will be used to hold the value.

/scoreboard players set $customGamerule Gamerules 0

You can set the custom gamerule's value to any numerical value for detection. If you set it to "true" or "false", it will be interpreted as 1 and 0 respectively. If you set it to any other string, it will be 0.

/gamerule customGamerule 0
/gamerule customGamerule 1
/gamerule customGamerule 100
/gamerule customGamerule true

Detection

Repeating > Chain > Conditional Chain

  1. Stand on top of the command block and run the following yourself.

    /stats block ~ ~-1 ~ set QueryResult $customGamerule Gamerules
    

    Now every time the command block executes a command that provides a QueryResult return value, fake player "$customGamerule" will have their "Gamerules" score set equal to that value.

    The command that will be placed within that command block, which queries the value of the gamerule:

    /gamerule customGamerule
    
  2. In order to detect the fake player's value, /scoreboard players test must be used.

    /scoreboard players test $customGamerule Gamerules 1 1
    
  3. Conditional. Command to run if the "customGamerule" gamerule is set to 1.

    /say "customGamerule" is set to 1.
    

As long as the gamerule is set to "1" or "true", the /say command will execute.

Related Topic