From 89b35a056967e1d24b4c82fce6adb56217a39be1 Mon Sep 17 00:00:00 2001 From: themode Date: Sat, 3 Apr 2021 16:03:03 +0200 Subject: [PATCH] Cleanup ItemTag --- .../net/minestom/server/item/ItemTag.java | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/main/java/net/minestom/server/item/ItemTag.java b/src/main/java/net/minestom/server/item/ItemTag.java index a0a19f28f..4ad673136 100644 --- a/src/main/java/net/minestom/server/item/ItemTag.java +++ b/src/main/java/net/minestom/server/item/ItemTag.java @@ -3,34 +3,39 @@ package net.minestom.server.item; import org.jetbrains.annotations.NotNull; import org.jglrxavpok.hephaistos.nbt.NBTCompound; -public abstract class ItemTag { +import java.util.function.BiConsumer; +import java.util.function.Function; + +public class ItemTag { private final String key; + private final Function readFunction; + private final BiConsumer writeConsumer; - private ItemTag(@NotNull String key) { + private ItemTag(@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 abstract T read(@NotNull NBTCompound nbtCompound); + protected T read(@NotNull NBTCompound nbtCompound) { + return readFunction.apply(nbtCompound); + } - protected abstract void write(@NotNull NBTCompound nbtCompound, @NotNull T value); + protected void write(@NotNull NBTCompound nbtCompound, @NotNull T value) { + this.writeConsumer.accept(nbtCompound, value); + } public static @NotNull ItemTag Integer(@NotNull String key) { - return new ItemTag<>(key) { - @Override - protected Integer read(@NotNull NBTCompound nbtCompound) { - return nbtCompound.getInt(key); - } - - @Override - protected void write(@NotNull NBTCompound nbtCompound, @NotNull Integer value) { - nbtCompound.setInt(key, value); - } - }; + return new ItemTag<>(key, + nbtCompound -> nbtCompound.getInt(key), + (nbtCompound, integer) -> nbtCompound.setInt(key, integer)); } }