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

219 lines
8.0 KiB
Java
Raw Normal View History

2022-03-20 01:47:57 +01:00
package net.minestom.server.tag;
import net.minestom.server.utils.ArrayUtils;
2022-03-20 01:47:57 +01:00
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnknownNullability;
import org.jglrxavpok.hephaistos.nbt.NBT;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import org.jglrxavpok.hephaistos.nbt.NBTCompoundLike;
import org.jglrxavpok.hephaistos.nbt.mutable.MutableNBTCompound;
import java.util.Arrays;
2022-03-24 09:03:30 +01:00
import java.util.function.UnaryOperator;
2022-03-20 01:47:57 +01:00
final class TagHandlerImpl implements TagHandler {
private volatile Entry<?>[] entries = new Entry[0];
2022-03-20 01:47:57 +01:00
private Cache cache;
@Override
public <T> @UnknownNullability T getTag(@NotNull Tag<T> tag) {
return read(entries, tag);
}
@Override
public synchronized <T> void setTag(@NotNull Tag<T> tag, @Nullable T value) {
2022-03-24 09:03:30 +01:00
// Convert value to fit the tag (e.g. list copies)
if (value != null) {
final UnaryOperator<T> copy = tag.copy;
if (copy != null) value = copy.apply(value);
}
2022-03-24 05:42:01 +01:00
int tagIndex = tag.index;
TagHandlerImpl local = this;
2022-03-20 01:47:57 +01:00
Entry<?>[] entries = this.entries;
final Entry<?>[] localEntries = entries;
2022-03-24 05:42:01 +01:00
final var paths = tag.path;
2022-03-24 05:42:01 +01:00
TagHandlerImpl[] pathHandlers = null;
if (paths != null) {
2022-03-28 15:59:36 +02:00
final int length = paths.length;
pathHandlers = new TagHandlerImpl[length];
for (int i = 0; i < length; i++) {
final Tag.PathEntry path = paths[i];
2022-03-24 05:42:01 +01:00
final int pathIndex = path.index();
if (pathIndex >= entries.length) {
if (value == null) return;
local.entries = entries = Arrays.copyOf(entries, pathIndex + 1);
}
final Entry<?> entry = entries[pathIndex];
2022-03-24 05:42:01 +01:00
if (entry == null) {
if (value == null) return;
// Empty path, create a new handler
local = new TagHandlerImpl();
2022-04-07 11:05:11 +02:00
entries[pathIndex] = new Entry(Tag.tag(path.name(), Serializers.VOID), local);
2022-03-24 05:42:01 +01:00
} else if (entry.value instanceof TagHandlerImpl handler) {
// Existing path, continue navigating
2022-03-24 05:42:01 +01:00
local = handler;
} else throw new IllegalStateException("Cannot set a path-able tag on a non-path-able entry");
2022-03-24 05:42:01 +01:00
entries = local.entries;
2022-03-28 15:59:36 +02:00
pathHandlers[i] = local;
2022-03-24 05:42:01 +01:00
}
// Handle removal if the tag was present (recursively)
if (value == null) {
2022-03-28 15:59:36 +02:00
pathHandlers[length - 1].entries[tagIndex] = null;
2022-03-24 05:42:01 +01:00
boolean empty = false;
2022-03-28 15:59:36 +02:00
for (int i = length - 1; i >= 0; i--) {
2022-03-24 05:42:01 +01:00
TagHandlerImpl handler = pathHandlers[i];
Entry<?>[] entr = handler.entries;
// Verify if the handler is empty
empty = tagIndex >= entr.length || ArrayUtils.isEmpty(entr);
if (empty && i > 0) {
2022-03-24 05:42:01 +01:00
TagHandlerImpl parent = pathHandlers[i - 1];
parent.entries[paths[i].index()] = null;
2022-03-24 05:42:01 +01:00
}
}
if (empty) {
// Remove the root handler
local = this;
entries = localEntries;
tagIndex = paths[0].index();
2022-03-24 05:42:01 +01:00
}
}
}
// Normal tag
if (tagIndex >= entries.length) {
if (value == null) return;
local.entries = entries = Arrays.copyOf(entries, tagIndex + 1);
2022-03-20 01:47:57 +01:00
}
2022-03-24 05:42:01 +01:00
entries[tagIndex] = value != null ? new Entry<>(tag, value) : null;
2022-03-20 01:47:57 +01:00
this.cache = null;
2022-03-24 05:42:01 +01:00
if (pathHandlers != null) {
for (var handler : pathHandlers) handler.cache = null;
}
2022-03-20 01:47:57 +01:00
}
@Override
public @NotNull TagReadable readableCopy() {
return updatedCache();
}
@Override
public void updateContent(@NotNull NBTCompoundLike compound) {
Entry<?>[] entries = new Entry[0];
for (var entry : compound) {
final String key = entry.getKey();
final NBT nbt = entry.getValue();
final Tag<NBT> tag = Tag.NBT(key);
final int index = tag.index;
if (index >= entries.length) {
entries = Arrays.copyOf(entries, index + 1);
}
entries[index] = new Entry<>(tag, nbt);
}
synchronized (this) {
this.cache = null;
this.entries = entries;
}
2022-03-20 01:47:57 +01:00
}
@Override
public @NotNull NBTCompound asCompound() {
return updatedCache().compound;
}
private synchronized Cache updatedCache() {
2022-03-20 01:47:57 +01:00
Cache cache = this.cache;
if (cache == null) {
Entry<?>[] entries = this.entries;
if (entries.length > 0) {
entries = entries.clone();
MutableNBTCompound tmp = new MutableNBTCompound();
for (Entry<?> entry : entries) {
if (entry == null) continue;
final Tag<?> tag = entry.tag;
final Object value = entry.value;
if (value instanceof TagHandler handler) {
// Path-able entry
tmp.put(tag.getKey(), handler.asCompound());
} else {
tag.writeUnsafe(tmp, value);
2022-03-24 05:42:01 +01:00
}
2022-03-20 01:47:57 +01:00
}
cache = !tmp.isEmpty() ? new Cache(entries, tmp.toCompound()) : Cache.EMPTY;
} else {
cache = Cache.EMPTY;
2022-03-20 01:47:57 +01:00
}
this.cache = cache;
2022-03-20 01:47:57 +01:00
}
return cache;
}
private static final class Entry<T> {
final Tag<T> tag;
2022-03-24 05:42:01 +01:00
final T value; // TagHandler type for path-able tags
volatile NBT nbt;
2022-03-20 01:47:57 +01:00
Entry(Tag<T> tag, T value) {
this.tag = tag;
this.value = value;
}
NBT updatedNbt() {
NBT nbt = this.nbt;
2022-04-07 11:05:11 +02:00
if (nbt == null) this.nbt = nbt = tag.entry.write().apply(value);
return nbt;
}
2022-03-20 01:47:57 +01:00
}
private record Cache(Entry<?>[] entries, NBTCompound compound) implements TagReadable {
static final Cache EMPTY = new Cache(new Entry[0], NBTCompound.EMPTY);
@Override
public <T> @UnknownNullability T getTag(@NotNull Tag<T> tag) {
return read(entries, tag);
}
}
private static <T> T read(Entry<?>[] entries, Tag<T> tag) {
final int index = tag.index;
2022-03-24 05:42:01 +01:00
Entry<?> entry;
final var paths = tag.path;
if (paths != null) {
2022-03-24 05:42:01 +01:00
// Must be a path-able entry
for (var path : paths) {
final int pathIndex = path.index();
if (pathIndex >= entries.length || (entry = entries[pathIndex]) == null) {
return tag.createDefault();
}
if (entry.value instanceof TagHandlerImpl handler) {
entries = handler.entries;
} else if (entry.updatedNbt() instanceof NBTCompound compound) {
var tmp = new TagHandlerImpl();
tmp.updateContent(compound);
entries = tmp.entries;
2022-03-24 05:42:01 +01:00
}
}
}
2022-03-20 01:47:57 +01:00
if (index >= entries.length || (entry = entries[index]) == null) {
return tag.createDefault();
}
2022-03-24 05:42:01 +01:00
final Object value = entry.value;
if (value instanceof TagHandlerImpl)
throw new IllegalStateException("Cannot read path-able tag " + tag.getKey());
2022-03-20 01:47:57 +01:00
final Tag entryTag = entry.tag;
if (entryTag.shareValue(tag)) {
2022-03-20 01:47:57 +01:00
// Tag is the same, return the value
//noinspection unchecked
2022-03-24 05:42:01 +01:00
return (T) value;
2022-03-20 01:47:57 +01:00
}
// Value must be parsed from nbt if the tag is different
final NBT nbt = entry.updatedNbt();
2022-03-23 06:51:36 +01:00
try {
2022-04-07 11:05:11 +02:00
return tag.entry.read().apply(nbt);
2022-03-23 06:51:36 +01:00
} catch (ClassCastException e) {
return tag.createDefault();
}
2022-03-20 01:47:57 +01:00
}
}