mirror of https://github.com/Minestom/Minestom.git
Add TagHandler#copy
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
1cdc0aeddb
commit
1bb0af7f77
|
@ -12,6 +12,8 @@ public interface TagHandler extends TagReadable, TagWritable {
|
|||
|
||||
@NotNull TagReadable readableCopy();
|
||||
|
||||
@NotNull TagHandler copy();
|
||||
|
||||
void updateContent(@NotNull NBTCompoundLike compound);
|
||||
|
||||
@NotNull NBTCompound asCompound();
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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")));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue