9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-30 12:29:15 +00:00

支持snbt解析

This commit is contained in:
XiaoMoMi
2025-06-24 03:44:54 +08:00
parent 7d440c32a3
commit 9e0bb40dc0
7 changed files with 60 additions and 31 deletions

View File

@@ -1,11 +1,14 @@
package net.momirealms.craftengine.bukkit.plugin;
import com.google.gson.JsonElement;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MRegistryOps;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.plugin.Platform;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.sparrow.nbt.CompoundTag;
import net.momirealms.sparrow.nbt.Tag;
import org.bukkit.Bukkit;
import java.util.Map;
@@ -19,14 +22,36 @@ public class BukkitPlatform implements Platform {
@SuppressWarnings("unchecked")
@Override
public Object nbt2Java(String nbt) {
public Object snbtToJava(String nbt) {
try {
Object tag = FastNMS.INSTANCE.method$TagParser$parseCompoundFully("{\"root\":" + nbt + "}");
Map<String, Object> map = (Map<String, Object>) MRegistryOps.NBT.convertTo(MRegistryOps.JAVA, tag);
return map.get("root");
} catch (CommandSyntaxException e) {
CraftEngine.instance().debug(e::getMessage);
throw new LocalizedResourceConfigException("warning.config.template.argument.default_value.invalid_syntax", e, nbt);
throw new LocalizedResourceConfigException("warning.config.type.snbt.invalid_syntax", e, nbt);
}
}
@Override
public Tag jsonToSparrowNBT(JsonElement json) {
return MRegistryOps.JSON.convertTo(MRegistryOps.SPARROW_NBT, json);
}
@Override
public Tag snbtToSparrowNBT(String nbt) {
try {
Object tag = FastNMS.INSTANCE.method$TagParser$parseCompoundFully("{\"root\":" + nbt + "}");
CompoundTag map = (CompoundTag) MRegistryOps.NBT.convertTo(MRegistryOps.SPARROW_NBT, tag);
return map.get("root");
} catch (CommandSyntaxException e) {
CraftEngine.instance().debug(e::getMessage);
throw new LocalizedResourceConfigException("warning.config.type.snbt.invalid_syntax", e, nbt);
}
}
@Override
public Tag javaToSparrowNBT(Object object) {
return MRegistryOps.JAVA.convertTo(MRegistryOps.SPARROW_NBT, object);
}
}

View File

@@ -42,7 +42,7 @@ public final class MRegistryOps {
// 1.20.1-1.20.4
JAVA = (DynamicOps<Object>) CoreReflections.method$RegistryOps$create.invoke(null, LegacyJavaOps.INSTANCE, FastNMS.INSTANCE.registryAccess());
} else {
JAVA = null;
throw new ReflectionInitException("Could not find JavaOps");
}
NBT = (DynamicOps<Object>) CoreReflections.method$RegistryOps$create.invoke(null, ReflectionUtils.getDeclaredField(clazz$NbtOps, clazz$NbtOps, 0).get(null), FastNMS.INSTANCE.registryAccess());
JSON = (DynamicOps<JsonElement>) CoreReflections.method$RegistryOps$create.invoke(null, JsonOps.INSTANCE, FastNMS.INSTANCE.registryAccess());