Unnecessary methods

This commit is contained in:
themode 2022-03-23 06:51:36 +01:00
parent ee5ca9050c
commit 355beac466
2 changed files with 16 additions and 24 deletions

View File

@ -29,8 +29,8 @@ public class Tag<T> {
} }
private final String key; private final String key;
private final Function<NBT, T> readFunction; final Function<NBT, T> readFunction;
private final Function<T, NBT> writeFunction; final Function<T, NBT> writeFunction;
private final Supplier<T> defaultValue; private final Supplier<T> defaultValue;
final int index; final int index;
@ -103,12 +103,15 @@ public class Tag<T> {
public @Nullable T read(@NotNull NBTCompoundLike nbt) { public @Nullable T read(@NotNull NBTCompoundLike nbt) {
final String key = this.key; final String key = this.key;
if (key.isEmpty()) { final NBT readable = key.isEmpty() ? nbt.toCompound() : nbt.get(key);
// Special handling for view tag final T result;
return convertToValue(nbt.toCompound()); try {
if (readable == null || (result = readFunction.apply(readable)) == null)
return createDefault();
return result;
} catch (ClassCastException e) {
return createDefault();
} }
final NBT subTag = nbt.get(key);
return convertToValue(subTag);
} }
T createDefault() { T createDefault() {
@ -133,21 +136,6 @@ public class Tag<T> {
write(nbtCompound, (T) value); write(nbtCompound, (T) value);
} }
T convertToValue(NBT nbt) {
final T result;
try {
if (nbt == null || (result = readFunction.apply(nbt)) == null)
return createDefault();
return result;
} catch (ClassCastException e) {
return createDefault();
}
}
NBT convertToNbt(T value) {
return writeFunction.apply(value);
}
public static @NotNull Tag<Byte> Byte(@NotNull String key) { public static @NotNull Tag<Byte> Byte(@NotNull String key) {
return tag(key, NBTByte.class, NBTByte::getValue, NBT::Byte); return tag(key, NBTByte.class, NBTByte::getValue, NBT::Byte);
} }

View File

@ -122,7 +122,11 @@ final class TagHandlerImpl implements TagHandler {
} }
// Value must be parsed from nbt if the tag is different // Value must be parsed from nbt if the tag is different
NBT nbt = entry.nbt; NBT nbt = entry.nbt;
if (nbt == null) entry.nbt = nbt = entryTag.convertToNbt(entry.value); if (nbt == null) entry.nbt = nbt = (NBT) entryTag.writeFunction.apply(entry.value);
return tag.convertToValue(nbt); try {
return tag.readFunction.apply(nbt);
} catch (ClassCastException e) {
return tag.createDefault();
}
} }
} }