-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor(serialization): Update itemstack serializer to use NBT API
- Loading branch information
Showing
2 changed files
with
32 additions
and
34 deletions.
There are no files selected for viewing
35 changes: 19 additions & 16 deletions
35
src/main/java/me/uni0305/mokoko/library/serialization/json/JsonItemStackArraySerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,35 @@ | ||
package me.uni0305.mokoko.library.serialization.json; | ||
|
||
import com.google.gson.JsonArray; | ||
import com.google.gson.Gson; | ||
import com.google.gson.JsonElement; | ||
import de.tr7zw.nbtapi.NBT; | ||
import de.tr7zw.nbtapi.iface.ReadWriteNBT; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class JsonItemStackArraySerializer implements JsonSerializer<ItemStack[]> { | ||
private final JsonItemStackSerializer serializer = new JsonItemStackSerializer(); | ||
private final Gson gson = new Gson(); | ||
|
||
@Override | ||
public @NotNull JsonElement serialize(ItemStack @NotNull [] items) { | ||
JsonArray array = new JsonArray(); | ||
for (ItemStack item : items) { | ||
JsonElement element = serializer.serialize(item); | ||
array.add(element); | ||
public @NotNull JsonElement serialize(@Nullable ItemStack @NotNull [] obj) throws RuntimeException { | ||
try { | ||
ReadWriteNBT nbt = NBT.itemStackArrayToNBT(obj); | ||
String json = nbt.toString(); | ||
return gson.toJsonTree(json); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
return array; | ||
} | ||
|
||
@Override | ||
public ItemStack @NotNull [] deserialize(@NotNull JsonElement json) { | ||
JsonArray array = json.getAsJsonArray(); | ||
int length = array.size(); | ||
ItemStack[] items = new ItemStack[length]; | ||
for (int i = 0; i < length; i++) { | ||
JsonElement element = array.get(i); | ||
items[i] = serializer.deserialize(element); | ||
public @Nullable ItemStack @Nullable [] deserialize(@NotNull JsonElement src) throws RuntimeException { | ||
try { | ||
String json = src.toString(); | ||
ReadWriteNBT nbt = NBT.parseNBT(json); | ||
return NBT.itemStackArrayFromNBT(nbt); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
return items; | ||
} | ||
} |
31 changes: 13 additions & 18 deletions
31
src/main/java/me/uni0305/mokoko/library/serialization/json/JsonItemStackSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters