mirror of
https://github.com/PaperMC/Paper.git
synced 2025-08-06 15:12:13 -07:00
Add offline PDC API (#8117)
This commit is contained in:
@@ -16,19 +16,23 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
||||
+import java.io.ByteArrayOutputStream;
|
||||
+import java.io.DataOutputStream;
|
||||
+import java.io.IOException;
|
||||
+import java.util.Collections;
|
||||
+import java.util.HashSet;
|
||||
+import java.util.Set;
|
||||
+import net.minecraft.nbt.CompoundTag;
|
||||
+import net.minecraft.nbt.NbtIo;
|
||||
+import net.minecraft.nbt.Tag;
|
||||
+import org.bukkit.NamespacedKey;
|
||||
+import org.bukkit.craftbukkit.persistence.CraftPersistentDataAdapterContext;
|
||||
+import org.bukkit.craftbukkit.persistence.CraftPersistentDataContainer;
|
||||
+import org.bukkit.craftbukkit.persistence.CraftPersistentDataTypeRegistry;
|
||||
+import org.bukkit.persistence.PersistentDataAdapterContext;
|
||||
+import org.bukkit.persistence.PersistentDataContainer;
|
||||
+import org.bukkit.persistence.PersistentDataType;
|
||||
+import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
+import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
+import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
+import org.jspecify.annotations.NullMarked;
|
||||
+import org.jspecify.annotations.Nullable;
|
||||
+
|
||||
+@DefaultQualifier(NonNull.class)
|
||||
+@NullMarked
|
||||
+public abstract class PaperPersistentDataContainerView implements PersistentDataContainerView {
|
||||
+
|
||||
+ protected final CraftPersistentDataTypeRegistry registry;
|
||||
@@ -48,7 +52,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
||||
+ Preconditions.checkArgument(key != null, "The NamespacedKey key cannot be null");
|
||||
+ Preconditions.checkArgument(type != null, "The provided type cannot be null");
|
||||
+
|
||||
+ final @Nullable Tag value = this.getTag(key.toString());
|
||||
+ final Tag value = this.getTag(key.toString());
|
||||
+ if (value == null) {
|
||||
+ return false;
|
||||
+ }
|
||||
@@ -67,7 +71,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
||||
+ Preconditions.checkArgument(key != null, "The NamespacedKey key cannot be null");
|
||||
+ Preconditions.checkArgument(type != null, "The provided type cannot be null");
|
||||
+
|
||||
+ final @Nullable Tag value = this.getTag(key.toString());
|
||||
+ final Tag value = this.getTag(key.toString());
|
||||
+ if (value == null) {
|
||||
+ return null;
|
||||
+ }
|
||||
@@ -82,13 +86,43 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public Set<NamespacedKey> getKeys() {
|
||||
+ final Set<String> names = this.toTagCompound().getAllKeys();
|
||||
+ final Set<NamespacedKey> keys = new HashSet<>(names.size());
|
||||
+ names.forEach(key -> {
|
||||
+ final String[] keyPart = key.split(":", 2);
|
||||
+ if (keyPart.length == 2) {
|
||||
+ keys.add(new NamespacedKey(keyPart[0], keyPart[1]));
|
||||
+ }
|
||||
+ });
|
||||
+ return Collections.unmodifiableSet(keys);
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public boolean isEmpty() {
|
||||
+ return this.toTagCompound().isEmpty();
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public void copyTo(final PersistentDataContainer other, final boolean replace) {
|
||||
+ Preconditions.checkArgument(other != null, "The target container cannot be null");
|
||||
+ final CraftPersistentDataContainer target = (CraftPersistentDataContainer) other;
|
||||
+ final CompoundTag tag = this.toTagCompound();
|
||||
+ for (final String key : tag.getAllKeys()) {
|
||||
+ if (replace || !target.getRaw().containsKey(key)) {
|
||||
+ target.getRaw().put(key, tag.get(key).copy());
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public PersistentDataAdapterContext getAdapterContext() {
|
||||
+ return this.adapterContext;
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public byte[] serializeToBytes() throws IOException {
|
||||
+ final net.minecraft.nbt.CompoundTag root = this.toTagCompound();
|
||||
+ final CompoundTag root = this.toTagCompound();
|
||||
+ final ByteArrayOutputStream byteArrayOutput = new ByteArrayOutputStream();
|
||||
+ try (final DataOutputStream dataOutput = new DataOutputStream(byteArrayOutput)) {
|
||||
+ NbtIo.write(root, dataOutput);
|
||||
@@ -128,35 +162,6 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
||||
+ public net.minecraft.nbt.Tag getTag(final String key) {
|
||||
+ return CraftItemStack.this.getPdcTag().get(key);
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public java.util.Set<org.bukkit.NamespacedKey> getKeys() {
|
||||
+ java.util.Set<org.bukkit.NamespacedKey> keys = new java.util.HashSet<>();
|
||||
+ CraftItemStack.this.getPdcTag().getAllKeys().forEach(key -> {
|
||||
+ final String[] keyData = key.split(":", 2);
|
||||
+ if (keyData.length == 2) {
|
||||
+ keys.add(new org.bukkit.NamespacedKey(keyData[0], keyData[1]));
|
||||
+ }
|
||||
+ });
|
||||
+ return java.util.Collections.unmodifiableSet(keys);
|
||||
+ };
|
||||
+
|
||||
+ @Override
|
||||
+ public boolean isEmpty() {
|
||||
+ return CraftItemStack.this.getPdcTag().isEmpty();
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public void copyTo(final org.bukkit.persistence.PersistentDataContainer other, final boolean replace) {
|
||||
+ Preconditions.checkArgument(other != null, "The target container cannot be null");
|
||||
+ final org.bukkit.craftbukkit.persistence.CraftPersistentDataContainer target = (org.bukkit.craftbukkit.persistence.CraftPersistentDataContainer) other;
|
||||
+ final net.minecraft.nbt.CompoundTag pdcTag = org.bukkit.craftbukkit.inventory.CraftItemStack.this.getPdcTag();
|
||||
+ for (final String key : pdcTag.getAllKeys()) {
|
||||
+ if (replace || !target.getRaw().containsKey(key)) {
|
||||
+ target.getRaw().put(key, pdcTag.get(key).copy());
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+ };
|
||||
+ @Override
|
||||
+ public io.papermc.paper.persistence.PersistentDataContainerView getPersistentDataContainer() {
|
||||
|
Reference in New Issue
Block a user