Make a PDC view accessible directly from ItemStack
This commit is contained in:
@@ -496,4 +496,34 @@ public final class CraftItemStack extends ItemStack {
|
||||
return mirrored;
|
||||
}
|
||||
// Paper end
|
||||
|
||||
// Paper start - pdc
|
||||
private net.minecraft.nbt.CompoundTag getPdcTag() {
|
||||
if (this.handle == null) {
|
||||
return new net.minecraft.nbt.CompoundTag();
|
||||
}
|
||||
final net.minecraft.world.item.component.CustomData customData = this.handle.getOrDefault(DataComponents.CUSTOM_DATA, net.minecraft.world.item.component.CustomData.EMPTY);
|
||||
// getUnsafe is OK here because we are only ever *reading* the data so immutability is preserved
|
||||
//noinspection deprecation
|
||||
return customData.getUnsafe().getCompound("PublicBukkitValues");
|
||||
}
|
||||
|
||||
private static final org.bukkit.craftbukkit.persistence.CraftPersistentDataTypeRegistry REGISTRY = new org.bukkit.craftbukkit.persistence.CraftPersistentDataTypeRegistry();
|
||||
private final io.papermc.paper.persistence.PaperPersistentDataContainerView pdcView = new io.papermc.paper.persistence.PaperPersistentDataContainerView(REGISTRY) {
|
||||
|
||||
@Override
|
||||
public net.minecraft.nbt.CompoundTag toTagCompound() {
|
||||
return CraftItemStack.this.getPdcTag();
|
||||
}
|
||||
|
||||
@Override
|
||||
public net.minecraft.nbt.Tag getTag(final String key) {
|
||||
return CraftItemStack.this.getPdcTag().get(key);
|
||||
}
|
||||
};
|
||||
@Override
|
||||
public io.papermc.paper.persistence.PersistentDataContainerView getPersistentDataContainer() {
|
||||
return this.pdcView;
|
||||
}
|
||||
// Paper end - pdc
|
||||
}
|
||||
|
||||
@@ -16,11 +16,10 @@ import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class CraftPersistentDataContainer implements PersistentDataContainer {
|
||||
public class CraftPersistentDataContainer extends io.papermc.paper.persistence.PaperPersistentDataContainerView implements PersistentDataContainer { // Paper - split up view and mutable
|
||||
|
||||
private final Map<String, Tag> customDataTags = new HashMap<>();
|
||||
private final CraftPersistentDataTypeRegistry registry;
|
||||
private final CraftPersistentDataAdapterContext adapterContext;
|
||||
// Paper - move to PersistentDataContainerView
|
||||
|
||||
public CraftPersistentDataContainer(Map<String, Tag> customTags, CraftPersistentDataTypeRegistry registry) {
|
||||
this(registry);
|
||||
@@ -28,10 +27,15 @@ public class CraftPersistentDataContainer implements PersistentDataContainer {
|
||||
}
|
||||
|
||||
public CraftPersistentDataContainer(CraftPersistentDataTypeRegistry registry) {
|
||||
this.registry = registry;
|
||||
this.adapterContext = new CraftPersistentDataAdapterContext(this.registry);
|
||||
super(registry); // Paper - move to PersistentDataContainerView
|
||||
}
|
||||
|
||||
// Paper start
|
||||
@Override
|
||||
public Tag getTag(final String key) {
|
||||
return this.customDataTags.get(key);
|
||||
}
|
||||
// Paper end
|
||||
|
||||
@Override
|
||||
public <T, Z> void set(@NotNull NamespacedKey key, @NotNull PersistentDataType<T, Z> type, @NotNull Z value) {
|
||||
@@ -42,44 +46,7 @@ public class CraftPersistentDataContainer implements PersistentDataContainer {
|
||||
this.customDataTags.put(key.toString(), this.registry.wrap(type, type.toPrimitive(value, this.adapterContext)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T, Z> boolean has(@NotNull NamespacedKey key, @NotNull PersistentDataType<T, Z> type) {
|
||||
Preconditions.checkArgument(key != null, "The NamespacedKey key cannot be null");
|
||||
Preconditions.checkArgument(type != null, "The provided type cannot be null");
|
||||
|
||||
Tag value = this.customDataTags.get(key.toString());
|
||||
if (value == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return this.registry.isInstanceOf(type, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean has(NamespacedKey key) {
|
||||
Preconditions.checkArgument(key != null, "The provided key for the custom value was null"); // Paper
|
||||
return this.customDataTags.get(key.toString()) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T, Z> Z get(@NotNull NamespacedKey key, @NotNull PersistentDataType<T, Z> type) {
|
||||
Preconditions.checkArgument(key != null, "The NamespacedKey key cannot be null");
|
||||
Preconditions.checkArgument(type != null, "The provided type cannot be null");
|
||||
|
||||
Tag value = this.customDataTags.get(key.toString());
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return type.fromPrimitive(this.registry.extract(type, value), this.adapterContext);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public <T, Z> Z getOrDefault(@NotNull NamespacedKey key, @NotNull PersistentDataType<T, Z> type, @NotNull Z defaultValue) {
|
||||
Z z = this.get(key, type);
|
||||
return z != null ? z : defaultValue;
|
||||
}
|
||||
// Paper - move to PersistentDataContainerView
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
@@ -186,16 +153,7 @@ public class CraftPersistentDataContainer implements PersistentDataContainer {
|
||||
// Paper end
|
||||
|
||||
// Paper start - byte array serialization
|
||||
@Override
|
||||
public byte[] serializeToBytes() throws java.io.IOException {
|
||||
final net.minecraft.nbt.CompoundTag root = this.toTagCompound();
|
||||
final java.io.ByteArrayOutputStream byteArrayOutput = new java.io.ByteArrayOutputStream();
|
||||
try (final java.io.DataOutputStream dataOutput = new java.io.DataOutputStream(byteArrayOutput)) {
|
||||
net.minecraft.nbt.NbtIo.write(root, dataOutput);
|
||||
return byteArrayOutput.toByteArray();
|
||||
}
|
||||
}
|
||||
|
||||
// Paper - move to PersistentDataContainerView
|
||||
@Override
|
||||
public void readFromBytes(final byte[] bytes, final boolean clear) throws java.io.IOException {
|
||||
if (clear) {
|
||||
|
||||
Reference in New Issue
Block a user