Minestom/src/main/java/net/minestom/server/tag/Tag.java

256 lines
9.2 KiB
Java
Raw Normal View History

2021-05-17 12:34:45 +02:00
package net.minestom.server.tag;
2022-03-20 01:47:57 +01:00
import net.minestom.server.item.ItemStack;
2022-03-23 08:49:40 +01:00
import net.minestom.server.utils.collection.IndexMap;
2021-05-17 17:46:56 +02:00
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
2021-05-17 12:34:45 +02:00
import org.jetbrains.annotations.NotNull;
2021-05-17 12:44:22 +02:00
import org.jetbrains.annotations.Nullable;
2022-03-20 01:47:57 +01:00
import org.jglrxavpok.hephaistos.nbt.*;
2021-12-13 16:41:30 +01:00
import org.jglrxavpok.hephaistos.nbt.mutable.MutableNBTCompound;
2021-05-17 12:34:45 +02:00
2022-03-24 05:42:01 +01:00
import java.util.Arrays;
import java.util.List;
2021-05-17 12:34:45 +02:00
import java.util.function.Function;
2021-05-17 12:44:22 +02:00
import java.util.function.Supplier;
2022-03-24 09:03:30 +01:00
import java.util.function.UnaryOperator;
2021-05-17 12:34:45 +02:00
2021-05-17 15:14:16 +02:00
/**
* Represents a key to retrieve or change a value.
* <p>
* All tags are serializable.
*
* @param <T> the tag type
*/
2021-05-17 17:46:56 +02:00
@ApiStatus.NonExtendable
public class Tag<T> {
2022-03-23 08:49:40 +01:00
private static final IndexMap<String> INDEX_MAP = new IndexMap<>();
2021-06-22 02:51:04 +02:00
2022-03-24 05:42:01 +01:00
record PathEntry(String name, int index) {
}
2022-03-24 09:03:30 +01:00
final int index;
2021-05-17 12:34:45 +02:00
private final String key;
2022-03-23 06:51:36 +01:00
final Function<NBT, T> readFunction;
final Function<T, NBT> writeFunction;
private final Supplier<T> defaultValue;
2021-05-17 12:44:22 +02:00
2022-03-24 05:42:01 +01:00
final List<PathEntry> path;
2022-03-24 09:03:30 +01:00
final UnaryOperator<T> copy;
2022-03-24 05:42:01 +01:00
2022-03-24 09:03:30 +01:00
Tag(int index, String key,
Function<NBT, T> readFunction,
Function<T, NBT> writeFunction,
@Nullable Supplier<T> defaultValue, @Nullable List<PathEntry> path,
@Nullable UnaryOperator<T> copy) {
assert index == INDEX_MAP.get(key);
this.index = index;
2021-05-17 12:34:45 +02:00
this.key = key;
this.readFunction = readFunction;
2022-03-20 01:47:57 +01:00
this.writeFunction = writeFunction;
this.defaultValue = defaultValue;
2022-03-24 05:42:01 +01:00
this.path = path;
2022-03-24 09:03:30 +01:00
this.copy = copy;
}
2022-03-20 01:47:57 +01:00
static <T, N extends NBT> Tag<T> tag(@NotNull String key,
@NotNull Function<N, T> readFunction,
@NotNull Function<T, N> writeFunction) {
2022-03-24 09:03:30 +01:00
return new Tag<>(INDEX_MAP.get(key), key, (Function<NBT, T>) readFunction, (Function<T, NBT>) writeFunction,
null, null, null);
2021-05-17 12:34:45 +02:00
}
2021-06-26 05:08:33 +02:00
/**
* Returns the key used to navigate inside the holder nbt.
*
* @return the tag key
*/
2022-03-20 01:47:57 +01:00
public @NotNull String getKey() {
2021-05-17 12:34:45 +02:00
return key;
}
@Contract(value = "_ -> new", pure = true)
2021-05-17 12:44:22 +02:00
public Tag<T> defaultValue(@NotNull Supplier<T> defaultValue) {
2022-03-24 09:03:30 +01:00
return new Tag<>(index, key, readFunction, writeFunction, defaultValue, path, copy);
2021-05-17 12:44:22 +02:00
}
@Contract(value = "_ -> new", pure = true)
2021-05-17 12:44:22 +02:00
public Tag<T> defaultValue(@NotNull T defaultValue) {
2021-05-28 16:21:54 +02:00
return defaultValue(() -> defaultValue);
}
@Contract(value = "_, _ -> new", pure = true)
public <R> Tag<R> map(@NotNull Function<T, R> readMap,
@NotNull Function<R, T> writeMap) {
2022-03-24 09:03:30 +01:00
return new Tag<>(index, key,
// Read
2022-03-20 01:47:57 +01:00
readFunction.andThen(t -> {
if (t == null) return null;
return readMap.apply(t);
}),
// Write
2022-03-20 01:47:57 +01:00
writeMap.andThen(writeFunction),
// Default value
2022-03-24 05:42:01 +01:00
() -> readMap.apply(createDefault()),
2022-03-24 09:03:30 +01:00
path, null);
}
@ApiStatus.Experimental
@Contract(value = "-> new", pure = true)
public Tag<List<T>> list() {
return new Tag<>(index, key,
read -> {
var list = (NBTList<?>) read;
final int size = list.getSize();
if (size == 0)
return List.of();
T[] array = (T[]) new Object[size];
for (int i = 0; i < size; i++) {
array[i] = readFunction.apply(list.get(i));
}
return List.of(array);
},
write -> {
final int size = write.size();
if (size == 0)
return new NBTList<>(NBTType.TAG_String); // String is the default type for lists
NBTType<NBT> type = null;
NBT[] array = new NBT[size];
for (int i = 0; i < size; i++) {
final NBT nbt = writeFunction.apply(write.get(i));
if (type == null) {
type = (NBTType<NBT>) nbt.getID();
} else if (type != nbt.getID()) {
throw new IllegalArgumentException("All elements of the list must have the same type");
}
array[i] = nbt;
}
return NBT.List(type, List.of(array));
}, null, path,
copy != null ? ts -> {
2022-03-25 08:23:33 +01:00
final int size = ts.size();
T[] array = (T[]) new Object[size];
2022-03-25 08:18:30 +01:00
boolean shallowCopy = true;
2022-03-25 08:23:33 +01:00
for (int i = 0; i < size; i++) {
final T t = ts.get(i);
final T copy = this.copy.apply(t);
if (shallowCopy && copy != t) shallowCopy = false;
array[i] = copy;
2022-03-24 09:03:30 +01:00
}
2022-03-25 08:18:30 +01:00
return shallowCopy ? List.copyOf(ts) : List.of(array);
2022-03-24 09:03:30 +01:00
} : List::copyOf);
2022-03-24 05:42:01 +01:00
}
@ApiStatus.Experimental
@Contract(value = "_ -> new", pure = true)
public Tag<T> path(@NotNull String @Nullable ... path) {
final List<PathEntry> entries = path != null ? Arrays.stream(path).map(s -> new PathEntry(s, INDEX_MAP.get(s))).toList() : null;
2022-03-24 09:03:30 +01:00
return new Tag<>(index, key, readFunction, writeFunction, defaultValue, entries, copy);
2021-05-17 12:44:22 +02:00
}
2022-03-20 01:47:57 +01:00
public @Nullable T read(@NotNull NBTCompoundLike nbt) {
final String key = this.key;
2022-03-23 06:51:36 +01:00
final NBT readable = key.isEmpty() ? nbt.toCompound() : nbt.get(key);
final T result;
try {
if (readable == null || (result = readFunction.apply(readable)) == null)
return createDefault();
return result;
} catch (ClassCastException e) {
return createDefault();
2021-05-17 13:04:00 +02:00
}
2022-03-20 01:47:57 +01:00
}
T createDefault() {
final var supplier = defaultValue;
return supplier != null ? supplier.get() : null;
2021-05-17 12:34:45 +02:00
}
2021-12-13 16:41:30 +01:00
public void write(@NotNull MutableNBTCompound nbtCompound, @Nullable T value) {
2022-03-20 01:47:57 +01:00
final String key = this.key;
if (value != null) {
final NBT nbt = writeFunction.apply(value);
if (key.isEmpty()) nbtCompound.copyFrom((NBTCompoundLike) nbt);
else nbtCompound.set(key, nbt);
2021-05-17 13:04:00 +02:00
} else {
2022-03-20 01:47:57 +01:00
if (key.isEmpty()) nbtCompound.clear();
else nbtCompound.remove(key);
2021-05-17 13:04:00 +02:00
}
2021-05-17 12:34:45 +02:00
}
2021-12-13 16:41:30 +01:00
public void writeUnsafe(@NotNull MutableNBTCompound nbtCompound, @Nullable Object value) {
//noinspection unchecked
2021-06-22 02:51:04 +02:00
write(nbtCompound, (T) value);
}
final boolean shareValue(@NotNull Tag<?> other) {
// Verify if these 2 tags can share the same cached value
// Key/Default value/Path are ignored
return this == other || this.readFunction == other.readFunction;
}
2021-05-17 12:34:45 +02:00
public static @NotNull Tag<Byte> Byte(@NotNull String key) {
2022-03-23 07:04:05 +01:00
return tag(key, NBTByte::getValue, NBT::Byte);
2021-05-17 12:34:45 +02:00
}
public static @NotNull Tag<Short> Short(@NotNull String key) {
2022-03-23 07:04:05 +01:00
return tag(key, NBTShort::getValue, NBT::Short);
2021-05-17 12:34:45 +02:00
}
public static @NotNull Tag<Integer> Integer(@NotNull String key) {
2022-03-23 07:04:05 +01:00
return tag(key, NBTInt::getValue, NBT::Int);
2021-05-17 12:34:45 +02:00
}
public static @NotNull Tag<Long> Long(@NotNull String key) {
2022-03-23 07:04:05 +01:00
return tag(key, NBTLong::getValue, NBT::Long);
2021-05-17 12:34:45 +02:00
}
public static @NotNull Tag<Float> Float(@NotNull String key) {
2022-03-23 07:04:05 +01:00
return tag(key, NBTFloat::getValue, NBT::Float);
2021-05-17 12:34:45 +02:00
}
public static @NotNull Tag<Double> Double(@NotNull String key) {
2022-03-23 07:04:05 +01:00
return tag(key, NBTDouble::getValue, NBT::Double);
2021-05-17 12:34:45 +02:00
}
public static @NotNull Tag<String> String(@NotNull String key) {
2022-03-23 07:04:05 +01:00
return tag(key, NBTString::getValue, NBT::String);
2021-05-17 12:34:45 +02:00
}
2021-06-22 02:51:04 +02:00
public static <T extends NBT> @NotNull Tag<T> NBT(@NotNull String key) {
2022-03-23 07:04:05 +01:00
return Tag.<T, T>tag(key, nbt -> nbt, t -> t);
2021-05-17 12:34:45 +02:00
}
2021-06-22 02:51:04 +02:00
/**
* Create a wrapper around a compound.
*
* @param key the tag key
* @param serializer the tag serializer
* @param <T> the tag type
* @return the created tag
*/
public static <T> @NotNull Tag<T> Structure(@NotNull String key, @NotNull TagSerializer<T> serializer) {
2022-03-23 07:04:05 +01:00
return tag(key,
(NBTCompound compound) -> serializer.read(TagHandler.fromCompound(compound)),
2022-03-20 01:47:57 +01:00
(value) -> {
TagHandler handler = TagHandler.newHandler();
serializer.write(handler, value);
return handler.asCompound();
});
2021-05-17 14:02:14 +02:00
}
2021-06-22 02:51:04 +02:00
public static <T> @NotNull Tag<T> View(@NotNull TagSerializer<T> serializer) {
2022-03-23 07:04:05 +01:00
return tag("",
(NBTCompound compound) -> serializer.read(TagHandler.fromCompound(compound)),
2022-03-20 01:47:57 +01:00
(value) -> {
TagHandler handler = TagHandler.newHandler();
serializer.write(handler, value);
return handler.asCompound();
});
}
2022-03-20 01:47:57 +01:00
public static @NotNull Tag<ItemStack> ItemStack(@NotNull String key) {
2022-03-23 07:04:05 +01:00
return tag(key, ItemStack::fromItemNBT, ItemStack::toItemNBT);
2021-06-22 03:05:22 +02:00
}
2021-05-17 12:34:45 +02:00
}