#1299: Add new PersistentDataContainer methods and clean up docs
This commit is contained in:
parent
b483a20db0
commit
4fea66e448
@ -54,6 +54,11 @@ public class CraftPersistentDataContainer implements PersistentDataContainer {
|
|||||||
return registry.isInstanceOf(type.getPrimitiveType(), value);
|
return registry.isInstanceOf(type.getPrimitiveType(), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean has(NamespacedKey key) {
|
||||||
|
return this.customDataTags.get(key.toString()) != null;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T, Z> Z get(NamespacedKey key, PersistentDataType<T, Z> type) {
|
public <T, Z> Z get(NamespacedKey key, PersistentDataType<T, Z> type) {
|
||||||
Preconditions.checkArgument(key != null, "The NamespacedKey key cannot be null");
|
Preconditions.checkArgument(key != null, "The NamespacedKey key cannot be null");
|
||||||
@ -99,6 +104,18 @@ public class CraftPersistentDataContainer implements PersistentDataContainer {
|
|||||||
return this.customDataTags.isEmpty();
|
return this.customDataTags.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void copyTo(PersistentDataContainer other, boolean replace) {
|
||||||
|
Preconditions.checkArgument(other != null, "The target container cannot be null");
|
||||||
|
|
||||||
|
CraftPersistentDataContainer target = (CraftPersistentDataContainer) other;
|
||||||
|
if (replace) {
|
||||||
|
target.customDataTags.putAll(customDataTags);
|
||||||
|
} else {
|
||||||
|
customDataTags.forEach(target.customDataTags::putIfAbsent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PersistentDataAdapterContext getAdapterContext() {
|
public PersistentDataAdapterContext getAdapterContext() {
|
||||||
return this.adapterContext;
|
return this.adapterContext;
|
||||||
|
@ -50,6 +50,13 @@ public class PersistentDataContainerTest extends AbstractTestingBase {
|
|||||||
assertThrows(IllegalArgumentException.class, () -> itemMeta.getPersistentDataContainer().has(VALID_KEY, new PrimitiveTagType<>(boolean.class)));
|
assertThrows(IllegalArgumentException.class, () -> itemMeta.getPersistentDataContainer().has(VALID_KEY, new PrimitiveTagType<>(boolean.class)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testHasNoType() {
|
||||||
|
ItemMeta itemMeta = createNewItemMeta();
|
||||||
|
itemMeta.getPersistentDataContainer().set(VALID_KEY, PersistentDataType.INTEGER, 1); // We gotta set this so we at least try to compare it
|
||||||
|
assertTrue(itemMeta.getPersistentDataContainer().has(VALID_KEY));
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Getting a tag
|
Getting a tag
|
||||||
*/
|
*/
|
||||||
@ -88,6 +95,28 @@ public class PersistentDataContainerTest extends AbstractTestingBase {
|
|||||||
return new NamespacedKey("test-plugin", keyName.toLowerCase());
|
return new NamespacedKey("test-plugin", keyName.toLowerCase());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCopyTo() {
|
||||||
|
PersistentDataContainer container = createComplexItemMeta().getPersistentDataContainer();
|
||||||
|
PersistentDataContainer target = container.getAdapterContext().newPersistentDataContainer();
|
||||||
|
target.set(VALID_KEY, PersistentDataType.INTEGER, 1);
|
||||||
|
container.set(VALID_KEY, PersistentDataType.INTEGER, 2);
|
||||||
|
container.copyTo(target, false);
|
||||||
|
|
||||||
|
assertEquals(1, target.get(VALID_KEY, PersistentDataType.INTEGER)); // Should not be replaced
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCopyToReplace() {
|
||||||
|
PersistentDataContainer container = createComplexItemMeta().getPersistentDataContainer();
|
||||||
|
PersistentDataContainer target = container.getAdapterContext().newPersistentDataContainer();
|
||||||
|
target.set(VALID_KEY, PersistentDataType.INTEGER, 1);
|
||||||
|
container.set(VALID_KEY, PersistentDataType.INTEGER, 2);
|
||||||
|
container.copyTo(target, true);
|
||||||
|
|
||||||
|
assertEquals(container, target);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Removing a tag
|
Removing a tag
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user