From 0f767da5f07daf3c2211890f2fa91f27b517be42 Mon Sep 17 00:00:00 2001 From: TheMode Date: Sun, 17 Apr 2022 00:16:59 +0200 Subject: [PATCH] Use NBT#getValue Signed-off-by: TheMode --- gradle/libs.versions.toml | 2 +- .../minestom/server/tag/TagNbtSeparator.java | 47 ++++--------------- 2 files changed, 11 insertions(+), 38 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 89b9bb1a8..023c70200 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -8,7 +8,7 @@ kotlin = "1.6.10" hydrazine = "1.7.2" dependencyGetter = "v1.0.1" minestomData = "895581d464" -hephaistos = "2.4.2" +hephaistos = "2.4.4" jetbrainsAnnotations = "23.0.0" # Terminal / Logging diff --git a/src/main/java/net/minestom/server/tag/TagNbtSeparator.java b/src/main/java/net/minestom/server/tag/TagNbtSeparator.java index 45e04c6a8..d0300f1b6 100644 --- a/src/main/java/net/minestom/server/tag/TagNbtSeparator.java +++ b/src/main/java/net/minestom/server/tag/TagNbtSeparator.java @@ -1,6 +1,9 @@ package net.minestom.server.tag; -import org.jglrxavpok.hephaistos.nbt.*; +import org.jglrxavpok.hephaistos.nbt.NBT; +import org.jglrxavpok.hephaistos.nbt.NBTCompound; +import org.jglrxavpok.hephaistos.nbt.NBTList; +import org.jglrxavpok.hephaistos.nbt.NBTType; import java.util.ArrayList; import java.util.List; @@ -47,20 +50,10 @@ final class TagNbtSeparator { } private static void convert(List path, String key, NBT nbt, Consumer consumer) { - if (nbt instanceof NBTByte nbtByte) { - consumer.accept(makeEntry(path, Tag.Byte(key), nbtByte.getValue())); - } else if (nbt instanceof NBTShort nbtShort) { - consumer.accept(makeEntry(path, Tag.Short(key), nbtShort.getValue())); - } else if (nbt instanceof NBTInt nbtInt) { - consumer.accept(makeEntry(path, Tag.Integer(key), nbtInt.getValue())); - } else if (nbt instanceof NBTLong nbtLong) { - consumer.accept(makeEntry(path, Tag.Long(key), nbtLong.getValue())); - } else if (nbt instanceof NBTFloat nbtFloat) { - consumer.accept(makeEntry(path, Tag.Float(key), nbtFloat.getValue())); - } else if (nbt instanceof NBTDouble nbtDouble) { - consumer.accept(makeEntry(path, Tag.Double(key), nbtDouble.getValue())); - } else if (nbt instanceof NBTString nbtString) { - consumer.accept(makeEntry(path, Tag.String(key), nbtString.getValue())); + var tagFunction = SUPPORTED_TYPES.get(nbt.getID()); + if (tagFunction != null) { + Tag tag = tagFunction.apply(key); + consumer.accept(makeEntry(path, tag, nbt.getValue())); } else if (nbt instanceof NBTCompound nbtCompound) { for (var ent : nbtCompound) { var newPath = new ArrayList<>(path); @@ -68,7 +61,7 @@ final class TagNbtSeparator { convert(newPath, ent.getKey(), ent.getValue(), consumer); } } else if (nbt instanceof NBTList nbtList) { - var tagFunction = SUPPORTED_TYPES.get(nbtList.getSubtagType()); + tagFunction = SUPPORTED_TYPES.get(nbtList.getSubtagType()); if (tagFunction == null) { // Invalid list subtype, fallback to nbt consumer.accept(makeEntry(path, Tag.NBT(key), nbt)); @@ -77,7 +70,7 @@ final class TagNbtSeparator { var tag = tagFunction.apply(key).list(); Object[] values = new Object[nbtList.getSize()]; for (int i = 0; i < values.length; i++) { - values[i] = nbtValue(nbtList.get(i)); + values[i] = nbtList.get(i).getValue(); } consumer.accept(makeEntry(path, Tag.class.cast(tag), List.of(values))); } catch (Exception e) { @@ -97,24 +90,4 @@ final class TagNbtSeparator { record Entry(Tag tag, T value) { } - - private static Object nbtValue(NBT nbt) throws IllegalArgumentException { - if (nbt instanceof NBTByte nbtByte) { - return nbtByte.getValue(); - } else if (nbt instanceof NBTShort nbtShort) { - return nbtShort.getValue(); - } else if (nbt instanceof NBTInt nbtInt) { - return nbtInt.getValue(); - } else if (nbt instanceof NBTLong nbtLong) { - return nbtLong.getValue(); - } else if (nbt instanceof NBTFloat nbtFloat) { - return nbtFloat.getValue(); - } else if (nbt instanceof NBTDouble nbtDouble) { - return nbtDouble.getValue(); - } else if (nbt instanceof NBTString nbtString) { - return nbtString.getValue(); - } else { - throw new IllegalArgumentException("Unsupported NBT type: " + nbt.getClass().getName()); - } - } }