Question World#setGameRule

  • After careful consideration and due to limited usage, we’ve made the decision to discontinue the PaperMC forums. Moving forward, we recommend using Hangar for plugin uploads, and for all other community discussions and support, please join us on Discord.

Xmark

New member
Oct 8, 2023
1
0
1
Goal: set given world's gamerule from a gamerule string (e.g. "doInsomnia")

Problem: return type of GameRule#getByName()


World#setGameRule() requires GameRule<T> as its rule parameter, but GameRule#getByName() provides GameRule<?>

the following code works, but the compiler complains:

Java:
String myGameRule = "doInsomnia";
boolean newValue = false;

GameRule gr = GameRule.getByName(myGameRule);
boolean success = getWorld().setGameRule(gr, newValue);

I can't use GameRule.DO_INSOMNIA because I'm getting the gamerule from a config file.

World#setGameRuleValue would in theory work since it takes String as both parameters, but it has be deprecated.

Any help would be appreciated
 
Last edited:

stefvanschie

Moderator
Staff member
Dec 17, 2021
145
7
29
28
There's not really an elegant way to go about this. Check that the game rule you load from the config actually has a boolean as type and then cast it to GameRule<Boolean>.