提取TagDataParser
This commit is contained in:
parent
7697968b29
commit
bb797c018a
2 changed files with 88 additions and 46 deletions
|
@ -2,9 +2,12 @@ package com.atsuishio.superbwarfare.data.launchable;
|
|||
|
||||
import com.atsuishio.superbwarfare.data.CustomData;
|
||||
import com.atsuishio.superbwarfare.data.gun.ProjectileInfo;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.atsuishio.superbwarfare.tools.TagDataParser;
|
||||
import com.google.gson.JsonObject;
|
||||
import net.minecraft.nbt.*;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.DoubleTag;
|
||||
import net.minecraft.nbt.NbtUtils;
|
||||
import net.minecraft.nbt.StringTag;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Locale;
|
||||
|
@ -23,48 +26,7 @@ public class LaunchableEntityTool {
|
|||
return null;
|
||||
}
|
||||
|
||||
var tag = new CompoundTag();
|
||||
|
||||
for (var d : launchableData.entrySet()) {
|
||||
var parsed = parseData(d.getValue(), data);
|
||||
if (parsed == null) continue;
|
||||
tag.put(d.getKey(), parsed);
|
||||
}
|
||||
|
||||
return tag;
|
||||
}
|
||||
|
||||
private static @Nullable Tag parseData(JsonElement object, ShootData data) {
|
||||
if (object.isJsonObject()) {
|
||||
var tag = new CompoundTag();
|
||||
for (var d : object.getAsJsonObject().entrySet()) {
|
||||
var parsed = parseData(d.getValue(), data);
|
||||
if (parsed == null) continue;
|
||||
tag.put(d.getKey(), parsed);
|
||||
}
|
||||
return tag;
|
||||
} else if (object.isJsonArray()) {
|
||||
var tag = new ListTag();
|
||||
for (var d : object.getAsJsonArray()) {
|
||||
tag.add(parseData(d, data));
|
||||
}
|
||||
return tag;
|
||||
} else if (object.isJsonPrimitive()) {
|
||||
var prime = object.getAsJsonPrimitive();
|
||||
if (prime.isString()) {
|
||||
return modifyStringTag(prime.getAsString(), data);
|
||||
} else if (prime.isNumber()) {
|
||||
return DoubleTag.valueOf(prime.getAsLong());
|
||||
} else if (prime.isBoolean()) {
|
||||
return ByteTag.valueOf(prime.getAsBoolean());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Tag modifyStringTag(String value, ShootData data) {
|
||||
return switch (value) {
|
||||
return TagDataParser.parse(launchableData, name -> switch (name) {
|
||||
case "@sbw:damage" -> DoubleTag.valueOf(data.damage());
|
||||
case "@sbw:owner" -> NbtUtils.createUUID(data.shooter());
|
||||
case "@sbw:owner_string_lower" ->
|
||||
|
@ -74,7 +36,7 @@ public class LaunchableEntityTool {
|
|||
case "@sbw:explosion_damage" -> DoubleTag.valueOf(data.explosionDamage());
|
||||
case "@sbw:explosion_radius" -> DoubleTag.valueOf(data.explosionRadius());
|
||||
case "@sbw:spread" -> DoubleTag.valueOf(data.spread());
|
||||
default -> StringTag.valueOf(value);
|
||||
};
|
||||
default -> StringTag.valueOf(name);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,80 @@
|
|||
package com.atsuishio.superbwarfare.tools;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import net.minecraft.nbt.*;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public class TagDataParser {
|
||||
/**
|
||||
* 将JsonObject转换为NBT Tag
|
||||
*/
|
||||
public static CompoundTag parse(JsonObject object) {
|
||||
return parse(object, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将JsonObject转换为NBT Tag,并替换自定义数据
|
||||
*
|
||||
* @param object JsonObject
|
||||
* @param tagModifier 替换函数
|
||||
* @return 替换后的NBT Tag
|
||||
*/
|
||||
public static CompoundTag parse(JsonObject object, @Nullable Function<String, Tag> tagModifier) {
|
||||
var tag = new CompoundTag();
|
||||
|
||||
for (var d : object.entrySet()) {
|
||||
var parsed = parse(d.getValue(), tagModifier);
|
||||
if (parsed == null) continue;
|
||||
tag.put(d.getKey(), parsed);
|
||||
}
|
||||
|
||||
return tag;
|
||||
}
|
||||
|
||||
/**
|
||||
* 尝试将单个JsonElement转为NBT Tag,并替换自定义数据
|
||||
*
|
||||
* @param object JsonElement
|
||||
* @param tagModifier 替换函数
|
||||
* @return 替换后的NBT Tag
|
||||
*/
|
||||
public static @Nullable Tag parse(JsonElement object, @Nullable Function<String, Tag> tagModifier) {
|
||||
if (object.isJsonObject()) {
|
||||
// 递归处理嵌套内容
|
||||
var tag = new CompoundTag();
|
||||
for (var d : object.getAsJsonObject().entrySet()) {
|
||||
var parsed = parse(d.getValue(), tagModifier);
|
||||
if (parsed == null) continue;
|
||||
tag.put(d.getKey(), parsed);
|
||||
}
|
||||
return tag;
|
||||
} else if (object.isJsonArray()) {
|
||||
// 处理数组相关内容
|
||||
var tag = new ListTag();
|
||||
for (var d : object.getAsJsonArray()) {
|
||||
tag.add(parse(d, tagModifier));
|
||||
}
|
||||
return tag;
|
||||
} else if (object.isJsonPrimitive()) {
|
||||
// 处理基础数据
|
||||
var prime = object.getAsJsonPrimitive();
|
||||
if (prime.isString()) {
|
||||
// 替换自定义数据
|
||||
if (tagModifier != null) {
|
||||
var tag = tagModifier.apply(prime.getAsString());
|
||||
if (tag != null) return tag;
|
||||
}
|
||||
return StringTag.valueOf(prime.getAsString());
|
||||
} else if (prime.isNumber()) {
|
||||
return DoubleTag.valueOf(prime.getAsLong());
|
||||
} else if (prime.isBoolean()) {
|
||||
return ByteTag.valueOf(prime.getAsBoolean());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue