diff --git a/src/main/java/net/minestom/server/tag/Tag.java b/src/main/java/net/minestom/server/tag/Tag.java new file mode 100644 index 000000000..2b9f3ab21 --- /dev/null +++ b/src/main/java/net/minestom/server/tag/Tag.java @@ -0,0 +1,110 @@ +package net.minestom.server.tag; + +import org.jetbrains.annotations.NotNull; +import org.jglrxavpok.hephaistos.nbt.NBT; +import org.jglrxavpok.hephaistos.nbt.NBTCompound; + +import java.util.function.BiConsumer; +import java.util.function.Function; + +public class Tag { + + private final String key; + private final Function readFunction; + private final BiConsumer writeConsumer; + + private Tag(@NotNull String key, + @NotNull Function readFunction, + @NotNull BiConsumer writeConsumer) { + this.key = key; + this.readFunction = readFunction; + this.writeConsumer = writeConsumer; + } + + public @NotNull String getKey() { + return key; + } + + protected T read(@NotNull NBTCompound nbtCompound) { + return readFunction.apply(nbtCompound); + } + + protected void write(@NotNull NBTCompound nbtCompound, @NotNull T value) { + this.writeConsumer.accept(nbtCompound, value); + } + + public static @NotNull Tag Byte(@NotNull String key) { + return new Tag<>(key, + nbtCompound -> nbtCompound.getByte(key), + (nbtCompound, value) -> nbtCompound.setByte(key, value)); + } + + public static @NotNull Tag Short(@NotNull String key) { + return new Tag<>(key, + nbtCompound -> nbtCompound.getShort(key), + (nbtCompound, value) -> nbtCompound.setShort(key, value)); + } + + public static @NotNull Tag Integer(@NotNull String key) { + return new Tag<>(key, + nbtCompound -> nbtCompound.getInt(key), + (nbtCompound, integer) -> nbtCompound.setInt(key, integer)); + } + + public static @NotNull Tag Long(@NotNull String key) { + return new Tag<>(key, + nbtCompound -> nbtCompound.getLong(key), + (nbtCompound, value) -> nbtCompound.setLong(key, value)); + } + + public static @NotNull Tag Float(@NotNull String key) { + return new Tag<>(key, + nbtCompound -> nbtCompound.getFloat(key), + (nbtCompound, value) -> nbtCompound.setFloat(key, value)); + } + + public static @NotNull Tag Double(@NotNull String key) { + return new Tag<>(key, + nbtCompound -> nbtCompound.getDouble(key), + (nbtCompound, value) -> nbtCompound.setDouble(key, value)); + } + + public static @NotNull Tag ByteArray(@NotNull String key) { + return new Tag<>(key, + nbtCompound -> nbtCompound.getByteArray(key), + (nbtCompound, value) -> nbtCompound.setByteArray(key, value)); + } + + public static @NotNull Tag String(@NotNull String key) { + return new Tag<>(key, + nbtCompound -> nbtCompound.getString(key), + (nbtCompound, value) -> nbtCompound.setString(key, value)); + } + + public static @NotNull Tag NBT(@NotNull String key) { + return new Tag<>(key, + nbt -> { + var currentNBT = nbt.get(key); + + // Avoid a NPE when cloning a null variable. + if (currentNBT == null) { + return null; + } + + return currentNBT.deepClone(); + }, + ((nbt, value) -> nbt.set(key, value.deepClone()))); + } + + public static @NotNull Tag IntArray(@NotNull String key) { + return new Tag<>(key, + nbtCompound -> nbtCompound.getIntArray(key), + (nbtCompound, value) -> nbtCompound.setIntArray(key, value)); + } + + public static @NotNull Tag LongArray(@NotNull String key) { + return new Tag<>(key, + nbtCompound -> nbtCompound.getLongArray(key), + (nbtCompound, value) -> nbtCompound.setLongArray(key, value)); + } +} diff --git a/src/main/java/net/minestom/server/tag/TagGetter.java b/src/main/java/net/minestom/server/tag/TagGetter.java new file mode 100644 index 000000000..9f5b99852 --- /dev/null +++ b/src/main/java/net/minestom/server/tag/TagGetter.java @@ -0,0 +1,8 @@ +package net.minestom.server.tag; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public interface TagGetter { + @Nullable T getTag(@NotNull Tag tag); +} diff --git a/src/main/java/net/minestom/server/tag/TagSetter.java b/src/main/java/net/minestom/server/tag/TagSetter.java new file mode 100644 index 000000000..ecd3c8db5 --- /dev/null +++ b/src/main/java/net/minestom/server/tag/TagSetter.java @@ -0,0 +1,8 @@ +package net.minestom.server.tag; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public interface TagSetter { + void setTag(@NotNull Tag tag, @Nullable T value); +}