4
4
import com .google .common .collect .HashBiMap ;
5
5
import com .google .common .collect .ImmutableList ;
6
6
import com .google .common .collect .ImmutableMap ;
7
+ import com .mojang .brigadier .arguments .BoolArgumentType ;
8
+ import com .mojang .brigadier .arguments .IntegerArgumentType ;
7
9
import io .izzel .arclight .api .EnumHelper ;
8
10
import io .izzel .arclight .api .Unsafe ;
9
11
import io .izzel .arclight .common .bridge .bukkit .EntityTypeBridge ;
28
30
import net .minecraft .world .item .Item ;
29
31
import net .minecraft .world .item .Items ;
30
32
import net .minecraft .world .item .crafting .CookingBookCategory ;
33
+ import net .minecraft .world .level .GameRules ;
31
34
import net .minecraft .world .level .block .Block ;
32
35
import net .minecraft .world .level .block .Blocks ;
33
36
import net .minecraft .world .level .dimension .LevelStem ;
34
- import org .bukkit .Art ;
35
- import org .bukkit .Material ;
36
- import org .bukkit .NamespacedKey ;
37
- import org .bukkit .Statistic ;
38
- import org .bukkit .World ;
37
+ import org .bukkit .*;
39
38
import org .bukkit .block .Biome ;
40
39
import org .bukkit .craftbukkit .v .CraftStatistic ;
41
40
import org .bukkit .craftbukkit .v .inventory .CraftRecipe ;
50
49
import org .bukkit .entity .SpawnCategory ;
51
50
import org .bukkit .potion .PotionType ;
52
51
52
+ import java .lang .reflect .Constructor ;
53
53
import java .lang .reflect .Field ;
54
+ import java .lang .reflect .InvocationTargetException ;
54
55
import java .lang .reflect .Modifier ;
55
56
import java .util .ArrayList ;
56
57
import java .util .Arrays ;
@@ -93,6 +94,7 @@ public static void registerAll(DedicatedServer console) {
93
94
loadEndDragonPhase ();
94
95
loadCookingBookCategory ();
95
96
loadFluids ();
97
+ loadGameRules ();
96
98
try {
97
99
for (var field : org .bukkit .Registry .class .getFields ()) {
98
100
if (Modifier .isStatic (field .getModifiers ()) && field .get (null ) instanceof org .bukkit .Registry .SimpleRegistry <?> registry ) {
@@ -103,6 +105,44 @@ public static void registerAll(DedicatedServer console) {
103
105
}
104
106
}
105
107
108
+ private static void loadGameRules () {
109
+ Map <String , GameRule <?>> gameRules ;
110
+ Constructor <GameRule > constructor ;
111
+ try {
112
+ var rules = GameRule .class .getDeclaredField ("gameRules" );
113
+ rules .setAccessible (true );
114
+ gameRules = (Map <String , GameRule <?>>) rules .get (null );
115
+ constructor = GameRule .class .getDeclaredConstructor (String .class , Class .class );
116
+ constructor .setAccessible (true );
117
+ } catch (ReflectiveOperationException e ) {
118
+ ArclightServer .LOGGER .warn ("Cannot register all custom game rules for bukkit!" , e );
119
+ ArclightServer .LOGGER .warn ("This is a bug, and will cause commands like mvgamerule not working properly. Please report this!" );
120
+ return ;
121
+ }
122
+ GameRules .visitGameRuleTypes (new GameRules .GameRuleTypeVisitor () {
123
+ @ Override
124
+ public <T extends GameRules .Value <T >> void visit (GameRules .Key <T > key , GameRules .Type <T > type ) {
125
+ if (!gameRules .containsKey (key .getId ())) {
126
+ Class clazz ;
127
+ var argType = type .createArgument ("arclight" ).getType ();
128
+ if (argType instanceof BoolArgumentType ) {
129
+ clazz = Boolean .class ;
130
+ } else if (argType instanceof IntegerArgumentType ) {
131
+ clazz = Integer .class ;
132
+ } else {
133
+ clazz = String .class ;
134
+ }
135
+ try {
136
+ var instance = constructor .newInstance (key .getId (), clazz );
137
+ gameRules .put (key .getId (), instance );
138
+ } catch (ReflectiveOperationException e ) {
139
+ ArclightServer .LOGGER .warn ("Cannot register custom game rule {} for bukkit!" , key .getId (), e );
140
+ }
141
+ }
142
+ }
143
+ });
144
+ }
145
+
106
146
private static void loadFluids () {
107
147
var id = org .bukkit .Fluid .values ().length ;
108
148
var newTypes = new ArrayList <org .bukkit .Fluid >();
0 commit comments