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