diff --git a/src/main/java/ch/njol/skript/bukkitutil/BukkitUtils.java b/src/main/java/ch/njol/skript/bukkitutil/BukkitUtils.java index bcc1659fc2d..9aa23bbb42e 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/BukkitUtils.java +++ b/src/main/java/ch/njol/skript/bukkitutil/BukkitUtils.java @@ -1,8 +1,13 @@ package ch.njol.skript.bukkitutil; import ch.njol.skript.Skript; +import ch.njol.skript.classes.registry.RegistryClassInfo; +import ch.njol.skript.util.PaperUtils; + import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; + +import org.bukkit.Keyed; import org.bukkit.Registry; import org.bukkit.inventory.EquipmentSlot; import org.bukkit.potion.PotionEffectType; @@ -67,4 +72,47 @@ public static EquipmentSlot getEquipmentSlotFromIndex(int slotIndex) { return BUKKIT_EQUIPMENT_SLOT_INDICES.inverse().get(slotIndex); } + /** + * Gets a {@link RegistryClassInfo} by checking if the {@link Class} from {@code classPath} exists + * and {@link Registry} or {@link io.papermc.paper.registry.RegistryKey} contains {@code registryName}. + * @param classPath The {@link String} representation of the desired {@link Class}. + * @param registryName The {@link String} representation of the desired {@link Registry}. + * @param codeName The name used in patterns. + * @param languageNode The language node of the type. + * @return {@link RegistryClassInfo} if the class and registry exists, otherwise {@code null}. + */ + public static @Nullable RegistryClassInfo getRegistryClassInfo( + String classPath, + String registryName, + String codeName, + String languageNode + ) { + if (!Skript.classExists(classPath)) + return null; + Registry registry = null; + if (BukkitUtils.registryExists(registryName)) { + try { + //noinspection unchecked + registry = (Registry) Registry.class.getField(registryName).get(null); + } catch (NoSuchFieldException | IllegalAccessException e) { + throw new RuntimeException(e); + } + } else if (PaperUtils.registryExists(registryName)) { + registry = PaperUtils.getBukkitRegistry(registryName); + } + if (registry != null) { + Class registryClass; + try { + //noinspection unchecked + registryClass = (Class) Class.forName(classPath); + } catch (ClassNotFoundException e) { + Skript.debug("Could not retrieve the class with the path: '" + classPath + "'."); + throw new RuntimeException(e); + } + return new RegistryClassInfo<>(registryClass, registry, codeName, languageNode); + } + Skript.debug("There were no registries found for '" + registryName + "'."); + return null; + } + } diff --git a/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java b/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java index d860e5dc169..d7c7f0ccc6f 100644 --- a/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java +++ b/src/main/java/ch/njol/skript/classes/data/BukkitClasses.java @@ -1418,7 +1418,7 @@ public String toVariableNameString(EnchantmentOffer eo) { .description("Represents the cause of the action of a potion effect on an entity, e.g. arrow, command") .since("2.10")); - ClassInfo wolfVariantClassInfo = getRegistryClassInfo( + ClassInfo wolfVariantClassInfo = BukkitUtils.getRegistryClassInfo( "org.bukkit.entity.Wolf$Variant", "WOLF_VARIANT", "wolfvariant", @@ -1566,7 +1566,7 @@ public String toVariableNameString(WorldBorder border) { .since("2.11") ); - ClassInfo pigVariantClassInfo = getRegistryClassInfo( + ClassInfo pigVariantClassInfo = BukkitUtils.getRegistryClassInfo( "org.bukkit.entity.Pig$Variant", "PIG_VARIANT", "pigvariant", @@ -1585,7 +1585,7 @@ public String toVariableNameString(WorldBorder border) { .requiredPlugins("Minecraft 1.21.5+") .documentationId("PigVariant")); - ClassInfo chickenVariantClassInfo = getRegistryClassInfo( + ClassInfo chickenVariantClassInfo = BukkitUtils.getRegistryClassInfo( "org.bukkit.entity.Chicken$Variant", "CHICKEN_VARIANT", "chickenvariant", @@ -1605,7 +1605,7 @@ public String toVariableNameString(WorldBorder border) { .documentationId("ChickenVariant") ); - ClassInfo cowVariantClassInfo = getRegistryClassInfo( + ClassInfo cowVariantClassInfo = BukkitUtils.getRegistryClassInfo( "org.bukkit.entity.Cow$Variant", "COW_VARIANT", "cowvariant", @@ -1633,48 +1633,4 @@ public String toVariableNameString(WorldBorder border) { ); } - - /** - * Gets a {@link RegistryClassInfo} by checking if the {@link Class} from {@code classPath} exists - * and {@link Registry} or {@link io.papermc.paper.registry.RegistryKey} contains {@code registryName}. - * @param classPath The {@link String} representation of the desired {@link Class}. - * @param registryName The {@link String} representation of the desired {@link Registry}. - * @param codeName The name used in patterns. - * @param languageNode The language node of the type. - * @return {@link RegistryClassInfo} if the class and registry exists, otherwise {@code null}. - */ - private static @Nullable RegistryClassInfo getRegistryClassInfo( - String classPath, - String registryName, - String codeName, - String languageNode - ) { - if (!Skript.classExists(classPath)) - return null; - Registry registry = null; - if (BukkitUtils.registryExists(registryName)) { - try { - //noinspection unchecked - registry = (Registry) Registry.class.getField(registryName).get(null); - } catch (NoSuchFieldException | IllegalAccessException e) { - throw new RuntimeException(e); - } - } else if (PaperUtils.registryExists(registryName)) { - registry = PaperUtils.getBukkitRegistry(registryName); - } - if (registry != null) { - Class registryClass; - try { - //noinspection unchecked - registryClass = (Class) Class.forName(classPath); - } catch (ClassNotFoundException e) { - Skript.debug("Could not retrieve the class with the path: '" + classPath + "'."); - throw new RuntimeException(e); - } - return new RegistryClassInfo<>(registryClass, registry, codeName, languageNode); - } - Skript.debug("There were no registries found for '" + registryName + "'."); - return null; - } - }