From 6ecc4691b1b8e3eb8ded0fd8622022f619156fda Mon Sep 17 00:00:00 2001 From: glisco Date: Tue, 5 Dec 2023 01:06:19 +0100 Subject: [PATCH] [endec] completely remove NbtKey (superseded by KeyedEndec) --- .../wispforest/owo/mixin/ItemStackMixin.java | 31 +-- .../owo/mixin/NbtCompoundMixin.java | 30 +-- .../io/wispforest/owo/nbt/NbtCarrier.java | 52 ----- .../java/io/wispforest/owo/nbt/NbtKey.java | 200 ------------------ .../owo/serialization/Deserializer.java | 2 +- .../impl/bytebuf/ByteBufDeserializer.java | 4 +- .../impl/data/DataInputDeserializer.java | 4 +- .../impl/edm/EdmDeserializer.java | 2 +- .../impl/json/JsonDeserializer.java | 2 +- .../impl/nbt/NbtDeserializer.java | 2 +- src/main/resources/fabric.mod.json | 2 - .../uwu/items/UwuTestStickItem.java | 10 +- 12 files changed, 17 insertions(+), 324 deletions(-) delete mode 100644 src/main/java/io/wispforest/owo/nbt/NbtCarrier.java delete mode 100644 src/main/java/io/wispforest/owo/nbt/NbtKey.java diff --git a/src/main/java/io/wispforest/owo/mixin/ItemStackMixin.java b/src/main/java/io/wispforest/owo/mixin/ItemStackMixin.java index aa5b98df..a8f5404f 100644 --- a/src/main/java/io/wispforest/owo/mixin/ItemStackMixin.java +++ b/src/main/java/io/wispforest/owo/mixin/ItemStackMixin.java @@ -1,7 +1,5 @@ package io.wispforest.owo.mixin; -import io.wispforest.owo.nbt.NbtCarrier; -import io.wispforest.owo.nbt.NbtKey; import io.wispforest.owo.serialization.MapCarrier; import io.wispforest.owo.serialization.impl.KeyedEndec; import io.wispforest.owo.serialization.impl.forwarding.ForwardingDeserializer; @@ -17,7 +15,7 @@ @SuppressWarnings("AddedMixinMembersNamePattern") @Mixin(ItemStack.class) -public abstract class ItemStackMixin implements NbtCarrier, MapCarrier { +public abstract class ItemStackMixin implements MapCarrier { @Shadow private @Nullable NbtCompound nbt; @@ -25,34 +23,9 @@ public abstract class ItemStackMixin implements NbtCarrier, MapCarrier { @Shadow public abstract NbtCompound getOrCreateNbt(); - // --- NbtCarrier (deprecated) --- - - @Override - public T get(@NotNull NbtKey key) { - return key.get(this.getOrCreateNbt()); - } - - @Override - public void put(@NotNull NbtKey key, @NotNull T value) { - key.put(this.getOrCreateNbt(), value); - } - - @Override - public void delete(@NotNull NbtKey key) { - if (this.nbt == null) return; - key.delete(this.nbt); - } - - @Override - public boolean has(@NotNull NbtKey key) { - return this.nbt != null && key.isIn(this.nbt); - } - - // --- MapCarrier --- - @Override public T getWithErrors(@NotNull KeyedEndec key) { - if(!this.has(key)) return key.defaultValue(); + if (!this.has(key)) return key.defaultValue(); return key.endec() .decodeFully(e -> ForwardingDeserializer.humanReadable(NbtDeserializer.of(e)), this.nbt.get(key.key())); } diff --git a/src/main/java/io/wispforest/owo/mixin/NbtCompoundMixin.java b/src/main/java/io/wispforest/owo/mixin/NbtCompoundMixin.java index 9753135a..628c928b 100644 --- a/src/main/java/io/wispforest/owo/mixin/NbtCompoundMixin.java +++ b/src/main/java/io/wispforest/owo/mixin/NbtCompoundMixin.java @@ -1,7 +1,5 @@ package io.wispforest.owo.mixin; -import io.wispforest.owo.nbt.NbtCarrier; -import io.wispforest.owo.nbt.NbtKey; import io.wispforest.owo.serialization.MapCarrier; import io.wispforest.owo.serialization.impl.KeyedEndec; import io.wispforest.owo.serialization.impl.forwarding.ForwardingDeserializer; @@ -17,7 +15,7 @@ @SuppressWarnings("AddedMixinMembersNamePattern") @Mixin(NbtCompound.class) -public abstract class NbtCompoundMixin implements NbtCarrier, MapCarrier { +public abstract class NbtCompoundMixin implements MapCarrier { @Shadow public abstract @Nullable NbtElement get(String key); @@ -28,33 +26,9 @@ public abstract class NbtCompoundMixin implements NbtCarrier, MapCarrier { @Shadow public abstract boolean contains(String key); - // --- NbtCarrier (deprecated) --- - - @Override - public T get(@NotNull NbtKey key) { - return key.get((NbtCompound) (Object) this); - } - - @Override - public void put(@NotNull NbtKey key, @NotNull T value) { - key.put((NbtCompound) (Object) this, value); - } - - @Override - public void delete(@NotNull NbtKey key) { - key.delete((NbtCompound) (Object) this); - } - - @Override - public boolean has(@NotNull NbtKey key) { - return key.isIn((NbtCompound) (Object) this); - } - - // --- MapCarrier --- - @Override public T getWithErrors(@NotNull KeyedEndec key) { - if(!this.has(key)) return key.defaultValue(); + if (!this.has(key)) return key.defaultValue(); return key.endec() .decodeFully(e -> ForwardingDeserializer.humanReadable(NbtDeserializer.of(e)), this.get(key.key())); } diff --git a/src/main/java/io/wispforest/owo/nbt/NbtCarrier.java b/src/main/java/io/wispforest/owo/nbt/NbtCarrier.java deleted file mode 100644 index a3bff385..00000000 --- a/src/main/java/io/wispforest/owo/nbt/NbtCarrier.java +++ /dev/null @@ -1,52 +0,0 @@ -package io.wispforest.owo.nbt; - -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.function.Function; - -public interface NbtCarrier { - - // Interface specification - - default T get(@NotNull NbtKey key) { - throw new IllegalStateException("Interface default method called"); - } - - default void put(@NotNull NbtKey key, @NotNull T value) { - throw new IllegalStateException("Interface default method called"); - } - - default void delete(@NotNull NbtKey key) { - throw new IllegalStateException("Interface default method called"); - } - - default boolean has(@NotNull NbtKey key) { - throw new IllegalStateException("Interface default method called"); - } - - // Default implementations - - default T getOr(@NotNull NbtKey key, @Nullable T defaultValue) { - return this.has(key) ? this.get(key) : defaultValue; - } - - default void putIfNotNull(@NotNull NbtKey key, @Nullable T value) { - if (value == null) return; - this.put(key, value); - } - - default void copy(@NotNull NbtKey key, @NotNull NbtCarrier other) { - other.put(key, this.get(key)); - } - - default void copyIfPresent(@NotNull NbtKey key, @NotNull NbtCarrier other) { - if (!this.has(key)) return; - this.copy(key, other); - } - - default void mutate(@NotNull NbtKey key, @NotNull Function mutator) { - this.put(key, mutator.apply(this.get(key))); - } - -} diff --git a/src/main/java/io/wispforest/owo/nbt/NbtKey.java b/src/main/java/io/wispforest/owo/nbt/NbtKey.java deleted file mode 100644 index 83753ae0..00000000 --- a/src/main/java/io/wispforest/owo/nbt/NbtKey.java +++ /dev/null @@ -1,200 +0,0 @@ -package io.wispforest.owo.nbt; - -import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NbtCompound; -import net.minecraft.nbt.NbtElement; -import net.minecraft.nbt.NbtList; -import net.minecraft.registry.Registry; -import net.minecraft.util.Identifier; -import org.apache.logging.log4j.util.TriConsumer; -import org.jetbrains.annotations.NotNull; - -import java.util.function.BiFunction; -import java.util.function.Function; - -/** - * A utility class for serializing data into {@link NbtCompound} - * instances. {@link Type} instances are used for holding the - * actual serializer functions while the key itself carries information - * about what string key to use. - *

- * In order to conveniently use instances of this class, employ the methods - * defined on {@link NbtCarrier} - this is interface-injected onto - * {@link ItemStack} and {@link NbtCompound} by default - * - * @param The type of data a given instance can serialize - */ -public class NbtKey { - - protected final String key; - protected final Type type; - - /** - * Creates a new key instance used for storing data of type - * {@code T} into NBT compounds with the given string as key - * - * @param key The string key to use as index into the NBT compound - * @param type The type object that holds the serializer implementations - */ - public NbtKey(String key, Type type) { - this.key = key; - this.type = type; - } - - /** - * @deprecated Use {@link NbtCarrier#get(NbtKey)} instead - */ - @Deprecated - public T get(@NotNull NbtCompound nbt) { - return this.type.getter.apply(nbt, this.key); - } - - /** - * @deprecated Use {@link NbtCarrier#put(NbtKey, T)} instead - */ - @Deprecated - public void put(@NotNull NbtCompound nbt, T value) { - this.type.setter.accept(nbt, this.key, value); - } - - /** - * @deprecated Use {@link NbtCarrier#delete(NbtKey)} instead - */ - @Deprecated - public void delete(@NotNull NbtCompound nbt) { - nbt.remove(this.key); - } - - /** - * @deprecated Use {@link NbtCarrier#has(NbtKey)} instead - */ - @Deprecated - public boolean isIn(@NotNull NbtCompound nbt) { - return nbt.contains(this.key, this.type.nbtEquivalent); - } - - /** - * A {@link NbtKey} used for serializing a {@link NbtList} of - * the given type - * - * @param The type of elements in the list - */ - public static final class ListKey extends NbtKey { - - private final Type elementType; - - public ListKey(String key, Type elementType) { - super(key, null); - this.elementType = elementType; - } - - @Override - public NbtList get(@NotNull NbtCompound nbt) { - return nbt.getList(this.key, this.elementType.nbtEquivalent); - } - - @Override - public void put(@NotNull NbtCompound nbt, NbtList value) { - nbt.put(this.key, value); - } - - @Override - public boolean isIn(@NotNull NbtCompound nbt) { - return nbt.contains(this.key, NbtElement.LIST_TYPE); - } - } - - /** - * A container type holding serialization functions, - * used for creating {@link NbtKey} instances - */ - public static final class Type { - public static final Type BYTE = new Type<>(NbtElement.BYTE_TYPE, NbtCompound::getByte, NbtCompound::putByte); - public static final Type SHORT = new Type<>(NbtElement.SHORT_TYPE, NbtCompound::getShort, NbtCompound::putShort); - public static final Type INT = new Type<>(NbtElement.INT_TYPE, NbtCompound::getInt, NbtCompound::putInt); - public static final Type LONG = new Type<>(NbtElement.LONG_TYPE, NbtCompound::getLong, NbtCompound::putLong); - public static final Type FLOAT = new Type<>(NbtElement.FLOAT_TYPE, NbtCompound::getFloat, NbtCompound::putFloat); - public static final Type DOUBLE = new Type<>(NbtElement.DOUBLE_TYPE, NbtCompound::getDouble, NbtCompound::putDouble); - public static final Type BYTE_ARRAY = new Type<>(NbtElement.BYTE_ARRAY_TYPE, NbtCompound::getByteArray, NbtCompound::putByteArray); - public static final Type STRING = new Type<>(NbtElement.STRING_TYPE, NbtCompound::getString, NbtCompound::putString); - public static final Type COMPOUND = new Type<>(NbtElement.COMPOUND_TYPE, NbtCompound::getCompound, NbtCompound::put); - public static final Type INT_ARRAY = new Type<>(NbtElement.INT_ARRAY_TYPE, NbtCompound::getIntArray, NbtCompound::putIntArray); - public static final Type LONG_ARRAY = new Type<>(NbtElement.LONG_ARRAY_TYPE, NbtCompound::getLongArray, NbtCompound::putLongArray); - public static final Type ITEM_STACK = new Type<>(NbtElement.COMPOUND_TYPE, Type::readItemStack, Type::writeItemStack); - public static final Type IDENTIFIER = new Type<>(NbtElement.STRING_TYPE, Type::readIdentifier, Type::writeIdentifier); - public static final Type BOOLEAN = new Type<>(NbtElement.BYTE_TYPE, NbtCompound::getBoolean, NbtCompound::putBoolean); - - private final byte nbtEquivalent; - private final BiFunction getter; - private final TriConsumer setter; - - private Type(byte nbtEquivalent, BiFunction getter, TriConsumer setter) { - this.nbtEquivalent = nbtEquivalent; - this.getter = getter; - this.setter = setter; - } - - /** - * Creates a new type that applies the given functions on top of - * this type. This allows easily composing types by abstracting away - * the underlying NBT compound - * - * @param getter The getter function to convert from this type's value type to the new one - * @param setter The setter function to convert from the new value type to this type's one - * @param The value type of the created type - * @return The new key - */ - public Type then(Function getter, Function setter) { - return new Type<>(this.nbtEquivalent, - (compound, s) -> getter.apply(this.getter.apply(compound, s)), - (compound, s, r) -> this.setter.accept(compound, s, setter.apply(r))); - } - - /** - * Creates a new {@link Type} that supports reading and writing data of type {@code T} - * into {@link NbtCompound} instances. Use this if you want to store data that is - * not supported by the default provided types - * - * @param nbtType The type of NBT element that is used to represent the data, - * see {@link NbtElement} for the relevant constants - * @param getter The function used for writing objects to an {@code NbtCompound} - * @param setter The function used for reading objects from an {@code NbtCompound} - * @param The type of data the created key can serialize - * @return The created Type instance - */ - public static Type of(byte nbtType, BiFunction getter, TriConsumer setter) { - return new Type<>(nbtType, getter, setter); - } - - /** - * Creates a new type that serializes registry entries of the given - * registry using their ID in string form - * - * @param registry The registry of which to serialize entries - * @param The type of registry entry to serialize - * @return The created type - */ - public static Type ofRegistry(Registry registry) { - return new Type<>(NbtElement.STRING_TYPE, - (compound, s) -> registry.get(new Identifier(compound.getString(s))), - (compound, s, t) -> compound.putString(s, registry.getId(t).toString())); - } - - private static void writeItemStack(NbtCompound nbt, String key, ItemStack stack) { - nbt.put(key, stack.writeNbt(new NbtCompound())); - } - - private static ItemStack readItemStack(NbtCompound nbt, String key) { - return nbt.contains(key, NbtElement.COMPOUND_TYPE) ? ItemStack.fromNbt(nbt.getCompound(key)) : ItemStack.EMPTY; - } - - private static void writeIdentifier(NbtCompound nbt, String key, Identifier identifier) { - nbt.putString(key, identifier.toString()); - } - - private static Identifier readIdentifier(NbtCompound nbt, String key) { - return nbt.contains(key, NbtElement.STRING_TYPE) ? new Identifier(nbt.getString(key)) : null; - } - } - -} \ No newline at end of file diff --git a/src/main/java/io/wispforest/owo/serialization/Deserializer.java b/src/main/java/io/wispforest/owo/serialization/Deserializer.java index 3adf017a..12bf7f0f 100644 --- a/src/main/java/io/wispforest/owo/serialization/Deserializer.java +++ b/src/main/java/io/wispforest/owo/serialization/Deserializer.java @@ -61,7 +61,7 @@ interface Struct { * Decode the value of field {@code name} using {@code endec}. If no * such field exists in the serialized data, an exception is thrown */ - @NotNull F field(String name, Endec endec); + @Nullable F field(String name, Endec endec); /** * Decode the value of field {@code name} using {@code endec}. If no diff --git a/src/main/java/io/wispforest/owo/serialization/impl/bytebuf/ByteBufDeserializer.java b/src/main/java/io/wispforest/owo/serialization/impl/bytebuf/ByteBufDeserializer.java index 78c21bc5..85ecece6 100644 --- a/src/main/java/io/wispforest/owo/serialization/impl/bytebuf/ByteBufDeserializer.java +++ b/src/main/java/io/wispforest/owo/serialization/impl/bytebuf/ByteBufDeserializer.java @@ -162,12 +162,12 @@ public V next() { } @Override - public @NotNull F field(String name, Endec endec) { + public @Nullable F field(String name, Endec endec) { return this.field(name, endec, null); } @Override - public @NotNull F field(String name, Endec endec, @Nullable F defaultValue) { + public @Nullable F field(String name, Endec endec, @Nullable F defaultValue) { return endec.decode(ByteBufDeserializer.this); } } diff --git a/src/main/java/io/wispforest/owo/serialization/impl/data/DataInputDeserializer.java b/src/main/java/io/wispforest/owo/serialization/impl/data/DataInputDeserializer.java index a27e883d..61385da5 100644 --- a/src/main/java/io/wispforest/owo/serialization/impl/data/DataInputDeserializer.java +++ b/src/main/java/io/wispforest/owo/serialization/impl/data/DataInputDeserializer.java @@ -208,12 +208,12 @@ public V next() { } @Override - public @NotNull F field(String name, Endec endec) { + public @Nullable F field(String name, Endec endec) { return endec.decode(DataInputDeserializer.this); } @Override - public F field(@Nullable String field, Endec endec, @Nullable F defaultValue) { + public @Nullable F field(@Nullable String field, Endec endec, @Nullable F defaultValue) { return endec.decode(DataInputDeserializer.this); } } diff --git a/src/main/java/io/wispforest/owo/serialization/impl/edm/EdmDeserializer.java b/src/main/java/io/wispforest/owo/serialization/impl/edm/EdmDeserializer.java index 322b9878..f5e43dd2 100644 --- a/src/main/java/io/wispforest/owo/serialization/impl/edm/EdmDeserializer.java +++ b/src/main/java/io/wispforest/owo/serialization/impl/edm/EdmDeserializer.java @@ -229,7 +229,7 @@ private Struct(java.util.Map> map) { } @Override - public @NotNull F field(String name, Endec endec) { + public @Nullable F field(String name, Endec endec) { if (!this.map.containsKey(name)) { throw new IllegalStateException("Field '" + name + "' was missing from serialized data, but no default value was provided"); } diff --git a/src/main/java/io/wispforest/owo/serialization/impl/json/JsonDeserializer.java b/src/main/java/io/wispforest/owo/serialization/impl/json/JsonDeserializer.java index 356cb73e..ab55874e 100644 --- a/src/main/java/io/wispforest/owo/serialization/impl/json/JsonDeserializer.java +++ b/src/main/java/io/wispforest/owo/serialization/impl/json/JsonDeserializer.java @@ -260,7 +260,7 @@ private Struct(JsonObject object) { } @Override - public @NotNull F field(String name, Endec endec) { + public @Nullable F field(String name, Endec endec) { if (!this.object.has(name)) { throw new IllegalStateException("Field '" + name + "' was missing from serialized data, but no default value was provided"); } diff --git a/src/main/java/io/wispforest/owo/serialization/impl/nbt/NbtDeserializer.java b/src/main/java/io/wispforest/owo/serialization/impl/nbt/NbtDeserializer.java index 3e460672..b28a84a7 100644 --- a/src/main/java/io/wispforest/owo/serialization/impl/nbt/NbtDeserializer.java +++ b/src/main/java/io/wispforest/owo/serialization/impl/nbt/NbtDeserializer.java @@ -244,7 +244,7 @@ public Struct(NbtCompound compound) { } @Override - public @NotNull F field(String name, Endec endec) { + public @Nullable F field(String name, Endec endec) { if (!this.compound.contains(name)) { throw new IllegalStateException("Field '" + name + "' was missing from serialized data, but no default value was provided"); } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 238936f0..7de60dbc 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -54,11 +54,9 @@ }, "loom:injected_interfaces": { "net/minecraft/class_1799": [ - "io/wispforest/owo/nbt/NbtCarrier", "io/wispforest/owo/serialization/MapCarrier" ], "net/minecraft/class_2487": [ - "io/wispforest/owo/nbt/NbtCarrier", "io/wispforest/owo/serialization/MapCarrier" ], "net/minecraft/class_2540": [ diff --git a/src/testmod/java/io/wispforest/uwu/items/UwuTestStickItem.java b/src/testmod/java/io/wispforest/uwu/items/UwuTestStickItem.java index fdbd39cc..69408e58 100644 --- a/src/testmod/java/io/wispforest/uwu/items/UwuTestStickItem.java +++ b/src/testmod/java/io/wispforest/uwu/items/UwuTestStickItem.java @@ -2,8 +2,10 @@ import io.wispforest.owo.itemgroup.OwoItemGroup; import io.wispforest.owo.itemgroup.OwoItemSettings; -import io.wispforest.owo.nbt.NbtKey; import io.wispforest.owo.ops.WorldOps; +import io.wispforest.owo.serialization.BuiltInEndecs; +import io.wispforest.owo.serialization.Endec; +import io.wispforest.owo.serialization.impl.KeyedEndec; import io.wispforest.uwu.Uwu; import io.wispforest.uwu.text.BasedTextContent; import net.minecraft.enchantment.Enchantments; @@ -24,9 +26,7 @@ public class UwuTestStickItem extends Item { - private static final NbtKey TEXT_KEY = new NbtKey<>("Text", NbtKey.Type.of(NbtElement.STRING_TYPE, - (compound, s) -> Text.Serializer.fromJson(compound.getString(s)), - (compound, s, text) -> compound.putString(s, Text.Serializer.toJson(text)))); + private static final KeyedEndec TEXT_KEY = BuiltInEndecs.TEXT.keyed("Text", Text.empty()); public UwuTestStickItem() { super(new OwoItemSettings().group(Uwu.SIX_TAB_GROUP).tab(3).maxCount(1) @@ -79,7 +79,7 @@ public ActionResult useOnBlock(ItemUsageContext context) { stickStack.mutate(TEXT_KEY, text -> MutableText.of(new BasedTextContent("basednite, ")).append(text)); - context.getPlayer().sendMessage(TEXT_KEY.get(stickStack.getNbt()), false); + context.getPlayer().sendMessage(stickStack.getNbt().get(TEXT_KEY), false); Uwu.BREAK_BLOCK_PARTICLES.spawn(context.getWorld(), Vec3d.of(context.getBlockPos()), null);