From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Sat, 27 Apr 2024 20:56:17 -0700 Subject: [PATCH] General ItemMeta fixes == AT == private-f net/minecraft/world/item/ItemStack components diff --git a/src/main/java/net/minecraft/world/item/ItemStack.java b/src/main/java/net/minecraft/world/item/ItemStack.java index 8e2b3dd109dca3089cbce82cd3788874613a3230..a45389d64c04cd4c2a35fbc511595be0535a8665 100644 --- a/src/main/java/net/minecraft/world/item/ItemStack.java +++ b/src/main/java/net/minecraft/world/item/ItemStack.java @@ -1251,6 +1251,11 @@ public final class ItemStack implements DataComponentHolder { public void setItem(Item item) { this.bukkitStack = null; // Paper this.item = item; + // Paper start - change base component prototype + final DataComponentPatch patch = this.getComponentsPatch(); + this.components = new PatchedDataComponentMap(this.item.components()); + this.applyComponents(patch); + // Paper end - change base component prototype } // CraftBukkit end diff --git a/src/main/java/net/minecraft/world/level/block/entity/BlockEntity.java b/src/main/java/net/minecraft/world/level/block/entity/BlockEntity.java index f37858eb18defbf11b2f4fe825ab9416ebbc7210..7963afff4b32a0e46be9bdeb413657718cfc14f5 100644 --- a/src/main/java/net/minecraft/world/level/block/entity/BlockEntity.java +++ b/src/main/java/net/minecraft/world/level/block/entity/BlockEntity.java @@ -140,6 +140,11 @@ public abstract class BlockEntity { CompoundTag nbttagcompound = new CompoundTag(); this.saveAdditional(nbttagcompound, registryLookup); + // Paper start - store PDC here as well + if (this.persistentDataContainer != null && !this.persistentDataContainer.isEmpty()) { + nbttagcompound.put("PublicBukkitValues", this.persistentDataContainer.toTagCompound()); + } + // Paper end return nbttagcompound; } diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftBlockEntityState.java b/src/main/java/org/bukkit/craftbukkit/block/CraftBlockEntityState.java index f97d47677e13441c0b39eb8d18ebee428ea53ca4..a0b7ec67755c5090f24bf9ec81f110c68cd064ca 100644 --- a/src/main/java/org/bukkit/craftbukkit/block/CraftBlockEntityState.java +++ b/src/main/java/org/bukkit/craftbukkit/block/CraftBlockEntityState.java @@ -132,6 +132,15 @@ public abstract class CraftBlockEntityState extends Craft return nbt; } + // Paper start - properly save blockentity itemstacks + public CompoundTag getSnapshotCustomNbtOnly() { + this.applyTo(this.snapshot); + final CompoundTag nbt = this.snapshot.saveCustomOnly(this.getRegistryAccess()); + this.snapshot.removeComponentsFromTag(nbt); + return nbt; + } + // Paper end + // copies the data of the given tile entity to this block state protected void load(T tileEntity) { if (tileEntity != null && tileEntity != this.snapshot) { diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java index aa23d417272bb160bba8358a8ab0792b56bc0a01..eba5a27e452c4063567fb02d6aabdfb0446d5daf 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java @@ -326,7 +326,14 @@ public final class CraftItemStack extends ItemStack { // Paper end - improve handled tags on type change // Paper start public static void applyMetaToItem(net.minecraft.world.item.ItemStack itemStack, ItemMeta itemMeta) { - final CraftMetaItem.Applicator tag = new CraftMetaItem.Applicator(); + // Paper start - support updating profile after resolving it + final CraftMetaItem.Applicator tag = new CraftMetaItem.Applicator() { + @Override + void skullCallback(final com.mojang.authlib.GameProfile gameProfile) { + itemStack.set(DataComponents.PROFILE, new net.minecraft.world.item.component.ResolvableProfile(gameProfile)); + } + }; + // Paper end - support updating profile after resolving it ((CraftMetaItem) itemMeta).applyToItem(tag); itemStack.applyComponents(tag.build()); } @@ -687,7 +694,14 @@ public final class CraftItemStack extends ItemStack { } if (!((CraftMetaItem) itemMeta).isEmpty()) { - CraftMetaItem.Applicator tag = new CraftMetaItem.Applicator(); + // Paper start - support updating profile after resolving it + CraftMetaItem.Applicator tag = new CraftMetaItem.Applicator() { + @Override + void skullCallback(final com.mojang.authlib.GameProfile gameProfile) { + item.set(DataComponents.PROFILE, new net.minecraft.world.item.component.ResolvableProfile(gameProfile)); + } + }; + // Paper end - support updating profile after resolving it ((CraftMetaItem) itemMeta).applyToItem(tag); item.restorePatch(tag.build()); diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBanner.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBanner.java index 2d6abecc94683f92da6be26b72ea829663b16d76..6a3b0c7f0cc3ffb17a231383ad103fa792d7b7ba 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBanner.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBanner.java @@ -107,6 +107,7 @@ public class CraftMetaBanner extends CraftMetaItem implements BannerMeta { void applyToItem(CraftMetaItem.Applicator tag) { super.applyToItem(tag); + if (this.patterns.isEmpty()) return; // Paper - don't write empty patterns List newPatterns = new ArrayList<>(); for (Pattern p : this.patterns) { diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBlockState.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBlockState.java index 3b647cb57918ed9d4b54dca718af80d20730c42e..aee276c844b9efc3c16b3f728ef237707011958d 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBlockState.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBlockState.java @@ -234,7 +234,15 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta super.applyToItem(tag); if (this.blockEntityTag != null) { - tag.put(CraftMetaBlockState.BLOCK_ENTITY_TAG, CustomData.of(this.blockEntityTag.getSnapshotNBTWithoutComponents())); + // Paper start - accurately replicate logic for creating ItemStack from BlockEntity + // taken from BlockEntity#saveToItem and BlockItem#setBlockEntityData + CompoundTag nbt = this.blockEntityTag.getSnapshotCustomNbtOnly(); + nbt.remove("id"); + if (!nbt.isEmpty()) { + BlockEntity.addEntityType(nbt, this.blockEntityTag.getTileEntity().getType()); + tag.put(CraftMetaBlockState.BLOCK_ENTITY_TAG, CustomData.of(nbt)); + } + // Paper end for (TypedDataComponent component : this.blockEntityTag.collectComponents()) { tag.builder.set(component); diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBookSigned.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBookSigned.java index 7f3733c29f2e79bffa24631efb20de49fde857f2..6a6e9a1478a2ead20467bc711d0ad4a9ab3010cb 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBookSigned.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBookSigned.java @@ -116,8 +116,8 @@ public class CraftMetaBookSigned extends CraftMetaItem implements BookMeta { } } - this.resolved = SerializableMeta.getObject(Boolean.class, map, CraftMetaBookSigned.RESOLVED.BUKKIT, true); - this.generation = SerializableMeta.getObject(Integer.class, map, CraftMetaBookSigned.GENERATION.BUKKIT, true); + this.resolved = SerializableMeta.getBoolean(map, CraftMetaBookSigned.RESOLVED.BUKKIT); // Paper - General ItemMeta fixes + this.generation = SerializableMeta.getObjectOptionally(Integer.class, map, CraftMetaBookSigned.GENERATION.BUKKIT, true).orElse(0); // Paper - General ItemMeta Fixes } @Override @@ -129,7 +129,7 @@ public class CraftMetaBookSigned extends CraftMetaItem implements BookMeta { for (Component page : this.pages) { list.add(Filterable.passThrough(page)); } - itemData.put(CraftMetaBookSigned.BOOK_CONTENT, new WrittenBookContent(Filterable.from(FilteredText.passThrough(this.title)), this.author, this.generation, list, this.resolved)); + itemData.put(CraftMetaBookSigned.BOOK_CONTENT, new WrittenBookContent(Filterable.from(FilteredText.passThrough(this.title == null ? "" : this.title)), this.author == null ? "" : this.author, this.generation, list, this.resolved)); } } diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaFirework.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaFirework.java index 8e0dd4b7a7a25a8beb27b507047bc48d8227627c..cf5d27ccc2225bac3aa57912f444f95d2f37e32e 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaFirework.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaFirework.java @@ -154,7 +154,7 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta { } Iterable effects = SerializableMeta.getObject(Iterable.class, map, CraftMetaFirework.EXPLOSIONS.BUKKIT, true); - this.safelyAddEffects(effects); + this.safelyAddEffects(effects, false); // Paper - limit firework effects } @Override @@ -162,7 +162,7 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta { return !(this.effects == null || this.effects.isEmpty()); } - void safelyAddEffects(Iterable collection) { + void safelyAddEffects(Iterable collection, final boolean throwOnOversize) { // Paper if (collection == null || (collection instanceof Collection && ((Collection) collection).isEmpty())) { return; } @@ -174,6 +174,15 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta { for (Object obj : collection) { Preconditions.checkArgument(obj instanceof FireworkEffect, "%s in %s is not a FireworkEffect", obj, collection); + // Paper start - limit firework effects + if (effects.size() + 1 > Fireworks.MAX_EXPLOSIONS) { + if (throwOnOversize) { + throw new IllegalArgumentException("Cannot have more than " + Fireworks.MAX_EXPLOSIONS + " firework effects"); + } else { + continue; + } + } + // Paper end - limit firework effects effects.add((FireworkEffect) obj); } } @@ -186,9 +195,13 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta { } List effects = new ArrayList<>(); - for (FireworkEffect effect : this.effects) { - effects.add(CraftMetaFirework.getExplosion(effect)); + // Paper start - fix NPE with effects list being null + if (this.effects != null) { + for (FireworkEffect effect : this.effects) { + effects.add(CraftMetaFirework.getExplosion(effect)); + } } + // Paper end itemTag.put(CraftMetaFirework.FIREWORKS, new Fireworks(this.power, effects)); } @@ -287,6 +300,7 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta { @Override public void addEffect(FireworkEffect effect) { Preconditions.checkArgument(effect != null, "FireworkEffect cannot be null"); + Preconditions.checkArgument(this.effects == null || this.effects.size() + 1 <= Fireworks.MAX_EXPLOSIONS, "cannot have more than %s firework effects", Fireworks.MAX_EXPLOSIONS); // Paper - limit firework effects if (this.effects == null) { this.effects = new ArrayList(); } @@ -296,6 +310,10 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta { @Override public void addEffects(FireworkEffect... effects) { Preconditions.checkArgument(effects != null, "effects cannot be null"); + // Paper start - limit firework effects + final int initialSize = this.effects == null ? 0 : this.effects.size(); + Preconditions.checkArgument(initialSize + effects.length <= Fireworks.MAX_EXPLOSIONS, "Cannot have more than %s firework effects", Fireworks.MAX_EXPLOSIONS); + // Paper end - limit firework effects if (effects.length == 0) { return; } @@ -314,7 +332,7 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta { @Override public void addEffects(Iterable effects) { Preconditions.checkArgument(effects != null, "effects cannot be null"); - this.safelyAddEffects(effects); + this.safelyAddEffects(effects, true); // Paper - limit firework effects } @Override diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java index b351ee556b732e7081a7a6157460ade219829f17..38454fdc978a24a35e685e65e9b41323781c3ead 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java @@ -165,9 +165,10 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta { } } - static final class Applicator { + static abstract class Applicator { // Paper - support updating profile after resolving it final DataComponentPatch.Builder builder = DataComponentPatch.builder(); + void skullCallback(com.mojang.authlib.GameProfile gameProfile) {} // Paper - support updating profile after resolving it Applicator put(ItemMetaKeyType key, T value) { this.builder.set(key.TYPE, value); @@ -1004,6 +1005,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta { @Override public void lore(final List lore) { + Preconditions.checkArgument(lore == null || lore.size() <= ItemLore.MAX_LINES, "lore cannot have more than %s lines", ItemLore.MAX_LINES); // Paper - limit lore lines this.lore = lore != null ? io.papermc.paper.adventure.PaperAdventure.asVanilla(lore) : null; } // Paper end @@ -1128,6 +1130,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta { // Paper end @Override public void setLore(List lore) { + Preconditions.checkArgument(lore == null || lore.size() <= ItemLore.MAX_LINES, "lore cannot have more than %s lines", ItemLore.MAX_LINES); // Paper - limit lore lines if (lore == null || lore.isEmpty()) { this.lore = null; } else { @@ -1143,6 +1146,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta { // Paper start @Override public void setLoreComponents(List lore) { + Preconditions.checkArgument(lore == null || lore.size() <= ItemLore.MAX_LINES, "lore cannot have more than %s lines", ItemLore.MAX_LINES); // Paper - limit lore lines if (lore == null) { this.lore = null; } else { @@ -1410,7 +1414,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta { @Override public String getAsString() { - CraftMetaItem.Applicator tag = new CraftMetaItem.Applicator(); + CraftMetaItem.Applicator tag = new CraftMetaItem.Applicator() {}; // Paper - support updating profile after resolving it this.applyToItem(tag); DataComponentPatch patch = tag.build(); Tag nbt = DataComponentPatch.CODEC.encodeStart(MinecraftServer.getDefaultRegistryAccess().createSerializationContext(NbtOps.INSTANCE), patch).getOrThrow(); diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java index c769d2a210060f6829a6cbe739d6d9ab2f602644..1feffe289a1e714084bd37b5c5ad23a37dd58325 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java @@ -137,10 +137,10 @@ class CraftMetaSkull extends CraftMetaItem implements SkullMeta { // Fill in textures PlayerProfile ownerProfile = new CraftPlayerProfile(this.profile); // getOwnerProfile may return null if (ownerProfile.getTextures().isEmpty()) { - ownerProfile.update().thenAccept((filledProfile) -> { + ownerProfile.update().thenAcceptAsync((filledProfile) -> { // Paper - run on main thread this.setOwnerProfile(filledProfile); - tag.put(CraftMetaSkull.SKULL_PROFILE, new ResolvableProfile(this.profile)); - }); + tag.skullCallback(this.profile); // Paper - actually set profile on itemstack + }, ((org.bukkit.craftbukkit.CraftServer) org.bukkit.Bukkit.getServer()).getServer()); // Paper - run on main thread } } diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/SerializableMeta.java b/src/main/java/org/bukkit/craftbukkit/inventory/SerializableMeta.java index 05a4a06c0def28fc97e61b4712c45c8730fec60c..a86eb660d8f523cb99a0b668ef1130535d50ce1c 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/SerializableMeta.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/SerializableMeta.java @@ -110,4 +110,21 @@ public final class SerializableMeta implements ConfigurationSerializable { } throw new IllegalArgumentException(field + "(" + object + ") is not a valid " + clazz); } + + // Paper start - General ItemMeta Fixes + public static java.util.Optional getObjectOptionally(Class clazz, Map map, Object field, boolean nullable) { + final Object object = map.get(field); + + if (clazz.isInstance(object)) { + return java.util.Optional.of(clazz.cast(object)); + } + if (object == null) { + if (!nullable) { + throw new NoSuchElementException(map + " does not contain " + field); + } + return java.util.Optional.empty(); + } + throw new IllegalArgumentException(field + "(" + object + ") is not a valid " + clazz); + } + // Paper end - General ItemMeta Fixes } diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/components/CraftFoodComponent.java b/src/main/java/org/bukkit/craftbukkit/inventory/components/CraftFoodComponent.java index c68e85cca0f532a94545c0b7f6ed54451ce5a47e..eb08b3453738bffd1a6350dc56c18b9740be5a01 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/components/CraftFoodComponent.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/components/CraftFoodComponent.java @@ -103,6 +103,7 @@ public final class CraftFoodComponent implements FoodComponent { @Override public void setEatSeconds(float eatSeconds) { + Preconditions.checkArgument(eatSeconds > 0, "Eat seconds must be positive"); // Paper - validate eat_seconds this.handle = new FoodProperties(this.handle.nutrition(), this.handle.saturation(), this.handle.canAlwaysEat(), eatSeconds, this.handle.effects()); } diff --git a/src/test/java/org/bukkit/craftbukkit/inventory/DeprecatedItemMetaCustomValueTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/DeprecatedItemMetaCustomValueTest.java index 0b11d5ea89539decd2f6c60c5b581bbd78ff1fd6..74ebadacbbd11b5a0d8f8c6cd6409cce17cfa37d 100644 --- a/src/test/java/org/bukkit/craftbukkit/inventory/DeprecatedItemMetaCustomValueTest.java +++ b/src/test/java/org/bukkit/craftbukkit/inventory/DeprecatedItemMetaCustomValueTest.java @@ -92,7 +92,7 @@ public class DeprecatedItemMetaCustomValueTest extends AbstractTestingBase { public void testNBTTagStoring() { CraftMetaItem itemMeta = this.createComplexItemMeta(); - CraftMetaItem.Applicator compound = new CraftMetaItem.Applicator(); + CraftMetaItem.Applicator compound = new CraftMetaItem.Applicator() {}; // Paper itemMeta.applyToItem(compound); assertEquals(itemMeta, new CraftMetaItem(compound.build(), null)); // Paper diff --git a/src/test/java/org/bukkit/craftbukkit/inventory/PersistentDataContainerTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/PersistentDataContainerTest.java index f3939074a886b20f17b00dd3c39833725f47d3f0..1123cc60671c1a48bba9b2baa1f10c6d5a6855fe 100644 --- a/src/test/java/org/bukkit/craftbukkit/inventory/PersistentDataContainerTest.java +++ b/src/test/java/org/bukkit/craftbukkit/inventory/PersistentDataContainerTest.java @@ -126,7 +126,7 @@ public class PersistentDataContainerTest extends AbstractTestingBase { public void testNBTTagStoring() { CraftMetaItem itemMeta = this.createComplexItemMeta(); - CraftMetaItem.Applicator compound = new CraftMetaItem.Applicator(); + CraftMetaItem.Applicator compound = new CraftMetaItem.Applicator() {}; // Paper itemMeta.applyToItem(compound); assertEquals(itemMeta, new CraftMetaItem(compound.build(), null)); // Paper @@ -472,7 +472,7 @@ public class PersistentDataContainerTest extends AbstractTestingBase { assertEquals(List.of(), container.get(PersistentDataContainerTest.requestKey("list"), PersistentDataType.LIST.strings())); // Write and read the entire container to NBT - final CraftMetaItem.Applicator storage = new CraftMetaItem.Applicator(); + final CraftMetaItem.Applicator storage = new CraftMetaItem.Applicator() {}; // Paper craftItem.applyToItem(storage); final CraftMetaItem readItem = new CraftMetaItem(storage.build(), null); // Paper