mirror of
https://github.com/Minestom/Minestom.git
synced 2024-09-27 14:13:24 +02:00
Replace ItemTag
This commit is contained in:
parent
9255adb7ec
commit
354b4e3bc3
@ -4,6 +4,8 @@ import io.netty.buffer.ByteBuf;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.minestom.server.instance.block.Block;
|
||||
import net.minestom.server.item.attribute.ItemAttribute;
|
||||
import net.minestom.server.tag.Tag;
|
||||
import net.minestom.server.tag.TagReader;
|
||||
import net.minestom.server.utils.binary.BinaryWriter;
|
||||
import net.minestom.server.utils.binary.Writeable;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
@ -14,7 +16,7 @@ import org.jglrxavpok.hephaistos.nbt.NBTCompound;
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class ItemMeta implements Writeable {
|
||||
public class ItemMeta implements TagReader, Writeable {
|
||||
|
||||
private final int damage;
|
||||
private final boolean unbreakable;
|
||||
@ -110,7 +112,7 @@ public class ItemMeta implements Writeable {
|
||||
}
|
||||
|
||||
@Contract(pure = true)
|
||||
public <T> T getOrDefault(@NotNull ItemTag<T> tag, @Nullable T defaultValue) {
|
||||
public <T> T getOrDefault(@NotNull Tag<T> tag, @Nullable T defaultValue) {
|
||||
var key = tag.getKey();
|
||||
if (nbt.containsKey(key)) {
|
||||
return tag.read(toNBT());
|
||||
@ -119,8 +121,18 @@ public class ItemMeta implements Writeable {
|
||||
}
|
||||
}
|
||||
|
||||
public <T> @Nullable T get(@NotNull ItemTag<T> tag) {
|
||||
return tag.read(toNBT());
|
||||
@Override
|
||||
public <T> @Nullable T getTag(@NotNull Tag<T> tag) {
|
||||
return tag.read(nbt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasTag(@NotNull Tag<?> tag) {
|
||||
return nbt.containsKey(tag.getKey());
|
||||
}
|
||||
|
||||
public <T> @Nullable T get(@NotNull Tag<T> tag) {
|
||||
return getTag(tag);
|
||||
}
|
||||
|
||||
public @NotNull NBTCompound toNBT() {
|
||||
|
@ -5,6 +5,8 @@ import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
|
||||
import net.minestom.server.adventure.AdventureSerializer;
|
||||
import net.minestom.server.instance.block.Block;
|
||||
import net.minestom.server.item.attribute.ItemAttribute;
|
||||
import net.minestom.server.tag.Tag;
|
||||
import net.minestom.server.tag.TagWriter;
|
||||
import net.minestom.server.utils.NBTUtils;
|
||||
import net.minestom.server.utils.Utils;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
@ -16,7 +18,7 @@ import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public abstract class ItemMetaBuilder {
|
||||
public abstract class ItemMetaBuilder implements TagWriter {
|
||||
|
||||
protected NBTCompound nbt = new NBTCompound();
|
||||
|
||||
@ -183,12 +185,13 @@ public abstract class ItemMetaBuilder {
|
||||
return canDestroy(Set.of(blocks));
|
||||
}
|
||||
|
||||
public <T> @NotNull ItemMetaBuilder set(@NotNull ItemTag<T> tag, @Nullable T value) {
|
||||
if (value != null) {
|
||||
tag.write(nbt, value);
|
||||
} else {
|
||||
this.nbt.removeTag(tag.getKey());
|
||||
}
|
||||
@Override
|
||||
public <T> void setTag(@NotNull Tag<T> tag, @Nullable T value) {
|
||||
tag.write(nbt, value);
|
||||
}
|
||||
|
||||
public <T> @NotNull ItemMetaBuilder set(@NotNull Tag<T> tag, @Nullable T value) {
|
||||
setTag(tag, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -1,112 +1,65 @@
|
||||
package net.minestom.server.item;
|
||||
|
||||
import net.minestom.server.tag.Tag;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBT;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBTList;
|
||||
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class ItemTag<T> {
|
||||
/**
|
||||
* @deprecated use {@link Tag}.
|
||||
*/
|
||||
@Deprecated
|
||||
public class ItemTag<T> extends Tag<T> {
|
||||
|
||||
private final String key;
|
||||
private final Function<NBTCompound, T> readFunction;
|
||||
private final BiConsumer<NBTCompound, T> writeConsumer;
|
||||
|
||||
private ItemTag(@NotNull String key,
|
||||
@NotNull Function<NBTCompound, T> readFunction,
|
||||
@NotNull BiConsumer<NBTCompound, T> writeConsumer) {
|
||||
this.key = key;
|
||||
this.readFunction = readFunction;
|
||||
this.writeConsumer = writeConsumer;
|
||||
protected ItemTag(@NotNull String key, @NotNull Function<NBTCompound, T> readFunction, @NotNull BiConsumer<NBTCompound, T> writeConsumer) {
|
||||
super(key, readFunction, writeConsumer);
|
||||
}
|
||||
|
||||
public @NotNull String getKey() {
|
||||
return key;
|
||||
public static @NotNull Tag<Byte> Byte(@NotNull String key) {
|
||||
return Tag.Byte(key);
|
||||
}
|
||||
|
||||
protected T read(@NotNull NBTCompound nbtCompound) {
|
||||
return readFunction.apply(nbtCompound);
|
||||
public static @NotNull Tag<Short> Short(@NotNull String key) {
|
||||
return Tag.Short(key);
|
||||
}
|
||||
|
||||
protected void write(@NotNull NBTCompound nbtCompound, @NotNull T value) {
|
||||
this.writeConsumer.accept(nbtCompound, value);
|
||||
public static @NotNull Tag<Integer> Integer(@NotNull String key) {
|
||||
return Tag.Integer(key);
|
||||
}
|
||||
|
||||
public static @NotNull ItemTag<Byte> Byte(@NotNull String key) {
|
||||
return new ItemTag<>(key,
|
||||
nbtCompound -> nbtCompound.getByte(key),
|
||||
(nbtCompound, value) -> nbtCompound.setByte(key, value));
|
||||
public static @NotNull Tag<Long> Long(@NotNull String key) {
|
||||
return Tag.Long(key);
|
||||
}
|
||||
|
||||
public static @NotNull ItemTag<Short> Short(@NotNull String key) {
|
||||
return new ItemTag<>(key,
|
||||
nbtCompound -> nbtCompound.getShort(key),
|
||||
(nbtCompound, value) -> nbtCompound.setShort(key, value));
|
||||
public static @NotNull Tag<Float> Float(@NotNull String key) {
|
||||
return Tag.Float(key);
|
||||
}
|
||||
|
||||
public static @NotNull ItemTag<Integer> Integer(@NotNull String key) {
|
||||
return new ItemTag<>(key,
|
||||
nbtCompound -> nbtCompound.getInt(key),
|
||||
(nbtCompound, integer) -> nbtCompound.setInt(key, integer));
|
||||
public static @NotNull Tag<Double> Double(@NotNull String key) {
|
||||
return Tag.Double(key);
|
||||
}
|
||||
|
||||
public static @NotNull ItemTag<Long> Long(@NotNull String key) {
|
||||
return new ItemTag<>(key,
|
||||
nbtCompound -> nbtCompound.getLong(key),
|
||||
(nbtCompound, value) -> nbtCompound.setLong(key, value));
|
||||
public static @NotNull Tag<byte[]> ByteArray(@NotNull String key) {
|
||||
return Tag.ByteArray(key);
|
||||
}
|
||||
|
||||
public static @NotNull ItemTag<Float> Float(@NotNull String key) {
|
||||
return new ItemTag<>(key,
|
||||
nbtCompound -> nbtCompound.getFloat(key),
|
||||
(nbtCompound, value) -> nbtCompound.setFloat(key, value));
|
||||
public static @NotNull Tag<String> String(@NotNull String key) {
|
||||
return Tag.String(key);
|
||||
}
|
||||
|
||||
public static @NotNull ItemTag<Double> Double(@NotNull String key) {
|
||||
return new ItemTag<>(key,
|
||||
nbtCompound -> nbtCompound.getDouble(key),
|
||||
(nbtCompound, value) -> nbtCompound.setDouble(key, value));
|
||||
public static @NotNull Tag<NBT> NBT(@NotNull String key) {
|
||||
return Tag.NBT(key);
|
||||
}
|
||||
|
||||
public static @NotNull ItemTag<byte[]> ByteArray(@NotNull String key) {
|
||||
return new ItemTag<>(key,
|
||||
nbtCompound -> nbtCompound.getByteArray(key),
|
||||
(nbtCompound, value) -> nbtCompound.setByteArray(key, value));
|
||||
public static @NotNull Tag<int[]> IntArray(@NotNull String key) {
|
||||
return Tag.IntArray(key);
|
||||
}
|
||||
|
||||
public static @NotNull ItemTag<String> String(@NotNull String key) {
|
||||
return new ItemTag<>(key,
|
||||
nbtCompound -> nbtCompound.getString(key),
|
||||
(nbtCompound, value) -> nbtCompound.setString(key, value));
|
||||
}
|
||||
|
||||
public static @NotNull ItemTag<NBT> NBT(@NotNull String key) {
|
||||
return new ItemTag<>(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 ItemTag<int[]> IntArray(@NotNull String key) {
|
||||
return new ItemTag<>(key,
|
||||
nbtCompound -> nbtCompound.getIntArray(key),
|
||||
(nbtCompound, value) -> nbtCompound.setIntArray(key, value));
|
||||
}
|
||||
|
||||
public static @NotNull ItemTag<long[]> LongArray(@NotNull String key) {
|
||||
return new ItemTag<>(key,
|
||||
nbtCompound -> nbtCompound.getLongArray(key),
|
||||
(nbtCompound, value) -> nbtCompound.setLongArray(key, value));
|
||||
public static @NotNull Tag<long[]> LongArray(@NotNull String key) {
|
||||
return Tag.LongArray(key);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package net.minestom.server.tag;
|
||||
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBT;
|
||||
@ -16,14 +17,15 @@ import java.util.function.Supplier;
|
||||
*
|
||||
* @param <T> the tag type
|
||||
*/
|
||||
public final class Tag<T> {
|
||||
@ApiStatus.NonExtendable
|
||||
public class Tag<T> {
|
||||
|
||||
private final String key;
|
||||
private final Function<NBTCompound, T> readFunction;
|
||||
private final BiConsumer<NBTCompound, T> writeConsumer;
|
||||
private volatile Supplier<T> defaultValue;
|
||||
|
||||
private Tag(@NotNull String key,
|
||||
protected Tag(@NotNull String key,
|
||||
@NotNull Function<NBTCompound, T> readFunction,
|
||||
@NotNull BiConsumer<NBTCompound, T> writeConsumer) {
|
||||
this.key = key;
|
||||
|
Loading…
Reference in New Issue
Block a user