Skip to content

Commit

Permalink
Add singular encode and decode
Browse files Browse the repository at this point in the history
  • Loading branch information
dig committed Nov 16, 2020
1 parent 6da5e10 commit 24345e7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
subprojects {
apply plugin: 'maven'
apply plugin: 'java'

group 'com.github.chain-plugins'
Expand Down
4 changes: 4 additions & 0 deletions bukkit/build.gradle
Original file line number Diff line number Diff line change
@@ -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/' }
Expand Down
35 changes: 23 additions & 12 deletions bukkit/src/main/java/com/github/chain/inventory/Serializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down

0 comments on commit 24345e7

Please sign in to comment.