|
2 | 2 |
|
3 | 3 | import java.io.ByteArrayInputStream; |
4 | 4 | import java.io.ByteArrayOutputStream; |
| 5 | +import java.lang.reflect.InvocationTargetException; |
5 | 6 | import java.lang.reflect.Method; |
6 | 7 | import java.util.ArrayList; |
7 | 8 | import java.util.List; |
8 | 9 | import java.util.Optional; |
9 | 10 | import java.util.UUID; |
| 11 | +import java.util.function.BiConsumer; |
10 | 12 |
|
11 | 13 | import org.bukkit.Material; |
12 | 14 | import org.bukkit.craftbukkit.v1_21_R1.CraftRegistry; |
|
25 | 27 | import net.minecraft.nbt.NbtAccounter; |
26 | 28 | import net.minecraft.nbt.NbtIo; |
27 | 29 | import net.minecraft.nbt.Tag; |
| 30 | +import net.minecraft.world.item.component.ResolvableProfile; |
28 | 31 |
|
29 | 32 | public class ZipNmsManager implements NmsManager { |
30 | 33 |
|
31 | | - private static final Class<?> CRAFTMETASKULL_CLASS = ReflectionUtil.getCraftBukkitClass("inventory.CraftMetaSkull"); |
32 | | - private static final Method CRAFTMETASKULL_SET_PROFILE = ReflectionUtil.getMethod(CRAFTMETASKULL_CLASS, |
33 | | - "setProfile", GameProfile.class); |
| 34 | + private static final BiConsumer<SkullMeta, GameProfile> SET_PROFILE; |
34 | 35 |
|
35 | 36 | private static final RegistryAccess DEFAULT_REGISTRY = CraftRegistry.getMinecraftRegistry(); |
36 | 37 |
|
37 | 38 | private static final CompoundTag NBT_EMPTY_ITEMSTACK = new CompoundTag(); |
38 | 39 |
|
39 | 40 | static { |
40 | 41 | NBT_EMPTY_ITEMSTACK.putString("id", "minecraft:air"); |
| 42 | + |
| 43 | + BiConsumer<SkullMeta, GameProfile> setProfile = (meta, profile) -> { |
| 44 | + throw new NullPointerException("Unable to find 'setProfile' method!"); |
| 45 | + }; |
| 46 | + |
| 47 | + Class<?> craftMetaSkullClass = new ItemStack(Material.PLAYER_HEAD) |
| 48 | + .getItemMeta() |
| 49 | + .getClass(); |
| 50 | + |
| 51 | + Method setResolvableProfileMethod = ReflectionUtil.searchMethod(craftMetaSkullClass, void.class, ResolvableProfile.class); |
| 52 | + if (setResolvableProfileMethod != null) { |
| 53 | + setProfile = (meta, profile) -> { |
| 54 | + try { |
| 55 | + setResolvableProfileMethod.invoke(meta, new ResolvableProfile(profile)); |
| 56 | + } catch (IllegalAccessException | InvocationTargetException e) { |
| 57 | + e.printStackTrace(); |
| 58 | + } |
| 59 | + }; |
| 60 | + } else { |
| 61 | + Method setProfileMethod = ReflectionUtil.searchMethod(craftMetaSkullClass, void.class, GameProfile.class); |
| 62 | + if (setProfileMethod != null) { |
| 63 | + setProfile = (meta, profile) -> { |
| 64 | + try { |
| 65 | + setProfileMethod.invoke(meta, profile); |
| 66 | + } catch (IllegalAccessException | InvocationTargetException e) { |
| 67 | + e.printStackTrace(); |
| 68 | + } |
| 69 | + }; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + SET_PROFILE = setProfile; |
41 | 74 | } |
42 | 75 |
|
43 | 76 | public byte[] nbtToBinary(CompoundTag compound) { |
@@ -103,9 +136,10 @@ public void setSkullProfile(SkullMeta meta, String texture) { |
103 | 136 | try { |
104 | 137 | GameProfile gameProfile = new GameProfile(UUID.randomUUID(), ""); |
105 | 138 | gameProfile.getProperties().put("textures", new Property("textures", texture)); |
106 | | - CRAFTMETASKULL_SET_PROFILE.invoke(meta, gameProfile); |
| 139 | + |
| 140 | + SET_PROFILE.accept(meta, gameProfile); |
107 | 141 | } catch (Exception e) { |
108 | | - throw new ClassCastException("Error by setting skull profile"); |
| 142 | + e.printStackTrace(); |
109 | 143 | } |
110 | 144 | } |
111 | 145 |
|
|
0 commit comments