diff --git a/build.gradle b/build.gradle index b9b2683..7544230 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,4 @@ subprojects { - apply plugin: 'maven' apply plugin: 'java' group 'com.github.chain-plugins' diff --git a/bukkit/build.gradle b/bukkit/build.gradle index 9729dd9..d1cff09 100644 --- a/bukkit/build.gradle +++ b/bukkit/build.gradle @@ -1,3 +1,7 @@ +plugins { + id 'maven' +} + repositories { maven { url = 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/' } maven { url = 'https://repo.codemc.org/repository/maven-public/' } diff --git a/bukkit/src/main/java/com/github/chain/inventory/Serializer.java b/bukkit/src/main/java/com/github/chain/inventory/Serializer.java index 43bbd06..9414ad2 100644 --- a/bukkit/src/main/java/com/github/chain/inventory/Serializer.java +++ b/bukkit/src/main/java/com/github/chain/inventory/Serializer.java @@ -8,7 +8,6 @@ import com.github.chain.inventory.nms.v1_8_R3.v1_8_R3NMS; import lombok.NonNull; import org.bukkit.Bukkit; -import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import java.lang.reflect.Array; @@ -19,29 +18,41 @@ public class Serializer { private static InventoryNMS nmsBridge; private static MinecraftVersion version; - public static String encode(@NonNull Inventory inventory) throws Exception { + public static String encode(@NonNull ItemStack[] items) throws Exception { checkIfProviderIsSet(); - ItemStack[] inventoryContents = inventory.getContents(); Class nmsItemStackClazz = getItemStackClass(); - - Object nmsItemArray = Array.newInstance(nmsItemStackClazz, inventoryContents.length); - for (int i = 0; i < inventoryContents.length; i++) { - Array.set(nmsItemArray, i, toNMSItem(inventoryContents[i])); + Object nmsItemArray = Array.newInstance(nmsItemStackClazz, items.length); + for (int i = 0; i < items.length; i++) { + Array.set(nmsItemArray, i, toNMSItem(items[i])); } return nmsBridge.encode((Object[]) nmsItemArray); } - public static void decode(@NonNull String encoded, @NonNull Inventory inventory) throws Exception { + public static ItemStack[] decode(@NonNull String encoded) throws Exception { checkIfProviderIsSet(); Object[] nmsItemStacks = nmsBridge.decode(encoded); - - ItemStack[] inventoryContents = new ItemStack[nmsItemStacks.length]; + ItemStack[] items = new ItemStack[nmsItemStacks.length]; for (int i = 0; i < nmsItemStacks.length; i++) { - inventoryContents[i] = toBukkitItem(nmsItemStacks[i]); + items[i] = toBukkitItem(nmsItemStacks[i]); } - inventory.setContents(inventoryContents); + return items; + } + + // TODO: refactor + public static String encode(@NonNull ItemStack item) throws Exception { + checkIfProviderIsSet(); + ItemStack[] items = new ItemStack[1]; + items[0] = item; + return encode(items); + } + + // TODO: refactor + public static ItemStack decodeSingular(@NonNull String encoded) throws Exception { + checkIfProviderIsSet(); + Object[] nmsItemStacks = nmsBridge.decode(encoded); + return toBukkitItem(nmsItemStacks[0]); } private static ItemStack toBukkitItem(Object itemStack) throws Exception {