Skip to content

Commit 5c69d66

Browse files
committed
Register custom game rules to bukkit; close IzzelAliz#1525
1 parent 6d22adf commit 5c69d66

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

arclight-common/src/main/java/io/izzel/arclight/common/mod/server/BukkitRegistry.java

+45-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.google.common.collect.HashBiMap;
55
import com.google.common.collect.ImmutableList;
66
import com.google.common.collect.ImmutableMap;
7+
import com.mojang.brigadier.arguments.BoolArgumentType;
8+
import com.mojang.brigadier.arguments.IntegerArgumentType;
79
import io.izzel.arclight.api.EnumHelper;
810
import io.izzel.arclight.api.Unsafe;
911
import io.izzel.arclight.common.bridge.bukkit.EntityTypeBridge;
@@ -28,14 +30,11 @@
2830
import net.minecraft.world.item.Item;
2931
import net.minecraft.world.item.Items;
3032
import net.minecraft.world.item.crafting.CookingBookCategory;
33+
import net.minecraft.world.level.GameRules;
3134
import net.minecraft.world.level.block.Block;
3235
import net.minecraft.world.level.block.Blocks;
3336
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.*;
3938
import org.bukkit.block.Biome;
4039
import org.bukkit.craftbukkit.v.CraftStatistic;
4140
import org.bukkit.craftbukkit.v.inventory.CraftRecipe;
@@ -50,7 +49,9 @@
5049
import org.bukkit.entity.SpawnCategory;
5150
import org.bukkit.potion.PotionType;
5251

52+
import java.lang.reflect.Constructor;
5353
import java.lang.reflect.Field;
54+
import java.lang.reflect.InvocationTargetException;
5455
import java.lang.reflect.Modifier;
5556
import java.util.ArrayList;
5657
import java.util.Arrays;
@@ -93,6 +94,7 @@ public static void registerAll(DedicatedServer console) {
9394
loadEndDragonPhase();
9495
loadCookingBookCategory();
9596
loadFluids();
97+
loadGameRules();
9698
try {
9799
for (var field : org.bukkit.Registry.class.getFields()) {
98100
if (Modifier.isStatic(field.getModifiers()) && field.get(null) instanceof org.bukkit.Registry.SimpleRegistry<?> registry) {
@@ -103,6 +105,44 @@ public static void registerAll(DedicatedServer console) {
103105
}
104106
}
105107

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+
106146
private static void loadFluids() {
107147
var id = org.bukkit.Fluid.values().length;
108148
var newTypes = new ArrayList<org.bukkit.Fluid>();

0 commit comments

Comments
 (0)