Skip to content

Commit

Permalink
fix possible CraftRegistry ClassNotFoundException
Browse files Browse the repository at this point in the history
  • Loading branch information
vytskalt committed Jul 4, 2024
1 parent 521bc47 commit a2e5df1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import net.kyori.adventure.text.Component;
import net.kyori.adventure.translation.GlobalTranslator;
import net.megavex.scoreboardlibrary.implementation.packetAdapter.modern.util.NativeAdventureUtil;
import net.megavex.scoreboardlibrary.implementation.packetAdapter.modern.util.RegistryUtil;
import net.minecraft.network.chat.Component.Serializer;
import net.minecraft.network.chat.MutableComponent;
import org.bukkit.craftbukkit.CraftRegistry;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -64,7 +64,7 @@ public ComponentProviderImpl(boolean isNativeAdventure) {
JsonElement json = gson().serializeToTree(translated);
Object[] args;
if (PacketAccessors.IS_1_20_5_OR_ABOVE) {
args = new Object[]{json, CraftRegistry.getMinecraftRegistry()};
args = new Object[]{json, RegistryUtil.MINECRAFT_REGISTRY};
} else {
args = new Object[]{json};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.megavex.scoreboardlibrary.implementation.packetAdapter.modern.util;

import net.minecraft.core.RegistryAccess;
import org.bukkit.Bukkit;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public final class RegistryUtil {
public static final RegistryAccess MINECRAFT_REGISTRY;

static {
String cbPackage = Bukkit.getServer().getClass().getPackage().getName();
Class<?> craftRegistry;
try {
craftRegistry = Class.forName(cbPackage + ".CraftRegistry");
} catch (ClassNotFoundException e) {
throw new ExceptionInInitializerError(e);
}

try {
Method method = craftRegistry.getDeclaredMethod("getMinecraftRegistry");
MINECRAFT_REGISTRY = (RegistryAccess) method.invoke(null);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
throw new ExceptionInInitializerError(e);
}
}

private RegistryUtil() {
}
}

0 comments on commit a2e5df1

Please sign in to comment.