Skip to content

Make the temporary getRegistryClassInfo public and outside the BukkitClasses #8041

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: dev/patch
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/main/java/ch/njol/skript/bukkitutil/BukkitUtils.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 <R extends Keyed> @Nullable RegistryClassInfo<?> getRegistryClassInfo(
String classPath,
String registryName,
String codeName,
String languageNode
) {
if (!Skript.classExists(classPath))
return null;
Registry<R> registry = null;
if (BukkitUtils.registryExists(registryName)) {
try {
//noinspection unchecked
registry = (Registry<R>) 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<R> registryClass;
try {
//noinspection unchecked
registryClass = (Class<R>) 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;
}

}
52 changes: 4 additions & 48 deletions src/main/java/ch/njol/skript/classes/data/BukkitClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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 <R extends Keyed> @Nullable RegistryClassInfo<?> getRegistryClassInfo(
String classPath,
String registryName,
String codeName,
String languageNode
) {
if (!Skript.classExists(classPath))
return null;
Registry<R> registry = null;
if (BukkitUtils.registryExists(registryName)) {
try {
//noinspection unchecked
registry = (Registry<R>) 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<R> registryClass;
try {
//noinspection unchecked
registryClass = (Class<R>) 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;
}

}