aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/1037-General-ItemMeta-fixes.patch
diff options
context:
space:
mode:
Diffstat (limited to 'patches/server/1037-General-ItemMeta-fixes.patch')
-rw-r--r--patches/server/1037-General-ItemMeta-fixes.patch1498
1 files changed, 1498 insertions, 0 deletions
diff --git a/patches/server/1037-General-ItemMeta-fixes.patch b/patches/server/1037-General-ItemMeta-fixes.patch
new file mode 100644
index 0000000000..052b5dc3ba
--- /dev/null
+++ b/patches/server/1037-General-ItemMeta-fixes.patch
@@ -0,0 +1,1498 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: Jake Potrebic <[email protected]>
+Date: Sat, 27 Apr 2024 20:56:17 -0700
+Subject: [PATCH] General ItemMeta fixes
+
+== AT ==
+private-f net/minecraft/world/item/ItemStack components
+public net/minecraft/world/food/FoodProperties DEFAULT_EAT_SECONDS
+public org/bukkit/craftbukkit/block/CraftBlockStates getBlockState(Lorg/bukkit/World;Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/block/state/BlockState;Lnet/minecraft/world/level/block/entity/BlockEntity;)Lorg/bukkit/craftbukkit/block/CraftBlockState;
+public net/minecraft/world/level/block/entity/BlockEntity saveId(Lnet/minecraft/nbt/CompoundTag;)V
+
+diff --git a/src/main/java/net/minecraft/world/item/ItemStack.java b/src/main/java/net/minecraft/world/item/ItemStack.java
+index 8e2b3dd109dca3089cbce82cd3788874613a3230..893efb2c4a07c33d41e934279dd914a9dbd4ef79 100644
+--- a/src/main/java/net/minecraft/world/item/ItemStack.java
++++ b/src/main/java/net/minecraft/world/item/ItemStack.java
+@@ -414,7 +414,7 @@ public final class ItemStack implements DataComponentHolder {
+ } finally {
+ world.captureBlockStates = false;
+ }
+- DataComponentPatch newData = this.getComponentsPatch();
++ DataComponentPatch newData = this.components.asPatch(); // Paper - Directly access components as patch instead of getComponentsPatch as said method yields EMPTY on items with count 0
+ int newCount = this.getCount();
+ this.setCount(oldCount);
+ this.restorePatch(oldData);
+@@ -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 65170cbb50d8d5030fc5e33b6389c554aec6ae31..6349f2e0a5ba30d250f5ffe43771f325c0999a76 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 397eb1a101bd60f49dbb2fa8eddf28f6f233167f..2c61e8d5bbab59c691f4cb003041e7e50e406b85 100644
+--- a/src/main/java/org/bukkit/craftbukkit/block/CraftBlockEntityState.java
++++ b/src/main/java/org/bukkit/craftbukkit/block/CraftBlockEntityState.java
+@@ -135,6 +135,19 @@ public abstract class CraftBlockEntityState<T extends BlockEntity> 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);
++ if (!nbt.isEmpty()) {
++ // have to include the "id" if it's going to have block entity data
++ this.snapshot.saveId(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 3e552a859846d206ba79c3ee740ae76a37ee7606..f1e1953f2dc65dc615b7b7b648c37b195d3b4c25 100644
+--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java
++++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java
+@@ -328,7 +328,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());
+ }
+@@ -689,15 +696,20 @@ 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());
+- }
+- // SpigotCraft#463 this is required now by the Vanilla client, so mimic ItemStack constructor in ensuring it
+- if (item.getItem() != null && item.getMaxDamage() > 0) {
+- item.setDamageValue(item.getDamageValue());
++ item.restorePatch(DataComponentPatch.EMPTY); // Paper - properly apply the new patch from itemmeta
++ item.applyComponents(tag.build()); // Paper - properly apply the new patch from itemmeta
+ }
++ // Paper - this is no longer needed
+
+ return true;
+ }
+diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaAxolotlBucket.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaAxolotlBucket.java
+index 44d8aa7123ac22cf9a22720ecadc8c5f63bafc0a..9665dc043b257bb0d6f7b40fd938ff549ab685c6 100644
+--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaAxolotlBucket.java
++++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaAxolotlBucket.java
+@@ -124,14 +124,13 @@ public class CraftMetaAxolotlBucket extends CraftMetaItem implements AxolotlBuck
+
+ @Override
+ public Axolotl.Variant getVariant() {
++ com.google.common.base.Preconditions.checkState(this.hasVariant(), "Variant is absent, check hasVariant first!"); // Paper - fix NPE
+ return Axolotl.Variant.values()[this.variant];
+ }
+
+ @Override
+ public void setVariant(Axolotl.Variant variant) {
+- if (variant == null) {
+- variant = Axolotl.Variant.LUCY;
+- }
++ com.google.common.base.Preconditions.checkArgument(variant != null, "Variant cannot be null!"); // Paper
+ this.variant = variant.ordinal();
+ }
+
+diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBanner.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBanner.java
+index 1ac3bec02fce28d5ce698305a7482a9eccbb1867..b494568f833dc21d4e2447ac3e5c5002288b5533 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<BannerPatternLayers.Layer> 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 12911233c01d0ac1af9adbd157d56d28361fc76f..f7b3ce6056b2d2c3b1c4d08ddce4553afedecdf7 100644
+--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBlockState.java
++++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBlockState.java
+@@ -142,9 +142,17 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta
+
+ @ItemMetaKey.Specific(ItemMetaKey.Specific.To.NBT)
+ static final ItemMetaKeyType<CustomData> BLOCK_ENTITY_TAG = new ItemMetaKeyType<>(DataComponents.BLOCK_ENTITY_DATA, "BlockEntityTag");
++ static final ItemMetaKey BLOCK_ENTITY_TAG_CUSTOM_DATA = new ItemMetaKey("block-entity-tag"); // Paper
++ static final ItemMetaKey BLOCK_ENTITY_COMPONENTS = new ItemMetaKey("block-entity-components"); // Paper
+
+ final Material material;
+- private CraftBlockEntityState<?> blockEntityTag;
++ // Paper start - store data separately
++ DataComponentMap components = DataComponentMap.EMPTY;
++ CustomData blockEntityTag = CustomData.EMPTY;
++ private Material materialForBlockEntityType() {
++ return (this.material != Material.SHIELD) ? this.material : CraftMetaBlockState.shieldToBannerHack();
++ }
++ // Paper end
+ private CompoundTag internalTag;
+
+ CraftMetaBlockState(CraftMetaItem meta, Material material) {
+@@ -153,41 +161,61 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta
+
+ if (!(meta instanceof CraftMetaBlockState)
+ || ((CraftMetaBlockState) meta).material != material) {
+- this.blockEntityTag = null;
++ // Paper start
++ this.components = DataComponentMap.EMPTY;
++ this.blockEntityTag = CustomData.EMPTY;
++ // Paper end
+ return;
+ }
+
+ CraftMetaBlockState te = (CraftMetaBlockState) meta;
++ // Paper start
++ this.components = te.components;
+ this.blockEntityTag = te.blockEntityTag;
++ // Paper end
+ }
+
+ CraftMetaBlockState(DataComponentPatch tag, Material material, final Set<DataComponentType<?>> extraHandledDcts) { // Paper
+ super(tag, extraHandledDcts); // Paper
+ this.material = material;
+
++ // Paper start - move to separate method to be re-called
++ this.updateBlockState(tag);
++ }
++
++ private void updateBlockState(final DataComponentPatch tag) {
++ // Paper end
+ getOrEmpty(tag, CraftMetaBlockState.BLOCK_ENTITY_TAG).ifPresent((nbt) -> {
+- this.blockEntityTag = CraftMetaBlockState.getBlockState(material, nbt.copyTag());
++ this.blockEntityTag = nbt; // Paper
+ });
+
+ if (!tag.isEmpty()) {
+- CraftBlockEntityState<?> blockEntityTag = this.blockEntityTag;
+- if (blockEntityTag == null) {
+- blockEntityTag = CraftMetaBlockState.getBlockState(material, null);
+- }
+-
+- // Convert to map
+- PatchedDataComponentMap map = new PatchedDataComponentMap(DataComponentMap.EMPTY);
+- map.applyPatch(tag);
+- // Apply
+- Set<DataComponentType<?>> applied = blockEntityTag.applyComponents(map, tag);
++ // Paper start - store data in a DataComponentMap to be used to construct CraftBlockEntityStates
++ final DataComponentMap.Builder map = DataComponentMap.builder();
++ final net.minecraft.world.level.block.entity.BlockEntity dummyBlockEntity = java.util.Objects.requireNonNull(
++ org.bukkit.craftbukkit.block.CraftBlockStates.createNewTileEntity(this.materialForBlockEntityType())
++ );
++
++ // we don't care about what's in here, all
++ // we want is to know which data component types are referenced
++ Set<DataComponentType<?>> applied = dummyBlockEntity.applyComponentsSet(DataComponentMap.EMPTY, DataComponentPatch.EMPTY);
++ // Paper end - store data in a DataComponentMap to be used to construct CraftBlockEntityStates
+ // Mark applied components as handled
+ for (DataComponentType<?> seen : applied) {
+ this.unhandledTags.clear(seen);
+ }
+ // Only set blockEntityTag if something was applied
+ if (!applied.isEmpty()) {
+- this.blockEntityTag = blockEntityTag;
++ // Paper start
++ for (final DataComponentType type : applied) {
++ if (CraftMetaItem.DEFAULT_HANDLED_DCTS.contains(type)) continue;
++ getOrEmpty(tag, type).ifPresent(value -> {
++ map.set(type, value);
++ });
++ }
++ // Paper end
+ }
++ this.components = map.build(); // Paper
+ }
+ }
+
+@@ -200,7 +228,10 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta
+ } else {
+ this.material = Material.AIR;
+ }
+- this.blockEntityTag = CraftMetaBlockState.getBlockState(this.material, this.internalTag);
++ // Paper start
++ if (this.internalTag != null) { // legacy
++ this.setBlockState(CraftMetaBlockState.getBlockState(this.material, this.internalTag));
++ }
+ this.internalTag = null;
+ }
+
+@@ -208,13 +239,21 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta
+ void applyToItem(CraftMetaItem.Applicator tag) {
+ 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
++ final CompoundTag nbt = this.blockEntityTag.copyTag();
++ if (nbt.contains("id", CraftMagicNumbers.NBT.TAG_STRING)) {
++ tag.put(CraftMetaBlockState.BLOCK_ENTITY_TAG, CustomData.of(nbt));
++ } else if (!nbt.isEmpty()) {
++ BlockEntity.addEntityType(nbt, java.util.Objects.requireNonNull(CraftBlockStates.getBlockEntityType(this.materialForBlockEntityType())));
++ tag.put(CraftMetaBlockState.BLOCK_ENTITY_TAG, CustomData.of(nbt));
++ }
+
+- for (TypedDataComponent<?> component : this.blockEntityTag.collectComponents()) {
+- tag.putIfAbsent(component);
+- }
++ for (final TypedDataComponent<?> component : this.components) {
++ if (CraftMetaItem.DEFAULT_HANDLED_DCTS.contains(component.type())) continue; // if the component type was already handled by CraftMetaItem, don't add it again
++ tag.builder.set(component);
+ }
++ // Paper end
+ }
+
+ @Override
+@@ -223,14 +262,29 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta
+
+ if (tag.contains(CraftMetaBlockState.BLOCK_ENTITY_TAG.NBT, CraftMagicNumbers.NBT.TAG_COMPOUND)) {
+ this.internalTag = tag.getCompound(CraftMetaBlockState.BLOCK_ENTITY_TAG.NBT);
++ return; // Paper - if legacy, don't check anything else
++ }
++ // Paper start - new serialization format
++ if (tag.contains(CraftMetaBlockState.BLOCK_ENTITY_TAG_CUSTOM_DATA.NBT, CraftMagicNumbers.NBT.TAG_COMPOUND)) {
++ this.blockEntityTag = CustomData.of(tag.getCompound(CraftMetaBlockState.BLOCK_ENTITY_TAG_CUSTOM_DATA.NBT));
++ }
++ if (tag.contains(CraftMetaBlockState.BLOCK_ENTITY_COMPONENTS.NBT, CraftMagicNumbers.NBT.TAG_COMPOUND)) {
++ this.components = DataComponentMap.CODEC.parse(org.bukkit.craftbukkit.CraftRegistry.getMinecraftRegistry().createSerializationContext(net.minecraft.nbt.NbtOps.INSTANCE), tag.getCompound(CraftMetaBlockState.BLOCK_ENTITY_COMPONENTS.NBT)).getOrThrow();
+ }
++ // Paper end - new serialization format
+ }
+
+ @Override
+ void serializeInternal(final Map<String, Tag> internalTags) {
+- if (this.blockEntityTag != null) {
+- internalTags.put(CraftMetaBlockState.BLOCK_ENTITY_TAG.NBT, this.blockEntityTag.getSnapshotNBT());
++ // Paper start - new serialization format
++ if (!this.blockEntityTag.isEmpty()) {
++ internalTags.put(CraftMetaBlockState.BLOCK_ENTITY_TAG_CUSTOM_DATA.NBT, this.blockEntityTag.getUnsafe()); // unsafe because it's serialized right away
+ }
++ if (!this.components.isEmpty()) {
++ final Tag componentsTag = DataComponentMap.CODEC.encodeStart(org.bukkit.craftbukkit.CraftRegistry.getMinecraftRegistry().createSerializationContext(net.minecraft.nbt.NbtOps.INSTANCE), this.components).getOrThrow();
++ internalTags.put(CraftMetaBlockState.BLOCK_ENTITY_COMPONENTS.NBT, componentsTag);
++ }
++ // Paper end - new serialization format
+ }
+
+ @Override
+@@ -244,9 +298,10 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta
+ int applyHash() {
+ final int original;
+ int hash = original = super.applyHash();
+- if (this.blockEntityTag != null) {
+- hash = 61 * hash + this.blockEntityTag.hashCode();
+- }
++ // Paper start
++ hash = 61 * hash + this.blockEntityTag.hashCode();
++ hash = 61 * hash + this.components.hashCode();
++ // Paper end
+ return original != hash ? CraftMetaBlockState.class.hashCode() ^ hash : hash;
+ }
+
+@@ -258,19 +313,19 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta
+ if (meta instanceof CraftMetaBlockState) {
+ CraftMetaBlockState that = (CraftMetaBlockState) meta;
+
+- return Objects.equal(this.blockEntityTag, that.blockEntityTag);
++ return Objects.equal(this.blockEntityTag, that.blockEntityTag) && Objects.equal(this.components, that.components); // Paper
+ }
+ return true;
+ }
+
+ @Override
+ boolean notUncommon(CraftMetaItem meta) {
+- return super.notUncommon(meta) && (meta instanceof CraftMetaBlockState || this.blockEntityTag == null);
++ return super.notUncommon(meta) && (meta instanceof CraftMetaBlockState || (this.blockEntityTag.isEmpty() && this.components.isEmpty())); // Paper
+ }
+
+ @Override
+ boolean isEmpty() {
+- return super.isEmpty() && this.blockEntityTag == null;
++ return super.isEmpty() && this.blockEntityTag.isEmpty() && this.components.isEmpty(); // Paper
+ }
+
+ @Override
+@@ -281,27 +336,53 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta
+ @Override
+ public CraftMetaBlockState clone() {
+ CraftMetaBlockState meta = (CraftMetaBlockState) super.clone();
+- if (this.blockEntityTag != null) {
+- meta.blockEntityTag = this.blockEntityTag.copy();
+- }
++ // Paper start - no need for "clone" because they are essentially immutables
++ meta.blockEntityTag = this.blockEntityTag;
++ meta.components = this.components;
++ // Paper end
+ return meta;
+ }
+
+ @Override
+ public boolean hasBlockState() {
+- return this.blockEntityTag != null;
++ return !this.blockEntityTag.isEmpty() || !this.components.isEmpty(); // Paper
+ }
+
+ // Paper start - add method to clear block state
+ @Override
+ public void clearBlockState() {
+- this.blockEntityTag = null;
++ // Paper start
++ this.blockEntityTag = CustomData.EMPTY;
++ this.components = DataComponentMap.EMPTY;
++ // Paper end
+ }
+ // Paper end - add method to clear block state
+
+ @Override
+- public BlockState getBlockState() {
+- return (this.blockEntityTag != null) ? this.blockEntityTag.copy() : CraftMetaBlockState.getBlockState(this.material, null);
++ // Paper start - create blockstate on-demand
++ public CraftBlockEntityState<?> getBlockState() {
++ BlockPos pos = BlockPos.ZERO;
++ final Material stateMaterial = this.materialForBlockEntityType();
++ if (!this.blockEntityTag.isEmpty()) {
++ // Paper "id" field is always present now
++ pos = BlockEntity.getPosFromTag(this.blockEntityTag.getUnsafe()); // unsafe is fine here, just querying
++ }
++ final net.minecraft.world.level.block.entity.BlockEntityType<?> type = java.util.Objects.requireNonNull(CraftBlockStates.getBlockEntityType(stateMaterial));
++ final net.minecraft.world.level.block.state.BlockState nmsBlockState = ((org.bukkit.craftbukkit.block.data.CraftBlockData) this.getBlockData(stateMaterial)).getState();
++ final net.minecraft.world.level.block.entity.BlockEntity blockEntity = java.util.Objects.requireNonNull(type.create(pos, nmsBlockState));
++ if (!this.blockEntityTag.isEmpty()) {
++ this.blockEntityTag.loadInto(blockEntity, org.bukkit.craftbukkit.CraftRegistry.getMinecraftRegistry());
++ }
++ final PatchedDataComponentMap patchedMap = new PatchedDataComponentMap(nmsBlockState.getBlock().asItem().components());
++ patchedMap.setAll(this.components);
++ final Applicator applicator = new Applicator() {};
++ super.applyToItem(applicator);
++ patchedMap.applyPatch(applicator.build());
++ blockEntity.applyComponents(nmsBlockState.getBlock().asItem().components(), patchedMap.asPatch());
++
++ // This is expected to always return a CraftBlockEntityState for the passed material:
++ return (CraftBlockEntityState<?>) CraftBlockStates.getBlockState(null, pos, nmsBlockState, blockEntity);
++ // Paper end
+ }
+
+ private static CraftBlockEntityState<?> getBlockState(Material material, CompoundTag blockEntityTag) {
+@@ -331,7 +412,23 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta
+ Class<?> blockStateType = CraftBlockStates.getBlockStateType(stateMaterial);
+ Preconditions.checkArgument(blockStateType == blockState.getClass() && blockState instanceof CraftBlockEntityState, "Invalid blockState for " + this.material);
+
+- this.blockEntityTag = (CraftBlockEntityState<?>) blockState;
++ // Paper start - when a new BlockState is set, the components from that block entity
++ // have to be used to update the fields on CraftMetaItem
++ final CraftBlockEntityState<?> craftBlockState = (CraftBlockEntityState<?>) blockState;
++ final CompoundTag data = craftBlockState.getSnapshotCustomNbtOnly();
++ final PatchedDataComponentMap patchedMap = new net.minecraft.core.component.PatchedDataComponentMap(craftBlockState.getHandle().getBlock().asItem().components());
++ final net.minecraft.core.component.DataComponentMap map = craftBlockState.collectComponents();
++ patchedMap.setAll(map);
++ if (!data.isEmpty()) {
++ patchedMap.set(BLOCK_ENTITY_TAG.TYPE, CustomData.of(data));
++ }
++ final DataComponentPatch patch = patchedMap.asPatch();
++ this.updateFromPatch(patch, null);
++ // we have to reset the fields because this should be like a "new" block entity is being used
++ this.blockEntityTag = CustomData.EMPTY;
++ this.components = DataComponentMap.EMPTY;
++ this.updateBlockState(patch);
++ // Paper end
+ }
+
+ private static Material shieldToBannerHack() {
+diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBook.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBook.java
+index a395c7ce952f4a60a5edf80e8731afa6388d18ea..e213ac74f8f4a62b7b8b2b7889250f5cdeb348fe 100644
+--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBook.java
++++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBook.java
+@@ -34,7 +34,7 @@ public class CraftMetaBook extends CraftMetaItem implements BookMeta, WritableBo
+ @ItemMetaKey.Specific(ItemMetaKey.Specific.To.NBT)
+ static final ItemMetaKeyType<WritableBookContent> BOOK_CONTENT = new ItemMetaKeyType<>(DataComponents.WRITABLE_BOOK_CONTENT);
+ static final ItemMetaKey BOOK_PAGES = new ItemMetaKey("pages");
+- static final int MAX_PAGES = Integer.MAX_VALUE; // SPIGOT-6911: Use Minecraft limits
++ static final int MAX_PAGES = WritableBookContent.MAX_PAGES; // SPIGOT-6911: Use Minecraft limits // Paper
+ static final int MAX_PAGE_LENGTH = WritableBookContent.PAGE_EDIT_LENGTH; // SPIGOT-6911: Use Minecraft limits
+
+ // We store the pages in their raw original text representation. See SPIGOT-5063, SPIGOT-5350, SPIGOT-3206
+diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBookSigned.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBookSigned.java
+index 3f78a0935d738854182254b345064e3c225dcd5f..218df87c596d47b431dbbf2aa42822ef174f948f 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));
+ }
+ }
+
+@@ -358,7 +358,13 @@ public class CraftMetaBookSigned extends CraftMetaItem implements BookMeta {
+ }
+
+ if (this.pages != null) {
+- builder.put(CraftMetaBookSigned.BOOK_PAGES.BUKKIT, ImmutableList.copyOf(this.pages));
++ // Paper start - deserialization expects json
++ final List<String> jsonPages = new ArrayList<>(this.pages.size());
++ for (final Component page : this.pages) {
++ jsonPages.add(CraftChatMessage.toJSON(page));
++ }
++ builder.put(CraftMetaBookSigned.BOOK_PAGES.BUKKIT, ImmutableList.copyOf(jsonPages));
++ // Paper end - deserialization expects json
+ }
+
+ if (this.resolved) {
+diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBundle.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBundle.java
+index f8c02fe01fd95aa5de8523c9ad452d91f5d3c16f..4447c754458e9fdead003b4044bf2bee6fcbaaa2 100644
+--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBundle.java
++++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBundle.java
+@@ -42,7 +42,7 @@ public class CraftMetaBundle extends CraftMetaItem implements BundleMeta {
+ bundle.items().forEach((item) -> {
+ ItemStack itemStack = CraftItemStack.asCraftMirror(item);
+
+- if (!itemStack.getType().isAir()) { // SPIGOT-7174 - Avoid adding air
++ if (!itemStack.isEmpty()) { // SPIGOT-7174 - Avoid adding air // Paper
+ this.addItem(itemStack);
+ }
+ });
+@@ -55,7 +55,7 @@ public class CraftMetaBundle extends CraftMetaItem implements BundleMeta {
+ Iterable<?> items = SerializableMeta.getObject(Iterable.class, map, CraftMetaBundle.ITEMS.BUKKIT, true);
+ if (items != null) {
+ for (Object stack : items) {
+- if (stack instanceof ItemStack itemStack && !itemStack.getType().isAir()) { // SPIGOT-7174 - Avoid adding air
++ if (stack instanceof ItemStack itemStack && !itemStack.isEmpty()) { // SPIGOT-7174 - Avoid adding air // Paper
+ this.addItem(itemStack);
+ }
+ }
+@@ -116,7 +116,7 @@ public class CraftMetaBundle extends CraftMetaItem implements BundleMeta {
+
+ @Override
+ public void addItem(ItemStack item) {
+- Preconditions.checkArgument(item != null && !item.getType().isAir(), "item is null or air");
++ Preconditions.checkArgument(item != null && !item.isEmpty(), "item is null or empty"); // Paper
+
+ if (this.items == null) {
+ this.items = new ArrayList<>();
+diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaColorableArmor.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaColorableArmor.java
+index 2c9ca54267579a210d4ea192517fc0fbce8e467a..4e423c4790d7b03c283c9a5fa94bce4a1153445e 100644
+--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaColorableArmor.java
++++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaColorableArmor.java
+@@ -22,16 +22,30 @@ public class CraftMetaColorableArmor extends CraftMetaArmor implements Colorable
+ Material.WOLF_ARMOR
+ );
+
+- private Color color = DEFAULT_LEATHER_COLOR;
++ private Integer color; // Paper - keep color component consistent with vanilla (top byte is ignored)
+
+ CraftMetaColorableArmor(CraftMetaItem meta) {
+ super(meta);
+- CraftMetaLeatherArmor.readColor(this, meta);
++ // Paper start
++ if (!(meta instanceof CraftMetaColorableArmor armorMeta)) {
++ return;
++ }
++
++ this.color = armorMeta.color;
++ // Paper end
+ }
+
+ CraftMetaColorableArmor(DataComponentPatch tag, java.util.Set<net.minecraft.core.component.DataComponentType<?>> extraHandledDcts) { // Paper
+ super(tag, extraHandledDcts); // Paper
+- CraftMetaLeatherArmor.readColor(this, tag);
++ // Paper start
++ getOrEmpty(tag, CraftMetaLeatherArmor.COLOR).ifPresent((dyedItemColor) -> {
++ if (!dyedItemColor.showInTooltip()) {
++ this.addItemFlags(org.bukkit.inventory.ItemFlag.HIDE_DYE);
++ }
++
++ this.color = dyedItemColor.rgb();
++ });
++ // Paper end
+ }
+
+ CraftMetaColorableArmor(Map<String, Object> map) {
+@@ -42,7 +56,11 @@ public class CraftMetaColorableArmor extends CraftMetaArmor implements Colorable
+ @Override
+ void applyToItem(CraftMetaItem.Applicator itemTag) {
+ super.applyToItem(itemTag);
+- CraftMetaLeatherArmor.applyColor(this, itemTag);
++ // Paper start
++ if (this.hasColor()) {
++ itemTag.put(CraftMetaLeatherArmor.COLOR, new net.minecraft.world.item.component.DyedItemColor(this.color, !this.hasItemFlag(org.bukkit.inventory.ItemFlag.HIDE_DYE)));
++ }
++ // Paper end
+ }
+
+ @Override
+@@ -68,16 +86,16 @@ public class CraftMetaColorableArmor extends CraftMetaArmor implements Colorable
+
+ @Override
+ public Color getColor() {
+- return this.color;
++ return this.color == null ? DEFAULT_LEATHER_COLOR : Color.fromRGB(this.color & 0xFFFFFF); // Paper - this should really be nullable
+ }
+
+ @Override
+ public void setColor(Color color) {
+- this.color = color == null ? DEFAULT_LEATHER_COLOR : color;
++ this.color = color == null ? null : color.asRGB(); // Paper
+ }
+
+ boolean hasColor() {
+- return CraftMetaLeatherArmor.hasColor(this);
++ return this.color != null; // Paper
+ }
+
+ @Override
+@@ -97,7 +115,7 @@ public class CraftMetaColorableArmor extends CraftMetaArmor implements Colorable
+ if (meta instanceof CraftMetaColorableArmor) {
+ CraftMetaColorableArmor that = (CraftMetaColorableArmor) meta;
+
+- return this.color.equals(that.color);
++ return this.hasColor() ? that.hasColor() && this.color.equals(that.color) : !that.hasColor(); // Paper - allow null
+ }
+ return true;
+ }
+diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaCompass.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaCompass.java
+index bbca26f5debb263b04516e68f6e49f68a38fa5b1..aacc4d010f4dfa4d9d11332b802205a6f35b6de3 100644
+--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaCompass.java
++++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaCompass.java
+@@ -36,7 +36,7 @@ public class CraftMetaCompass extends CraftMetaItem implements CompassMeta {
+ private int lodestoneX;
+ private int lodestoneY;
+ private int lodestoneZ;
+- private boolean tracked = true;
++ private Boolean tracked = null; // Paper - tri-state
+
+ CraftMetaCompass(CraftMetaItem meta) {
+ super(meta);
+@@ -80,7 +80,7 @@ public class CraftMetaCompass extends CraftMetaItem implements CompassMeta {
+ this.setLodestone(lodestone);
+ }
+ }
+- this.tracked = SerializableMeta.getBoolean(map, CraftMetaCompass.LODESTONE_TRACKED.BUKKIT);
++ this.tracked = SerializableMeta.getObjectOptionally(Boolean.class, map, CraftMetaCompass.LODESTONE_TRACKED.BUKKIT, true).orElse(null); // Paper - tri-state
+ }
+
+ @Override
+@@ -146,12 +146,12 @@ public class CraftMetaCompass extends CraftMetaItem implements CompassMeta {
+ }
+
+ boolean hasLodestoneTracked() {
+- return !this.tracked;
++ return this.tracked != null; // Paper - tri-state
+ }
+
+ @Override
+ public boolean isLodestoneTracked() {
+- return this.tracked;
++ return this.tracked != null && this.tracked; // Paper - tri-state
+ }
+
+ @Override
+diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaCrossbow.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaCrossbow.java
+index a3fa95377e083e51ad7596d21eeb08172bdb18b2..0f5a64fc5eb619e18f5eeb97f377c9d593e4a38f 100644
+--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaCrossbow.java
++++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaCrossbow.java
+@@ -122,7 +122,7 @@ public class CraftMetaCrossbow extends CraftMetaItem implements CrossbowMeta {
+ @Override
+ public void addChargedProjectile(ItemStack item) {
+ Preconditions.checkArgument(item != null, "item");
+- Preconditions.checkArgument(item.getType() == Material.FIREWORK_ROCKET || CraftItemType.bukkitToMinecraft(item.getType()) instanceof ArrowItem, "Item %s is not an arrow or firework rocket", item);
++ Preconditions.checkArgument(!item.isEmpty(), "Item cannot be empty"); // Paper
+
+ if (this.chargedProjectiles == null) {
+ this.chargedProjectiles = new ArrayList<>();
+diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaEntityTag.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaEntityTag.java
+index 3f6c5cbbf63631e4b72dc43558651ea94f31ca78..da474a5b963d8e6769d120e9091e60ed0a468a9f 100644
+--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaEntityTag.java
++++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaEntityTag.java
+@@ -98,7 +98,7 @@ public class CraftMetaEntityTag extends CraftMetaItem {
+ if (meta instanceof CraftMetaEntityTag) {
+ CraftMetaEntityTag that = (CraftMetaEntityTag) meta;
+
+- return this.entityTag != null ? that.entityTag != null && this.entityTag.equals(that.entityTag) : this.entityTag == null;
++ return this.entityTag != null ? that.entityTag != null && this.entityTag.equals(that.entityTag) : that.entityTag == null; // Paper
+ }
+ return true;
+ }
+diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaFirework.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaFirework.java
+index 8e0dd4b7a7a25a8beb27b507047bc48d8227627c..77489c3ffaa3a72d4cf105499a77150fca6d8526 100644
+--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaFirework.java
++++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaFirework.java
+@@ -55,7 +55,7 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta {
+
+ this.power = that.power;
+
+- if (that.hasEffects()) {
++ if (that.effects != null) { // Paper
+ this.effects = new ArrayList<>(that.effects);
+ }
+ }
+@@ -86,19 +86,14 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta {
+ .with(CraftMetaFirework.getEffectType(explosion.shape()));
+
+ IntList colors = explosion.colors();
+- // People using buggy command generators specify a list rather than an int here, so recover with dummy data.
+- // Wrong: Colors: [1234]
+- // Right: Colors: [I;1234]
+- if (colors.isEmpty()) {
+- effect.withColor(Color.WHITE);
+- }
++ // Paper - this is no longer needed
+
+ for (int color : colors) {
+- effect.withColor(Color.fromRGB(color));
++ effect.withColor(Color.fromRGB(color & 0xFFFFFF)); // Paper - try to keep color component consistent with vanilla (top byte is ignored), this will however change the color component for out of bound color
+ }
+
+ for (int color : explosion.fadeColors()) {
+- effect.withFade(Color.fromRGB(color));
++ effect.withFade(Color.fromRGB(color & 0xFFFFFF)); // Paper
+ }
+
+ return effect.build();
+@@ -154,7 +149,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 +157,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 +169,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 +190,13 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta {
+ }
+
+ List<FireworkExplosion> 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));
+ }
+@@ -218,7 +226,7 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta {
+ }
+
+ boolean isFireworkEmpty() {
+- return !(this.hasEffects() || this.hasPower());
++ return !(this.effects != null || this.hasPower()); // Paper - empty effects list should stay on the item
+ }
+
+ boolean hasPower() {
+@@ -234,7 +242,7 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta {
+ if (meta instanceof CraftMetaFirework that) {
+
+ return (this.hasPower() ? that.hasPower() && this.power == that.power : !that.hasPower())
+- && (this.hasEffects() ? that.hasEffects() && this.effects.equals(that.effects) : !that.hasEffects());
++ && (this.effects != null ? that.effects != null && this.effects.equals(that.effects) : that.effects == null); // Paper
+ }
+
+ return true;
+@@ -252,7 +260,7 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta {
+ if (this.hasPower()) {
+ hash = 61 * hash + this.power;
+ }
+- if (this.hasEffects()) {
++ if (this.effects != null) { // Paper
+ hash = 61 * hash + 13 * this.effects.hashCode();
+ }
+ return hash != original ? CraftMetaFirework.class.hashCode() ^ hash : hash;
+@@ -262,7 +270,7 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta {
+ Builder<String, Object> serialize(Builder<String, Object> builder) {
+ super.serialize(builder);
+
+- if (this.hasEffects()) {
++ if (this.effects != null) { // Paper
+ builder.put(CraftMetaFirework.EXPLOSIONS.BUKKIT, ImmutableList.copyOf(this.effects));
+ }
+
+@@ -287,6 +295,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<FireworkEffect>();
+ }
+@@ -296,6 +305,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 +327,7 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta {
+ @Override
+ public void addEffects(Iterable<FireworkEffect> effects) {
+ Preconditions.checkArgument(effects != null, "effects cannot be null");
+- this.safelyAddEffects(effects);
++ this.safelyAddEffects(effects, true); // Paper - limit firework effects
+ }
+
+ @Override
+@@ -349,7 +362,7 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta {
+ @Override
+ public void setPower(int power) {
+ Preconditions.checkArgument(power >= 0, "power cannot be less than zero: %s", power);
+- Preconditions.checkArgument(power < 0x80, "power cannot be more than 127: %s", power);
++ Preconditions.checkArgument(power <= 0xFF, "power cannot be more than 255: %s", power); // Paper - set correct limit
+ this.power = power;
+ }
+ }
+diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java
+index 12a193db7475870e5107c86c7611bb4b92feacb8..c235b80b94fdb6c77766016114713cd501ffd67c 100644
+--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java
++++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java
+@@ -172,9 +172,10 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
+ }
+ }
+
+- static final class Applicator {
++ static abstract class Applicator { // Paper - support updating profile after resolving it
+
+- private final DataComponentPatch.Builder builder = DataComponentPatch.builder();
++ final DataComponentPatch.Builder builder = DataComponentPatch.builder(); // Paper - private -> package-private
++ void skullCallback(com.mojang.authlib.GameProfile gameProfile) {} // Paper - support updating profile after resolving it
+
+ <T> Applicator put(ItemMetaKeyType<T> key, T value) {
+ this.builder.set(key.TYPE, value);
+@@ -293,7 +294,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
+ this.enchantments = new EnchantmentMap(meta.enchantments); // Paper
+ }
+
+- if (meta.hasAttributeModifiers()) {
++ if (meta.attributeModifiers != null) { // Paper
+ this.attributeModifiers = LinkedHashMultimap.create(meta.attributeModifiers);
+ }
+
+@@ -323,6 +324,11 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
+ }
+
+ CraftMetaItem(DataComponentPatch tag, Set<DataComponentType<?>> extraHandledTags) { // Paper - improve handled tags on type changes
++ // Paper start - properly support data components in BlockEntity
++ this.updateFromPatch(tag, extraHandledTags);
++ }
++ protected final void updateFromPatch(DataComponentPatch tag, Set<DataComponentType<?>> extraHandledTags) {
++ // Paper end - properly support data components in BlockEntity
+ CraftMetaItem.getOrEmpty(tag, CraftMetaItem.NAME).ifPresent((component) -> {
+ this.displayName = component;
+ });
+@@ -733,7 +739,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
+ Map<?, ?> mods = SerializableMeta.getObject(Map.class, map, key.BUKKIT, true);
+ Multimap<Attribute, AttributeModifier> result = LinkedHashMultimap.create();
+ if (mods == null) {
+- return result;
++ return null; // Paper - null is different from an empty map
+ }
+
+ for (Object obj : mods.keySet()) {
+@@ -887,10 +893,8 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
+ }
+
+ void applyModifiers(Multimap<Attribute, AttributeModifier> modifiers, CraftMetaItem.Applicator tag) {
+- if (modifiers == null || modifiers.isEmpty()) {
+- if (this.hasItemFlag(ItemFlag.HIDE_ATTRIBUTES)) {
+- tag.put(CraftMetaItem.ATTRIBUTES, new ItemAttributeModifiers(Collections.emptyList(), false));
+- }
++ if (modifiers == null/* || modifiers.isEmpty()*/) { // Paper - empty modifiers has a specific meaning, they should still be saved
++ // Paper - don't save ItemFlag if the underlying data isn't present
+ return;
+ }
+
+@@ -919,7 +923,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
+
+ @Overridden
+ boolean isEmpty() {
+- return !(this.hasDisplayName() || this.hasItemName() || this.hasLocalizedName() || this.hasEnchants() || (this.lore != null) || this.hasCustomModelData() || this.hasBlockData() || this.hasRepairCost() || !this.unhandledTags.build().isEmpty() || !this.persistentDataContainer.isEmpty() || this.hideFlag != 0 || this.isHideTooltip() || this.isUnbreakable() || this.hasEnchantmentGlintOverride() || this.isFireResistant() || this.hasMaxStackSize() || this.hasRarity() || this.hasFood() || this.hasDamage() || this.hasMaxDamage() || this.hasAttributeModifiers() || this.customTag != null || this.canPlaceOnPredicates != null || this.canBreakPredicates != null); // Paper
++ return !(this.hasDisplayName() || this.hasItemName() || this.hasLocalizedName() || this.hasEnchants() || (this.lore != null) || this.hasCustomModelData() || this.hasBlockData() || this.hasRepairCost() || !this.unhandledTags.build().isEmpty() || !this.persistentDataContainer.isEmpty() || this.hideFlag != 0 || this.isHideTooltip() || this.isUnbreakable() || this.hasEnchantmentGlintOverride() || this.isFireResistant() || this.hasMaxStackSize() || this.hasRarity() || this.hasFood() || this.hasDamage() || this.hasMaxDamage() || this.attributeModifiers != null || this.customTag != null || this.canPlaceOnPredicates != null || this.canBreakPredicates != null); // Paper
+ }
+
+ // Paper start
+@@ -1015,6 +1019,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
+
+ @Override
+ public void lore(final List<? extends net.kyori.adventure.text.Component> 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
+@@ -1139,6 +1144,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
+ // Paper end
+ @Override
+ public void setLore(List<String> 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 {
+@@ -1154,6 +1160,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
+ // Paper start
+ @Override
+ public void setLoreComponents(List<net.md_5.bungee.api.chat.BaseComponent[]> 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 {
+@@ -1295,7 +1302,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
+
+ @Override
+ public FoodComponent getFood() {
+- return (this.hasFood()) ? new CraftFoodComponent(this.food) : new CraftFoodComponent(new FoodProperties(0, 0, false, 0, Collections.emptyList()));
++ return (this.hasFood()) ? new CraftFoodComponent(this.food) : new CraftFoodComponent(new FoodProperties(0, 0, false, FoodProperties.DEFAULT_EAT_SECONDS, Collections.emptyList())); // Paper - create a valid food properties
+ }
+
+ @Override
+@@ -1321,7 +1328,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
+
+ @Override
+ public Multimap<Attribute, AttributeModifier> getAttributeModifiers(@Nullable EquipmentSlot slot) {
+- this.checkAttributeList();
++ if (this.attributeModifiers == null) return LinkedHashMultimap.create(); // Paper - don't change the components
+ SetMultimap<Attribute, AttributeModifier> result = LinkedHashMultimap.create();
+ for (Map.Entry<Attribute, AttributeModifier> entry : this.attributeModifiers.entries()) {
+ if (entry.getValue().getSlot() == null || entry.getValue().getSlot() == slot) {
+@@ -1334,6 +1341,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
+ @Override
+ public Collection<AttributeModifier> getAttributeModifiers(@Nonnull Attribute attribute) {
+ Preconditions.checkNotNull(attribute, "Attribute cannot be null");
++ if (this.attributeModifiers == null) return null; // Paper - fix NPE
+ return this.attributeModifiers.containsKey(attribute) ? ImmutableList.copyOf(this.attributeModifiers.get(attribute)) : null;
+ }
+
+@@ -1341,10 +1349,12 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
+ public boolean addAttributeModifier(@Nonnull Attribute attribute, @Nonnull AttributeModifier modifier) {
+ Preconditions.checkNotNull(attribute, "Attribute cannot be null");
+ Preconditions.checkNotNull(modifier, "AttributeModifier cannot be null");
+- this.checkAttributeList();
++ if (this.attributeModifiers != null) { // Paper
+ for (Map.Entry<Attribute, AttributeModifier> entry : this.attributeModifiers.entries()) {
+ Preconditions.checkArgument(!(entry.getValue().getUniqueId().equals(modifier.getUniqueId()) && entry.getKey() == attribute), "Cannot register AttributeModifier. Modifier is already applied! %s", modifier); // Paper
+ }
++ } // Paper
++ this.checkAttributeList(); // Paper - moved down
+ return this.attributeModifiers.put(attribute, modifier);
+ }
+
+@@ -1355,8 +1365,11 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
+ return;
+ }
+
+- this.checkAttributeList();
+- this.attributeModifiers.clear();
++ // Paper start - fix modifiers meta
++ if (this.attributeModifiers != null) {
++ this.attributeModifiers.clear();
++ }
++ // Paper end
+
+ Iterator<Map.Entry<Attribute, AttributeModifier>> iterator = attributeModifiers.entries().iterator();
+ while (iterator.hasNext()) {
+@@ -1366,6 +1379,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
+ iterator.remove();
+ continue;
+ }
++ this.checkAttributeList(); // Paper - moved down
+ this.attributeModifiers.put(next.getKey(), next.getValue());
+ }
+ }
+@@ -1373,13 +1387,13 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
+ @Override
+ public boolean removeAttributeModifier(@Nonnull Attribute attribute) {
+ Preconditions.checkNotNull(attribute, "Attribute cannot be null");
+- this.checkAttributeList();
++ if (this.attributeModifiers == null) return false; // Paper
+ return !this.attributeModifiers.removeAll(attribute).isEmpty();
+ }
+
+ @Override
+ public boolean removeAttributeModifier(@Nullable EquipmentSlot slot) {
+- this.checkAttributeList();
++ if (this.attributeModifiers == null) return false; // Paper
+ int removed = 0;
+ Iterator<Map.Entry<Attribute, AttributeModifier>> iter = this.attributeModifiers.entries().iterator();
+
+@@ -1399,7 +1413,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
+ public boolean removeAttributeModifier(@Nonnull Attribute attribute, @Nonnull AttributeModifier modifier) {
+ Preconditions.checkNotNull(attribute, "Attribute cannot be null");
+ Preconditions.checkNotNull(modifier, "AttributeModifier cannot be null");
+- this.checkAttributeList();
++ if (this.attributeModifiers == null) return false; // Paper
+ int removed = 0;
+ Iterator<Map.Entry<Attribute, AttributeModifier>> iter = this.attributeModifiers.entries().iterator();
+
+@@ -1421,7 +1435,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();
+@@ -1430,7 +1444,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
+
+ @Override
+ public String getAsComponentString() {
+- CraftMetaItem.Applicator tag = new CraftMetaItem.Applicator();
++ CraftMetaItem.Applicator tag = new CraftMetaItem.Applicator() {}; // Paper
+ this.applyToItem(tag);
+ DataComponentPatch patch = tag.build();
+
+@@ -1470,6 +1484,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
+ if (first == null || second == null) {
+ return false;
+ }
++ if (first.isEmpty() && second.isEmpty()) return true; // Paper - empty modifiers are equivalent
+ for (Map.Entry<Attribute, AttributeModifier> entry : first.entries()) {
+ if (!second.containsEntry(entry.getKey(), entry.getValue())) {
+ return false;
+@@ -1495,6 +1510,8 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
+
+ @Override
+ public void setDamage(int damage) {
++ Preconditions.checkArgument(damage >= 0, "Damage cannot be negative"); // Paper
++ Preconditions.checkArgument(!this.hasMaxDamage() || damage <= this.maxDamage, "Damage cannot exceed max damage"); // Paper
+ this.damage = damage;
+ }
+
+@@ -1511,6 +1528,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
+
+ @Override
+ public void setMaxDamage(Integer maxDamage) {
++ Preconditions.checkArgument(maxDamage == null || maxDamage > 0, "Max damage should be positive"); // Paper
+ this.maxDamage = maxDamage;
+ }
+
+@@ -1542,7 +1560,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
+ && (this.hasCustomModelData() ? that.hasCustomModelData() && this.customModelData.equals(that.customModelData) : !that.hasCustomModelData())
+ && (this.hasBlockData() ? that.hasBlockData() && this.blockData.equals(that.blockData) : !that.hasBlockData())
+ && (this.hasRepairCost() ? that.hasRepairCost() && this.repairCost == that.repairCost : !that.hasRepairCost())
+- && (this.hasAttributeModifiers() ? that.hasAttributeModifiers() && CraftMetaItem.compareModifiers(this.attributeModifiers, that.attributeModifiers) : !that.hasAttributeModifiers())
++ && (this.attributeModifiers != null ? that.attributeModifiers != null && CraftMetaItem.compareModifiers(this.attributeModifiers, that.attributeModifiers) : that.attributeModifiers == null) // Paper - track only null modifiers
+ && (this.unhandledTags.equals(that.unhandledTags))
+ && (Objects.equals(this.customTag, that.customTag))
+ && (this.persistentDataContainer.equals(that.persistentDataContainer))
+@@ -1598,8 +1616,8 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
+ hash = 61 * hash + (this.hasRarity() ? this.rarity.hashCode() : 0);
+ hash = 61 * hash + (this.hasFood() ? this.food.hashCode() : 0);
+ hash = 61 * hash + (this.hasDamage() ? this.damage : 0);
+- hash = 61 * hash + (this.hasMaxDamage() ? 1231 : 1237);
+- hash = 61 * hash + (this.hasAttributeModifiers() ? this.attributeModifiers.hashCode() : 0);
++ hash = 61 * hash + (this.hasMaxDamage() ? this.maxDamage.hashCode() : 0); // Paper - max damage is not a boolean
++ hash = 61 * hash + (this.attributeModifiers != null ? this.attributeModifiers.hashCode() : 0); // Paper - track only null attributes
+ hash = 61 * hash + (this.canPlaceOnPredicates != null ? this.canPlaceOnPredicates.hashCode() : 0); // Paper
+ hash = 61 * hash + (this.canBreakPredicates != null ? this.canBreakPredicates.hashCode() : 0); // Paper
+ hash = 61 * hash + this.version;
+@@ -1619,7 +1637,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
+ if (this.enchantments != null) {
+ clone.enchantments = new EnchantmentMap(this.enchantments); // Paper
+ }
+- if (this.hasAttributeModifiers()) {
++ if (this.attributeModifiers != null) { // Paper
+ clone.attributeModifiers = LinkedHashMultimap.create(this.attributeModifiers);
+ }
+ if (this.customTag != null) {
+@@ -1823,7 +1841,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
+ }
+
+ static void serializeModifiers(Multimap<Attribute, AttributeModifier> modifiers, ImmutableMap.Builder<String, Object> builder, ItemMetaKey key) {
+- if (modifiers == null || modifiers.isEmpty()) {
++ if (modifiers == null/* || modifiers.isEmpty()*/) { // Paper - null and an empty map have different behaviors
+ return;
+ }
+
+@@ -1905,7 +1923,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
+ // Paper start - improve checking handled tags
+ @org.jetbrains.annotations.VisibleForTesting
+ public static final Map<Class<? extends CraftMetaItem>, Set<DataComponentType<?>>> HANDLED_DCTS_PER_TYPE = new HashMap<>();
+- private static final Set<DataComponentType<?>> DEFAULT_HANDLED_DCTS = Set.of(
++ protected static final Set<DataComponentType<?>> DEFAULT_HANDLED_DCTS = Set.of(
+ CraftMetaItem.NAME.TYPE,
+ CraftMetaItem.ITEM_NAME.TYPE,
+ CraftMetaItem.LORE.TYPE,
+@@ -1971,7 +1989,12 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
+ // Paper end - improve checking handled data component types
+
+ protected static <T> Optional<? extends T> getOrEmpty(DataComponentPatch tag, ItemMetaKeyType<T> type) {
+- Optional<? extends T> result = tag.get(type.TYPE);
++ // Paper start
++ return getOrEmpty(tag, type.TYPE);
++ }
++ protected static <T> Optional<? extends T> getOrEmpty(final DataComponentPatch tag, final DataComponentType<T> type) {
++ Optional<? extends T> result = tag.get(type);
++ // Paper end
+
+ return (result != null) ? result : Optional.empty();
+ }
+diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaLeatherArmor.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaLeatherArmor.java
+index 157a7b7351f48e68d2923c72ed3bbe3dcae21383..70c0d4cc85c045d040a35cd406f3f7ce9b6a58fa 100644
+--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaLeatherArmor.java
++++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaLeatherArmor.java
+@@ -28,16 +28,30 @@ class CraftMetaLeatherArmor extends CraftMetaItem implements LeatherArmorMeta {
+
+ static final ItemMetaKeyType<DyedItemColor> COLOR = new ItemMetaKeyType<>(DataComponents.DYED_COLOR, "color");
+
+- private Color color = DEFAULT_LEATHER_COLOR;
++ private Integer color; // Paper - keep color component consistent with vanilla (top byte is ignored)
+
+ CraftMetaLeatherArmor(CraftMetaItem meta) {
+ super(meta);
+- CraftMetaLeatherArmor.readColor(this, meta);
++ // Paper start
++ if (!(meta instanceof CraftMetaLeatherArmor leatherMeta)) {
++ return;
++ }
++
++ this.color = leatherMeta.color;
++ // Paper end
+ }
+
+ CraftMetaLeatherArmor(DataComponentPatch tag, java.util.Set<net.minecraft.core.component.DataComponentType<?>> extraHandledDcts) { // Paper
+ super(tag, extraHandledDcts); // Paper
+- CraftMetaLeatherArmor.readColor(this, tag);
++ // Paper start
++ getOrEmpty(tag, CraftMetaLeatherArmor.COLOR).ifPresent((dyedItemColor) -> {
++ if (!dyedItemColor.showInTooltip()) {
++ this.addItemFlags(ItemFlag.HIDE_DYE);
++ }
++
++ this.color = dyedItemColor.rgb();
++ });
++ // Paper end
+ }
+
+ CraftMetaLeatherArmor(Map<String, Object> map) {
+@@ -48,7 +62,11 @@ class CraftMetaLeatherArmor extends CraftMetaItem implements LeatherArmorMeta {
+ @Override
+ void applyToItem(CraftMetaItem.Applicator itemTag) {
+ super.applyToItem(itemTag);
+- CraftMetaLeatherArmor.applyColor(this, itemTag);
++ // Paper start
++ if (this.hasColor()) {
++ itemTag.put(CraftMetaLeatherArmor.COLOR, new DyedItemColor(this.color, !this.hasItemFlag(ItemFlag.HIDE_DYE)));
++ }
++ // Paper end
+ }
+
+ @Override
+@@ -72,16 +90,16 @@ class CraftMetaLeatherArmor extends CraftMetaItem implements LeatherArmorMeta {
+
+ @Override
+ public Color getColor() {
+- return this.color;
++ return this.color == null ? DEFAULT_LEATHER_COLOR : Color.fromRGB(this.color & 0xFFFFFF); // Paper
+ }
+
+ @Override
+ public void setColor(Color color) {
+- this.color = color == null ? DEFAULT_LEATHER_COLOR : color;
++ this.color = color == null ? null : color.asRGB(); // Paper
+ }
+
+ boolean hasColor() {
+- return CraftMetaLeatherArmor.hasColor(this);
++ return this.color != null; // Paper
+ }
+
+ @Override
+@@ -101,7 +119,7 @@ class CraftMetaLeatherArmor extends CraftMetaItem implements LeatherArmorMeta {
+ if (meta instanceof CraftMetaLeatherArmor) {
+ CraftMetaLeatherArmor that = (CraftMetaLeatherArmor) meta;
+
+- return this.color.equals(that.color);
++ return this.hasColor() ? that.hasColor() && this.color.equals(that.color) : !that.hasColor(); // Paper - allow null
+ }
+ return true;
+ }
+@@ -121,14 +139,16 @@ class CraftMetaLeatherArmor extends CraftMetaItem implements LeatherArmorMeta {
+ return original != hash ? CraftMetaLeatherArmor.class.hashCode() ^ hash : hash;
+ }
+
++ @io.papermc.paper.annotation.DoNotUse // Paper
+ static void readColor(LeatherArmorMeta meta, CraftMetaItem other) {
+ if (!(other instanceof CraftMetaLeatherArmor armorMeta)) {
+ return;
+ }
+
+- meta.setColor(armorMeta.color);
++ // meta.setColor(armorMeta.color); // Paper - commented out, color is now an integer and cannot be passed to setColor
+ }
+
++ @io.papermc.paper.annotation.DoNotUse // Paper
+ static void readColor(LeatherArmorMeta meta, DataComponentPatch tag) {
+ getOrEmpty(tag, CraftMetaLeatherArmor.COLOR).ifPresent((dyedItemColor) -> {
+ if (!dyedItemColor.showInTooltip()) {
+@@ -151,6 +171,7 @@ class CraftMetaLeatherArmor extends CraftMetaItem implements LeatherArmorMeta {
+ return !DEFAULT_LEATHER_COLOR.equals(meta.getColor());
+ }
+
++ @io.papermc.paper.annotation.DoNotUse // Paper
+ static void applyColor(LeatherArmorMeta meta, CraftMetaItem.Applicator tag) {
+ if (CraftMetaLeatherArmor.hasColor(meta)) {
+ tag.put(CraftMetaLeatherArmor.COLOR, new DyedItemColor(meta.getColor().asRGB(), !meta.hasItemFlag(ItemFlag.HIDE_DYE)));
+diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaMap.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaMap.java
+index d829f4da371b44e7480896118547734be400a314..0ff40e10ce8bab8010222ef7697d585430aeda03 100644
+--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaMap.java
++++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaMap.java
+@@ -30,7 +30,7 @@ class CraftMetaMap extends CraftMetaItem implements MapMeta {
+
+ private Integer mapId;
+ private byte scaling = CraftMetaMap.SCALING_EMPTY;
+- private Color color;
++ private Integer color; // Paper - keep color component consistent with vanilla (top byte is ignored)
+
+ CraftMetaMap(CraftMetaItem meta) {
+ super(meta);
+@@ -58,7 +58,7 @@ class CraftMetaMap extends CraftMetaItem implements MapMeta {
+
+ getOrEmpty(tag, CraftMetaMap.MAP_COLOR).ifPresent((mapColor) -> {
+ try {
+- this.color = Color.fromRGB(mapColor.rgb());
++ this.color = mapColor.rgb(); // Paper
+ } catch (IllegalArgumentException ex) {
+ // Invalid colour
+ }
+@@ -102,7 +102,7 @@ class CraftMetaMap extends CraftMetaItem implements MapMeta {
+ }
+
+ if (this.hasColor()) {
+- tag.put(CraftMetaMap.MAP_COLOR, new MapItemColor(this.color.asRGB()));
++ tag.put(CraftMetaMap.MAP_COLOR, new MapItemColor(this.color)); // Paper
+ }
+ }
+
+@@ -127,6 +127,7 @@ class CraftMetaMap extends CraftMetaItem implements MapMeta {
+
+ @Override
+ public int getMapId() {
++ Preconditions.checkState(this.hasMapId(), "Item does not have map associated - check hasMapId() first!"); // Paper - fix NPE
+ return this.mapId;
+ }
+
+@@ -187,12 +188,12 @@ class CraftMetaMap extends CraftMetaItem implements MapMeta {
+
+ @Override
+ public Color getColor() {
+- return this.color;
++ return this.color == null ? null : Color.fromRGB(this.color & 0xFFFFFF); // Paper
+ }
+
+ @Override
+ public void setColor(Color color) {
+- this.color = color;
++ this.color = color == null ? null : color.asRGB(); // Paper
+ }
+
+ @Override
+diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaOminousBottle.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaOminousBottle.java
+index 7197c4f5698fd041c4db6d0f6a80c55f77661789..062ef890b42075adb5663453806dda3ad89a6aa0 100644
+--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaOminousBottle.java
++++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaOminousBottle.java
+@@ -75,6 +75,7 @@ public class CraftMetaOminousBottle extends CraftMetaItem implements OminousBott
+
+ @Override
+ public int getAmplifier() {
++ Preconditions.checkState(this.hasAmplifier(), "'ominous_bottle_amplifier' data component is absent. Check hasAmplifier first!"); // Paper - fix NPE
+ return this.ominousBottleAmplifier;
+ }
+
+diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java
+index db7f71af22d904de08d4badaa7f66d1286d5bf16..e32143e140db50a11cf602cf622d68b787c0d647 100644
+--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java
++++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java
+@@ -47,7 +47,7 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
+
+ private PotionType type;
+ private List<PotionEffect> customEffects;
+- private Color color;
++ private Integer color; // Paper - keep color component consistent with vanilla (top byte is ignored)
+
+ CraftMetaPotion(CraftMetaItem meta) {
+ super(meta);
+@@ -70,7 +70,7 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
+
+ potionContents.customColor().ifPresent((customColor) -> {
+ try {
+- this.color = Color.fromRGB(customColor);
++ this.color = customColor; // Paper
+ } catch (IllegalArgumentException ex) {
+ // Invalid colour
+ }
+@@ -126,7 +126,7 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
+ super.applyToItem(tag);
+
+ Optional<Holder<Potion>> defaultPotion = (this.hasBasePotionType()) ? Optional.of(CraftPotionType.bukkitToMinecraftHolder(this.type)) : Optional.empty();
+- Optional<Integer> potionColor = (this.hasColor()) ? Optional.of(this.color.asRGB()) : Optional.empty();
++ Optional<Integer> potionColor = (this.hasColor()) ? Optional.of(this.color) : Optional.empty(); // Paper
+
+ List<MobEffectInstance> effectList = new ArrayList<>();
+ if (this.customEffects != null) {
+@@ -295,12 +295,12 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
+
+ @Override
+ public Color getColor() {
+- return this.color;
++ return this.color == null ? null : Color.fromRGB(this.color & 0xFFFFFF); // Paper
+ }
+
+ @Override
+ public void setColor(Color color) {
+- this.color = color;
++ this.color = color == null ? null : color.asRGB(); // Paper
+ }
+
+ @Override
+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/CraftMetaSpawnEgg.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSpawnEgg.java
+index a3c1a8c469630464ac80b7786731462046134998..4bc0aa160e5ed90be622932ff735a9ed98830f33 100644
+--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSpawnEgg.java
++++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSpawnEgg.java
+@@ -251,6 +251,7 @@ public class CraftMetaSpawnEgg extends CraftMetaItem implements SpawnEggMeta {
+
+ @Override
+ public EntitySnapshot getSpawnedEntity() {
++ if (this.entityTag == null) return null; // Paper - fix NPE
+ return CraftEntitySnapshot.create(this.entityTag);
+ }
+
+@@ -268,8 +269,8 @@ public class CraftMetaSpawnEgg extends CraftMetaItem implements SpawnEggMeta {
+ if (meta instanceof CraftMetaSpawnEgg) {
+ CraftMetaSpawnEgg that = (CraftMetaSpawnEgg) meta;
+
+- return this.hasSpawnedType() ? that.hasSpawnedType() && this.spawnedType.equals(that.spawnedType) : !that.hasSpawnedType()
+- && this.entityTag != null ? that.entityTag != null && this.entityTag.equals(that.entityTag) : this.entityTag == null;
++ return (this.hasSpawnedType() ? that.hasSpawnedType() && this.spawnedType.equals(that.spawnedType) : !that.hasSpawnedType()) // Paper
++ && (this.entityTag != null ? that.entityTag != null && this.entityTag.equals(that.entityTag) : that.entityTag == null); // Paper
+ }
+ return true;
+ }
+diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaTropicalFishBucket.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaTropicalFishBucket.java
+index b5392a3a6f6f3d0a54549e6bb93f28590ee048f0..7514aa6f206c4b82fecd112783f96bb9dd73c01f 100644
+--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaTropicalFishBucket.java
++++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaTropicalFishBucket.java
+@@ -126,6 +126,7 @@ class CraftMetaTropicalFishBucket extends CraftMetaItem implements TropicalFishB
+
+ @Override
+ public DyeColor getPatternColor() {
++ com.google.common.base.Preconditions.checkState(this.hasVariant(), "This bucket doesn't have variant, check hasVariant first!"); // Paper - fix NPE
+ return CraftTropicalFish.getPatternColor(this.variant);
+ }
+
+@@ -139,6 +140,7 @@ class CraftMetaTropicalFishBucket extends CraftMetaItem implements TropicalFishB
+
+ @Override
+ public DyeColor getBodyColor() {
++ com.google.common.base.Preconditions.checkState(this.hasVariant(), "This bucket doesn't have variant, check hasVariant first!"); // Paper - fix NPE
+ return CraftTropicalFish.getBodyColor(this.variant);
+ }
+
+@@ -152,6 +154,7 @@ class CraftMetaTropicalFishBucket extends CraftMetaItem implements TropicalFishB
+
+ @Override
+ public TropicalFish.Pattern getPattern() {
++ com.google.common.base.Preconditions.checkState(this.hasVariant(), "This bucket doesn't have variant, check hasVariant first!"); // Paper - fix NPE
+ return CraftTropicalFish.getPattern(this.variant);
+ }
+
+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 <T> java.util.Optional<T> getObjectOptionally(Class<T> 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..b647b5205b9c54ccb83e09a9410c722e33e5378d 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());
+ }
+
+@@ -118,6 +119,7 @@ public final class CraftFoodComponent implements FoodComponent {
+
+ @Override
+ public FoodEffect addEffect(PotionEffect effect, float probability) {
++ Preconditions.checkArgument(0 <= probability && probability <= 1, "Probability cannot be outside range [0,1]"); // Paper
+ List<FoodProperties.PossibleEffect> effects = new ArrayList<>(this.handle.effects());
+
+ FoodProperties.PossibleEffect newEffect = new net.minecraft.world.food.FoodProperties.PossibleEffect(CraftPotionUtil.fromBukkit(effect), probability);
+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/ItemMetaTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java
+index d6018439015583fa0344c7c01b2e60a13de29795..aabe3730fa582f442ee0544dd1a9f3123f719c68 100644
+--- a/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java
++++ b/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java
+@@ -66,7 +66,7 @@ import org.junit.jupiter.api.Test;
+
+ public class ItemMetaTest extends AbstractTestingBase {
+
+- static final int MAX_FIREWORK_POWER = 127; // Please update ItemStackFireworkTest if/when this gets changed.
++ static final int MAX_FIREWORK_POWER = 255; // Please update ItemStackFireworkTest if/when this gets changed. // Paper - it changed
+
+ @Test
+ public void testPowerLimitExact() {
+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