mirror of
https://github.com/songoda/SongodaCore.git
synced 2024-11-28 21:15:45 +01:00
feat!: Remove ItemUtils#applyRandomEnchants
method
Use `NmsItem#copyAndApplyRandomEnchantment` instead
This commit is contained in:
parent
488490de18
commit
704059d817
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user