Skip to content

Commit

Permalink
Add data component support for 1.21.4
Browse files Browse the repository at this point in the history
  • Loading branch information
virustotalop committed Feb 3, 2025
1 parent 420b785 commit 2437cbb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public CitizensRegistry() {
this.getIdMethod = ReflectionUtil.getMethod(this.npcClass, "getId");
this.getNPCMethod = ReflectionUtil.getMethodByParams(
this.npcRegistry.getClass(),
"getNPC", Entity.class
new String[]{"getNPC"},
Entity.class
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public String replace(PlayerWrapper<?> playerWrapper, String text) {

private Method getPlaceholdersMethod() {
Class<?> clazz = ReflectionUtil.classForName("me.clip.placeholderapi.PlaceholderAPI");
return ReflectionUtil.getMethodByParams(clazz, "setPlaceholders", OfflinePlayer.class, String.class);
return ReflectionUtil.getMethodByParams(
clazz,
new String[]{"setPlaceholders"},
OfflinePlayer.class,
String.class
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.*;

public final class BukkitDataComponentUtil {

Expand Down Expand Up @@ -72,6 +69,8 @@ public final class BukkitDataComponentUtil {
.getClassIfExists("net.minecraft.network.chat.Component");
private static final Class<?> TAG = ReflectionUtil
.getClassIfExists("net.minecraft.nbt.Tag");
private static final Class<?> REFERENCE = ReflectionUtil
.getClassIfExists("net.minecraft.core.Holder$Reference");

private static final Field NBT_OPS_INSTANCE =
ReflectionUtil.getDeclaredField(NBT_OPS, "INSTANCE");
Expand All @@ -89,7 +88,7 @@ public final class BukkitDataComponentUtil {
private static final Method RESOURCE_LOCATION_READ = ReflectionUtil
.getStaticMethod(RESOURCE_LOCATION, RESOURCE_LOCATION, STRING_READER);
private static final Method MAPPED_REGISTRY_GET = ReflectionUtil
.getMethodByParams(MAPPED_REGISTRY, "get", RESOURCE_LOCATION);
.getMethodByParams(MAPPED_REGISTRY, new String[]{"getValue", "get"}, RESOURCE_LOCATION);
private static final Method CODEC_OR_THROW = ReflectionUtil
.getMethod(DATA_COMPONENT_TYPE, "codecOrThrow");
private static final Method PARSE = ReflectionUtil
Expand All @@ -116,6 +115,8 @@ public final class BukkitDataComponentUtil {
.getMethod(COMPONENT, 0, "getString");
private static final Method TAG_PARSER_READ_VALUE = ReflectionUtil
.getMethod(TAG_PARSER, "readValue");
private static final Method HOLDER_VALUE = ReflectionUtil
.getMethod(REFERENCE, "value");

private static final Constructor<?> STRING_READER_CON =
ReflectionUtil.getConstructor(STRING_READER, String.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,17 @@ public static Method getMethod(@Nullable Class<?> cl, int paramCount, String...
return null;
}

public static Method getMethodByParams(Class<?> cl, String methodName, Class<?>... params) {
public static Method getMethodByParams(Class<?> cl, String[] methodNames, Class<?>... params) {
if (cl == null) {
return null;
}
try {
Method method = cl.getDeclaredMethod(methodName, params);
method.setAccessible(true);
return method;
} catch (NoSuchMethodException | SecurityException e) {
e.printStackTrace();
for (String methodName : methodNames) {
try {
Method method = cl.getDeclaredMethod(methodName, params);
method.setAccessible(true);
return method;
} catch (NoSuchMethodException | SecurityException e) {
}
}
return null;
}
Expand Down

0 comments on commit 2437cbb

Please sign in to comment.