Skip to content

Commit

Permalink
Fixed @hand for all versions lower than 1.20.4
Browse files Browse the repository at this point in the history
  • Loading branch information
FabioZumbi12 committed Aug 6, 2024
1 parent 15ae2cc commit c5062d1
Showing 1 changed file with 76 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ use this class in other plugins, an acknowledgment in the plugin documentation w

import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -497,9 +494,9 @@ private String toJson() {
* @return instance of same {@link UltimateFancy}.
*/
public UltimateFancy next() {
if (workingGroup.size() > 0) {
if (!workingGroup.isEmpty()) {
for (JSONObject obj : workingGroup) {
if (obj.containsKey("text") && obj.get("text").toString().length() > 0) {
if (obj.containsKey("text") && !obj.get("text").toString().isEmpty()) {
for (ExtraElement element : pendentElements) {
obj.put(element.getAction(), element.getJson());
}
Expand Down Expand Up @@ -564,8 +561,8 @@ public UltimateFancy hoverShowItem(ItemStack item) {
JSONObject jItem = parseHoverItem(item);
if (Utf8.encodedLength(jItem.toJSONString()) > 32767)
pendentElements.add(new ExtraElement("hoverEvent", parseHoverItem(new ItemStack(item.getType()))));

pendentElements.add(new ExtraElement("hoverEvent", jItem));
else
pendentElements.add(new ExtraElement("hoverEvent", jItem));
return this;
}

Expand Down Expand Up @@ -656,18 +653,36 @@ private JSONObject parseHoverItem(ItemStack item) {
String jItem = convertItemStackToJson(item);
if (Utf8.encodedLength(jItem) > 32767)
obj.put("value", convertItemStackToJson(new ItemStack(item.getType())));

obj.put("value", jItem);
else
obj.put("value", jItem);
return obj;
}

private String convertItemStackToJson(ItemStack itemStack) {
Class<?> craftItemStackClazz = ReflectionUtil.getOBCClass("inventory.CraftItemStack");
Method asNMSCopyMethod = ReflectionUtil.getMethod(craftItemStackClazz, "asNMSCopy", ItemStack.class);
Method asNMSCopyMethod;
try {
asNMSCopyMethod = ReflectionUtil.getMethod(craftItemStackClazz, "asNMSCopy", ItemStack.class);
} catch (Exception e) {
e.printStackTrace();
return null;
}

Class<?> nmsItemStackClazz = ReflectionUtil.getNMSClassItem("ItemStack");
Class<?> nbtTagCompoundClazz = ReflectionUtil.getNMSClassNbt("NBTTagCompound");

Method saveNmsItemStackMethod;

Class<?> nmsItemStackClazz = ReflectionUtil.getNMSClass("ItemStack");
Class<?> nbtTagCompoundClazz = ReflectionUtil.getNMSClass("NBTTagCompound");
Method saveNmsItemStackMethod = ReflectionUtil.getMethod(nmsItemStackClazz, "save", nbtTagCompoundClazz);
try{
saveNmsItemStackMethod = ReflectionUtil.getMethod(nmsItemStackClazz, "save", nbtTagCompoundClazz); // <= 1.17
} catch (Exception t1){
try{
saveNmsItemStackMethod = ReflectionUtil.getMethod(nmsItemStackClazz, "b", nbtTagCompoundClazz); // <= 1.20.4
} catch (Exception t2){
t2.printStackTrace();
return null;
}
}

Object nmsNbtTagCompoundObj;
Object nmsItemStackObj;
Expand All @@ -678,6 +693,7 @@ private String convertItemStackToJson(ItemStack itemStack) {
nmsItemStackObj = asNMSCopyMethod.invoke(null, itemStack);
itemAsJsonObject = saveNmsItemStackMethod.invoke(nmsItemStackObj, nmsNbtTagCompoundObj);
} catch (Throwable t) {
t.printStackTrace();
return null;
}
return itemAsJsonObject.toString();
Expand Down Expand Up @@ -799,10 +815,13 @@ public static String getVersion() {
versionString = name.substring(name.lastIndexOf('.') + 1) + ".";
}

if (versionString.equals("craftbukkit."))
versionString = "";

return versionString;
}

public static Class<?> getNMSClass(String nmsClassName) {
public static Class<?> getNMSClassNbt(String nmsClassName) {
if (loadedNMSClasses.containsKey(nmsClassName)) {
return loadedNMSClasses.get(nmsClassName);
}
Expand All @@ -812,9 +831,43 @@ public static Class<?> getNMSClass(String nmsClassName) {

try {
clazz = Class.forName(clazzName);
} catch (Throwable t) {
t.printStackTrace();
return loadedNMSClasses.put(nmsClassName, null);
} catch (Throwable t1) {
try {
clazzName = "net.minecraft.nbt." + nmsClassName;
clazz = Class.forName(clazzName);
} catch (Throwable t2) {
try {
clazzName = "net.minecraft.core." + nmsClassName;
clazz = Class.forName(clazzName);
} catch (Throwable t3) {
t3.printStackTrace();
return null;
}
}
}

loadedNMSClasses.put(nmsClassName, clazz);
return clazz;
}

public static Class<?> getNMSClassItem(String nmsClassName) {
if (loadedNMSClasses.containsKey(nmsClassName)) {
return loadedNMSClasses.get(nmsClassName);
}

String clazzName = "net.minecraft.server." + getVersion() + nmsClassName;
Class<?> clazz;

try {
clazz = Class.forName(clazzName);
} catch (Throwable t1) {
try {
clazzName = "net.minecraft.world.item." + nmsClassName;
clazz = Class.forName(clazzName);
} catch (Throwable t2) {
t2.printStackTrace();
return null;
}
}

loadedNMSClasses.put(nmsClassName, clazz);
Expand All @@ -833,15 +886,14 @@ public static Class<?> getOBCClass(String obcClassName) {
clazz = Class.forName(clazzName);
} catch (Throwable t) {
t.printStackTrace();
loadedOBCClasses.put(obcClassName, null);
return null;
}

loadedOBCClasses.put(obcClassName, clazz);
return clazz;
}

public static Method getMethod(Class<?> clazz, String methodName, Class<?>... params) {
public static Method getMethod(Class<?> clazz, String methodName, Class<?>... params) throws Exception {
if (!loadedMethods.containsKey(clazz)) {
loadedMethods.put(clazz, new HashMap<>());
}
Expand All @@ -852,16 +904,9 @@ public static Method getMethod(Class<?> clazz, String methodName, Class<?>... pa
return methods.get(methodName);
}

try {
Method method = clazz.getMethod(methodName, params);
methods.put(methodName, method);
loadedMethods.put(clazz, methods);
return method;
} catch (Exception e) {
e.printStackTrace();
methods.put(methodName, null);
loadedMethods.put(clazz, methods);
return null;
}
Method method = clazz.getMethod(methodName, params);
methods.put(methodName, method);
loadedMethods.put(clazz, methods);
return method;
}
}

0 comments on commit c5062d1

Please sign in to comment.