feat!: Remove ItemUtils#applyRandomEnchants method

Use `NmsItem#copyAndApplyRandomEnchantment` instead
This commit is contained in:
Christian Koop 2024-09-06 10:51:04 +02:00
parent 488490de18
commit 704059d817
No known key found for this signature in database
GPG Key ID: 6A4A09E8ED946113

View File

@ -37,9 +37,7 @@ import java.util.Base64;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;
@ -89,56 +87,6 @@ public class ItemUtils {
return titleCase.toString().trim();
}
private static Method methodAsBukkitCopy, methodAsNMSCopy, methodA;
private static Object randomInstance;
static {
try {
Class<?> clazzEnchantmentManager = ClassMapping.ENCHANTMENT_MANAGER.getClazz();
Class<?> clazzItemStack = ClassMapping.ITEM_STACK.getClazz();
Class<?> clazzCraftItemStack = ClassMapping.CRAFT_ITEM_STACK.getClazz();
methodAsBukkitCopy = clazzCraftItemStack.getMethod("asBukkitCopy", clazzItemStack);
methodAsNMSCopy = MethodMapping.CB_ITEM_STACK__AS_NMS_COPY.getMethod(clazzCraftItemStack);
if (ServerVersion.isServerVersionAbove(ServerVersion.V1_20)) {
//Do nothing
} else if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_19)) {
Class<?> clazzRandomSource = ClassMapping.RANDOM_SOURCE.getClazz();
methodA = clazzEnchantmentManager.getMethod("a", clazzRandomSource.getMethod("c").getReturnType(), clazzItemStack, int.class, boolean.class);
randomInstance = ClassMapping.SINGLE_THREADED_RANDOM_SOURCE.getClazz().getConstructor(long.class).newInstance(ThreadLocalRandom.current().nextLong());
} else if (ServerVersion.isServerVersion(ServerVersion.V1_8)) {
methodA = clazzEnchantmentManager.getMethod("a", Random.class, clazzItemStack, int.class);
randomInstance = new Random();
} else {
methodA = clazzEnchantmentManager.getMethod("a", Random.class, clazzItemStack, int.class, boolean.class);
randomInstance = new Random();
}
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException ex) {
ex.printStackTrace();
}
}
public static ItemStack applyRandomEnchants(ItemStack item, int level) {
try {
Object nmsItemStack = methodAsNMSCopy.invoke(null, item);
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_19)) {
nmsItemStack = methodA.invoke(null, randomInstance, nmsItemStack, level, false);
} else if (ServerVersion.isServerVersion(ServerVersion.V1_8)) {
nmsItemStack = methodA.invoke(null, randomInstance, nmsItemStack, level);
} else {
nmsItemStack = methodA.invoke(null, randomInstance, nmsItemStack, level, false);
}
item = (ItemStack) methodAsBukkitCopy.invoke(null, nmsItemStack);
} catch (IllegalAccessException | InvocationTargetException ex) {
ex.printStackTrace();
}
return item;
}
public static String itemStackArrayToBase64(ItemStack[] items) {
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();