From 355beac4669108e3238feddfa5b5827a2c6e6468 Mon Sep 17 00:00:00 2001 From: themode Date: Wed, 23 Mar 2022 06:51:36 +0100 Subject: [PATCH] Unnecessary methods --- .../java/net/minestom/server/tag/Tag.java | 32 ++++++------------- .../minestom/server/tag/TagHandlerImpl.java | 8 +++-- 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/src/main/java/net/minestom/server/tag/Tag.java b/src/main/java/net/minestom/server/tag/Tag.java index 37d106e29..806cc4867 100644 --- a/src/main/java/net/minestom/server/tag/Tag.java +++ b/src/main/java/net/minestom/server/tag/Tag.java @@ -29,8 +29,8 @@ public class Tag { } private final String key; - private final Function readFunction; - private final Function writeFunction; + final Function readFunction; + final Function writeFunction; private final Supplier defaultValue; final int index; @@ -103,12 +103,15 @@ public class Tag { 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 { 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(@NotNull String key) { return tag(key, NBTByte.class, NBTByte::getValue, NBT::Byte); } diff --git a/src/main/java/net/minestom/server/tag/TagHandlerImpl.java b/src/main/java/net/minestom/server/tag/TagHandlerImpl.java index cb4cc7454..35e85984a 100644 --- a/src/main/java/net/minestom/server/tag/TagHandlerImpl.java +++ b/src/main/java/net/minestom/server/tag/TagHandlerImpl.java @@ -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(); + } } }