Question World#setGameRule

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
102
3
16
18
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>.