Skip to content

Commit

Permalink
Merge branch 'feature/item-nbt-api'
Browse files Browse the repository at this point in the history
  • Loading branch information
Uni0305 committed Dec 6, 2024
2 parents 2ce3eeb + dafa3fd commit 3c96f75
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 49 deletions.
16 changes: 7 additions & 9 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ plugins {
id("maven-publish")
id("co.uzzu.dotenv.gradle") version "4.0.0"
id("io.freefair.lombok") version "8.10.2"
id("io.papermc.paperweight.userdev") version "1.7.3"
id("xyz.jpenilla.resource-factory-bukkit-convention") version "1.2.0"
id("xyz.jpenilla.run-paper") version "2.3.1"
}
Expand All @@ -14,10 +13,13 @@ version = "0.1.7"

repositories {
mavenCentral()
maven("https://repo.papermc.io/repository/maven-public/")
maven("https://repo.codemc.io/repository/maven-public/")
}

dependencies {
paperweight.paperDevBundle("1.20.4-R0.1-SNAPSHOT")
compileOnly("io.papermc.paper:paper-api:1.20.4-R0.1-SNAPSHOT")
compileOnly("de.tr7zw:item-nbt-api-plugin:2.14.0")
compileOnly("com.google.code.gson:gson:2.11.0")
compileOnly("com.zaxxer:HikariCP:6.0.0")
compileOnly("org.mariadb.jdbc:mariadb-java-client:3.4.1")
Expand All @@ -37,20 +39,17 @@ bukkitPluginYaml {
apiVersion = "1.20"
author = "Uni0305"
description = "A library plugin for Uni0305's plugins."
depend = listOf("NBTAPI")
libraries = listOf(
"com.google.code.gson:gson:2.11.0",
"com.zaxxer:HikariCP:6.0.0",
"org.mariadb.jdbc:mariadb-java-client:3.4.1"
)
}

tasks.assemble {
dependsOn(tasks.reobfJar)
}

tasks.reobfJar {
tasks.jar {
if (env.isPresent("JAR_DIR"))
outputJar = File(env.fetch("JAR_DIR"), "${project.name}-${project.version}.jar")
destinationDirectory.set(file(env.fetch("JAR_DIR")))
}

tasks.runServer {
Expand All @@ -63,7 +62,6 @@ publishing {
publications {
create<MavenPublication>("maven") {
from(components["java"])
artifact(tasks.reobfJar)
}
}
}
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;
}
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,33 @@
package me.uni0305.mokoko.library.serialization.json;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.mojang.serialization.Dynamic;
import com.mojang.serialization.JsonOps;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.nbt.Tag;
import org.bukkit.craftbukkit.v1_20_R3.inventory.CraftItemStack;
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 JsonItemStackSerializer implements JsonSerializer<ItemStack> {
private final Gson gson = new Gson();

@Override
public @NotNull JsonElement serialize(@NotNull ItemStack obj) throws RuntimeException {
try {
net.minecraft.world.item.ItemStack nms = CraftItemStack.asNMSCopy(obj);
CompoundTag nbt = nms.save(new CompoundTag());
Dynamic<Tag> tag = new Dynamic<>(NbtOps.INSTANCE, nbt);
Dynamic<JsonElement> json = tag.convert(JsonOps.INSTANCE);
return json.getValue();
ReadWriteNBT nbt = NBT.itemStackToNBT(obj);
String json = nbt.toString();
return gson.toJsonTree(json);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public @NotNull ItemStack deserialize(@NotNull JsonElement src) throws RuntimeException {
public @Nullable ItemStack deserialize(@NotNull JsonElement src) throws RuntimeException {
try {
Dynamic<JsonElement> json = new Dynamic<>(JsonOps.INSTANCE, src);
Dynamic<Tag> tag = json.convert(NbtOps.INSTANCE);
CompoundTag nbt = (CompoundTag) tag.getValue();
net.minecraft.world.item.ItemStack nms = net.minecraft.world.item.ItemStack.of(nbt);
return CraftItemStack.asBukkitCopy(nms);
String json = src.toString();
ReadWriteNBT nbt = NBT.parseNBT(json);
return NBT.itemStackFromNBT(nbt);
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,24 @@ public class JsonLocationSerializer implements JsonSerializer<Location> {
private final Gson gson = new Gson();

@Override
public @NotNull JsonElement serialize(@NotNull Location obj) {
Map<String, Object> map = obj.serialize();
return gson.toJsonTree(map);
public @NotNull JsonElement serialize(@NotNull Location obj) throws RuntimeException {
try {
Map<String, Object> map = obj.serialize();
return gson.toJsonTree(map);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public @NotNull Location deserialize(@NotNull JsonElement src) throws IllegalArgumentException {
TypeToken<Map<String, Object>> type = new TypeToken<>() {};
Map<String, Object> map = gson.fromJson(src, type);
return Location.deserialize(map);
public @NotNull Location deserialize(@NotNull JsonElement src) throws RuntimeException {
try {
TypeToken<Map<String, Object>> type = new TypeToken<>() {
};
Map<String, Object> map = gson.fromJson(src, type);
return Location.deserialize(map);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

0 comments on commit 3c96f75

Please sign in to comment.