1+ package net .imprex .zip .nms .v1_20_R2 ;
2+
3+ import java .io .ByteArrayInputStream ;
4+ import java .io .ByteArrayOutputStream ;
5+ import java .lang .reflect .Method ;
6+ import java .util .ArrayList ;
7+ import java .util .List ;
8+ import java .util .UUID ;
9+
10+ import org .bukkit .Material ;
11+ import org .bukkit .craftbukkit .v1_20_R2 .inventory .CraftItemStack ;
12+ import org .bukkit .inventory .ItemStack ;
13+ import org .bukkit .inventory .meta .SkullMeta ;
14+
15+ import com .mojang .authlib .GameProfile ;
16+ import com .mojang .authlib .properties .Property ;
17+
18+ import net .imprex .zip .common .ReflectionUtil ;
19+ import net .imprex .zip .nms .api .NmsManager ;
20+ import net .minecraft .nbt .CompoundTag ;
21+ import net .minecraft .nbt .ListTag ;
22+ import net .minecraft .nbt .NbtIo ;
23+ import net .minecraft .nbt .Tag ;
24+
25+ public class ZipNmsManager implements NmsManager {
26+
27+ private static final Class <?> CRAFTMETASKULL_CLASS = ReflectionUtil .getCraftBukkitClass ("inventory.CraftMetaSkull" );
28+ private static final Method CRAFTMETASKULL_SET_PROFILE = ReflectionUtil .getMethod (CRAFTMETASKULL_CLASS ,
29+ "setProfile" , GameProfile .class );
30+
31+ public byte [] nbtToBinary (CompoundTag compound ) {
32+ try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream ()) {
33+ NbtIo .writeCompressed (compound , outputStream );
34+ return outputStream .toByteArray ();
35+ } catch (Exception e ) {
36+ e .printStackTrace ();
37+ }
38+ return null ;
39+ }
40+
41+ public CompoundTag binaryToNBT (byte [] binary ) {
42+ try (ByteArrayInputStream inputStream = new ByteArrayInputStream (binary )) {
43+ return NbtIo .readCompressed (inputStream );
44+ } catch (Exception e ) {
45+ e .printStackTrace ();
46+ }
47+ return new CompoundTag ();
48+ }
49+
50+ @ Override
51+ public byte [] itemstackToBinary (ItemStack [] items ) {
52+ CompoundTag inventory = new CompoundTag ();
53+ ListTag list = new ListTag ();
54+ for (ItemStack itemStack : items ) {
55+ net .minecraft .world .item .ItemStack craftItem = CraftItemStack .asNMSCopy (itemStack );
56+ list .add (craftItem .save (new CompoundTag ()));
57+ }
58+ inventory .put ("i" , list );
59+ return nbtToBinary (inventory );
60+ }
61+
62+ @ Override
63+ public List <ItemStack > binaryToItemStack (byte [] binary ) {
64+ CompoundTag nbt = binaryToNBT (binary );
65+ List <ItemStack > items = new ArrayList <>();
66+ if (nbt .contains ("i" , 9 )) {
67+ ListTag list = nbt .getList ("i" , 10 );
68+ for (Tag base : list ) {
69+ if (base instanceof CompoundTag ) {
70+ items .add (CraftItemStack .asBukkitCopy (net .minecraft .world .item .ItemStack .of ((CompoundTag ) base )));
71+ }
72+ }
73+ }
74+ return items ;
75+ }
76+
77+ @ Override
78+ public void setSkullProfile (SkullMeta meta , String texture ) {
79+ try {
80+ GameProfile gameProfile = new GameProfile (UUID .randomUUID (), "" );
81+ gameProfile .getProperties ().put ("textures" , new Property ("textures" , texture ));
82+ CRAFTMETASKULL_SET_PROFILE .invoke (meta , gameProfile );
83+ } catch (Exception e ) {
84+ throw new ClassCastException ("Error by setting skull profile" );
85+ }
86+ }
87+
88+ @ Override
89+ public boolean isAir (Material material ) {
90+ return material == null || material == Material .AIR ;
91+ }
92+ }
0 commit comments