Add TagHandler#copy

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2022-04-10 15:49:33 +02:00
parent 1cdc0aeddb
commit 1bb0af7f77
3 changed files with 34 additions and 1 deletions

View File

@ -12,6 +12,8 @@ public interface TagHandler extends TagReadable, TagWritable {
@NotNull TagReadable readableCopy();
@NotNull TagHandler copy();
void updateContent(@NotNull NBTCompoundLike compound);
@NotNull NBTCompound asCompound();

View File

@ -13,9 +13,18 @@ import java.util.Arrays;
import java.util.function.UnaryOperator;
final class TagHandlerImpl implements TagHandler {
private volatile Entry<?>[] entries = new Entry[0];
private volatile Entry<?>[] entries;
private Cache cache;
TagHandlerImpl(Entry<?>[] entries, Cache cache) {
this.entries = entries;
this.cache = cache;
}
TagHandlerImpl() {
this(new Entry[0], null);
}
static TagHandlerImpl fromCompound(NBTCompoundLike compound) {
TagHandlerImpl handler = new TagHandlerImpl();
for (var entry : compound) {
@ -60,6 +69,11 @@ final class TagHandlerImpl implements TagHandler {
return updatedCache();
}
@Override
public synchronized @NotNull TagHandler copy() {
return new TagHandlerImpl(entries.clone(), cache);
}
@Override
public void updateContent(@NotNull NBTCompoundLike compound) {
final TagHandlerImpl converted = fromCompound(compound);

View File

@ -0,0 +1,17 @@
package net.minestom.server.tag;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TagHandlerCopyTest {
@Test
public void copy() {
var handler1 = TagHandler.newHandler();
handler1.setTag(Tag.String("key"), "test");
var handler2 = handler1.copy();
assertEquals(handler1.getTag(Tag.String("key")), handler2.getTag(Tag.String("key")));
}
}