mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-04 07:28:19 +01:00
Avoid potential stacktrace generation
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
a4adbb49fe
commit
917302cbbb
@ -12,26 +12,26 @@ import java.util.function.Function;
|
||||
* Basic serializers for {@link Tag tags}.
|
||||
*/
|
||||
final class Serializers {
|
||||
static final Entry<TagHandlerImpl, NBTCompound> PATH = new Entry<>(TagHandlerImpl::fromCompound, TagHandlerImpl::asCompound);
|
||||
static final Entry<TagHandlerImpl, NBTCompound> PATH = new Entry<>(NBTCompound.class, TagHandlerImpl::fromCompound, TagHandlerImpl::asCompound);
|
||||
|
||||
static final Entry<Byte, NBTByte> BYTE = new Entry<>(NBTByte::getValue, NBT::Byte);
|
||||
static final Entry<Boolean, NBTByte> BOOLEAN = new Entry<>(NBTByte::asBoolean, NBT::Boolean);
|
||||
static final Entry<Short, NBTShort> SHORT = new Entry<>(NBTShort::getValue, NBT::Short);
|
||||
static final Entry<Integer, NBTInt> INT = new Entry<>(NBTInt::getValue, NBT::Int);
|
||||
static final Entry<Long, NBTLong> LONG = new Entry<>(NBTLong::getValue, NBT::Long);
|
||||
static final Entry<Float, NBTFloat> FLOAT = new Entry<>(NBTFloat::getValue, NBT::Float);
|
||||
static final Entry<Double, NBTDouble> DOUBLE = new Entry<>(NBTDouble::getValue, NBT::Double);
|
||||
static final Entry<String, NBTString> STRING = new Entry<>(NBTString::getValue, NBT::String);
|
||||
static final Entry<NBT, NBT> NBT_ENTRY = new Entry<>(Function.identity(), Function.identity());
|
||||
static final Entry<Byte, NBTByte> BYTE = new Entry<>(NBTByte.class, NBTByte::getValue, NBT::Byte);
|
||||
static final Entry<Boolean, NBTByte> BOOLEAN = new Entry<>(NBTByte.class, NBTByte::asBoolean, NBT::Boolean);
|
||||
static final Entry<Short, NBTShort> SHORT = new Entry<>(NBTShort.class, NBTShort::getValue, NBT::Short);
|
||||
static final Entry<Integer, NBTInt> INT = new Entry<>(NBTInt.class, NBTInt::getValue, NBT::Int);
|
||||
static final Entry<Long, NBTLong> LONG = new Entry<>(NBTLong.class, NBTLong::getValue, NBT::Long);
|
||||
static final Entry<Float, NBTFloat> FLOAT = new Entry<>(NBTFloat.class, NBTFloat::getValue, NBT::Float);
|
||||
static final Entry<Double, NBTDouble> DOUBLE = new Entry<>(NBTDouble.class, NBTDouble::getValue, NBT::Double);
|
||||
static final Entry<String, NBTString> STRING = new Entry<>(NBTString.class, NBTString::getValue, NBT::String);
|
||||
static final Entry<NBT, NBT> NBT_ENTRY = new Entry<>(NBT.class, Function.identity(), Function.identity());
|
||||
|
||||
static final Entry<java.util.UUID, NBTIntArray> UUID = new Entry<>(intArray -> Utils.intArrayToUuid(intArray.getValue().copyArray()),
|
||||
static final Entry<java.util.UUID, NBTIntArray> UUID = new Entry<>(NBTIntArray.class, intArray -> Utils.intArrayToUuid(intArray.getValue().copyArray()),
|
||||
uuid -> NBT.IntArray(Utils.uuidToIntArray(uuid)));
|
||||
static final Entry<ItemStack, NBTCompound> ITEM = new Entry<>(ItemStack::fromItemNBT, ItemStack::toItemNBT);
|
||||
static final Entry<Component, NBTString> COMPONENT = new Entry<>(input -> GsonComponentSerializer.gson().deserialize(input.getValue()),
|
||||
static final Entry<ItemStack, NBTCompound> ITEM = new Entry<>(NBTCompound.class, ItemStack::fromItemNBT, ItemStack::toItemNBT);
|
||||
static final Entry<Component, NBTString> COMPONENT = new Entry<>(NBTString.class, input -> GsonComponentSerializer.gson().deserialize(input.getValue()),
|
||||
component -> NBT.String(GsonComponentSerializer.gson().serialize(component)));
|
||||
|
||||
static <T> Entry<T, NBTCompound> fromTagSerializer(TagSerializer<T> serializer) {
|
||||
return new Serializers.Entry<>(
|
||||
return new Serializers.Entry<>(NBTCompound.class,
|
||||
(NBTCompound compound) -> {
|
||||
if (compound.isEmpty()) return null;
|
||||
return serializer.read(TagHandler.fromCompound(compound));
|
||||
@ -44,6 +44,13 @@ final class Serializers {
|
||||
});
|
||||
}
|
||||
|
||||
record Entry<T, N extends NBT>(Function<N, T> read, Function<T, N> write) {
|
||||
record Entry<T, N extends NBT>(Class<N> nbtType, Function<N, T> reader, Function<T, N> writer) {
|
||||
T read(N nbt) {
|
||||
return reader.apply(nbt);
|
||||
}
|
||||
|
||||
N write(T value) {
|
||||
return writer.apply(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -62,7 +62,7 @@ public class Tag<T> {
|
||||
}
|
||||
|
||||
static <T, N extends NBT> Tag<T> tag(@NotNull String key, @NotNull Serializers.Entry<T, N> entry) {
|
||||
return new Tag<>(INDEX_MAP.get(key), key, entry.read(), (Serializers.Entry<T, NBT>) entry,
|
||||
return new Tag<>(INDEX_MAP.get(key), key, entry.reader(), (Serializers.Entry<T, NBT>) entry,
|
||||
null, null, null, 0);
|
||||
}
|
||||
|
||||
@ -98,13 +98,13 @@ public class Tag<T> {
|
||||
public <R> Tag<R> map(@NotNull Function<T, R> readMap,
|
||||
@NotNull Function<R, T> writeMap) {
|
||||
var entry = this.entry;
|
||||
final Function<NBT, R> readFunction = entry.read().andThen(t -> {
|
||||
final Function<NBT, R> readFunction = entry.reader().andThen(t -> {
|
||||
if (t == null) return null;
|
||||
return readMap.apply(t);
|
||||
});
|
||||
final Function<R, NBT> writeFunction = writeMap.andThen(entry.write());
|
||||
final Function<R, NBT> writeFunction = writeMap.andThen(entry.writer());
|
||||
return new Tag<>(index, key, readMap,
|
||||
new Serializers.Entry<>(readFunction, writeFunction),
|
||||
new Serializers.Entry<>(entry.nbtType(), readFunction, writeFunction),
|
||||
// Default value
|
||||
() -> {
|
||||
T defaultValue = createDefault();
|
||||
@ -118,9 +118,10 @@ public class Tag<T> {
|
||||
@Contract(value = "-> new", pure = true)
|
||||
public Tag<List<T>> list() {
|
||||
var entry = this.entry;
|
||||
var readFunction = entry.read();
|
||||
var writeFunction = entry.write();
|
||||
var listEntry = new Serializers.Entry<List<T>, NBT>(
|
||||
var readFunction = entry.reader();
|
||||
var writeFunction = entry.writer();
|
||||
var listEntry = new Serializers.Entry<List<T>, NBTList>(
|
||||
NBTList.class,
|
||||
read -> {
|
||||
var list = (NBTList<?>) read;
|
||||
final int size = list.getSize();
|
||||
@ -161,7 +162,8 @@ public class Tag<T> {
|
||||
}
|
||||
return shallowCopy ? List.copyOf(ts) : List.of(array);
|
||||
} : List::copyOf;
|
||||
return new Tag<>(index, key, readComparator, listEntry, null, path, co, listScope + 1);
|
||||
return new Tag<>(index, key, readComparator, Serializers.Entry.class.cast(listEntry),
|
||||
null, path, co, listScope + 1);
|
||||
}
|
||||
|
||||
@ApiStatus.Experimental
|
||||
@ -184,7 +186,7 @@ public class Tag<T> {
|
||||
final NBT readable = isView() ? nbt.toCompound() : nbt.get(key);
|
||||
final T result;
|
||||
try {
|
||||
if (readable == null || (result = entry.read().apply(readable)) == null)
|
||||
if (readable == null || (result = entry.read(readable)) == null)
|
||||
return createDefault();
|
||||
return result;
|
||||
} catch (ClassCastException e) {
|
||||
@ -194,7 +196,7 @@ public class Tag<T> {
|
||||
|
||||
public void write(@NotNull MutableNBTCompound nbtCompound, @Nullable T value) {
|
||||
if (value != null) {
|
||||
final NBT nbt = entry.write().apply(value);
|
||||
final NBT nbt = entry.write(value);
|
||||
if (isView()) nbtCompound.copyFrom((NBTCompoundLike) nbt);
|
||||
else nbtCompound.set(key, nbt);
|
||||
} else {
|
||||
|
@ -186,11 +186,9 @@ final class TagHandlerImpl implements TagHandler {
|
||||
}
|
||||
// Value must be parsed from nbt if the tag is different
|
||||
final NBT nbt = entry.updatedNbt();
|
||||
try {
|
||||
return tag.entry.read().apply(nbt);
|
||||
} catch (ClassCastException e) {
|
||||
return tag.createDefault();
|
||||
}
|
||||
final Serializers.Entry<T, NBT> serializerEntry = tag.entry;
|
||||
return serializerEntry.nbtType().isAssignableFrom(nbt.getClass()) ?
|
||||
serializerEntry.read(nbt) : tag.createDefault();
|
||||
}
|
||||
|
||||
private static Int2ObjectOpenHashMap<Entry<?>> traversePath(Tag.PathEntry[] paths,
|
||||
@ -256,7 +254,7 @@ final class TagHandlerImpl implements TagHandler {
|
||||
@Override
|
||||
public NBT updatedNbt() {
|
||||
NBT nbt = this.nbt;
|
||||
if (nbt == null) this.nbt = nbt = tag.entry.write().apply(value);
|
||||
if (nbt == null) this.nbt = nbt = tag.entry.write(value);
|
||||
return nbt;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user