2024-04-28 05:57:42 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Sat, 27 Apr 2024 20:56:17 -0700
Subject: [PATCH] General ItemMeta fixes
2024-04-29 02:13:30 +02:00
== AT ==
private-f net/minecraft/world/item/ItemStack components
2024-05-26 00:58:56 +02:00
public net/minecraft/world/food/FoodProperties DEFAULT_EAT_SECONDS
2024-05-26 01:37:35 +02:00
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;
2024-05-29 21:22:51 +02:00
public net/minecraft/world/level/block/entity/BlockEntity saveId(Lnet/minecraft/nbt/CompoundTag;)V
2024-04-28 05:57:42 +02:00
2024-06-15 17:26:55 +02:00
Co-authored-by: GhastCraftHD <julius.gruenberg@leghast.de>
2024-04-29 02:13:30 +02:00
diff --git a/src/main/java/net/minecraft/world/item/ItemStack.java b/src/main/java/net/minecraft/world/item/ItemStack.java
2024-10-25 13:52:04 +02:00
index 98b5208baeaa12a5ff2788e457c542000d6ea48b..babd89f39c43b0c64709d99bf8aca6cdc6ca1b24 100644
2024-04-29 02:13:30 +02:00
--- a/src/main/java/net/minecraft/world/item/ItemStack.java
+++ b/src/main/java/net/minecraft/world/item/ItemStack.java
2024-10-25 13:52:04 +02:00
@@ -1361,6 +1361,11 @@ public final class ItemStack implements DataComponentHolder {
2024-04-29 02:13:30 +02:00
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
2024-04-28 21:34:11 +02:00
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
2024-10-27 18:11:15 +01:00
index 645a7ec0709cbd3c0cfbf75f7b8622a67515f74c..39fc5aa6ac8c66d8dd7437262124b61c4c138689 100644
2024-04-28 21:34:11 +02:00
--- a/src/main/java/net/minecraft/world/level/block/entity/BlockEntity.java
+++ b/src/main/java/net/minecraft/world/level/block/entity/BlockEntity.java
2024-10-27 18:11:15 +01:00
@@ -147,6 +147,11 @@ public abstract class BlockEntity {
2024-04-28 21:34:11 +02:00
CompoundTag nbttagcompound = new CompoundTag();
2024-10-24 17:16:31 +02:00
this.saveAdditional(nbttagcompound, registries);
2024-04-28 21:34:11 +02:00
+ // 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
2024-10-21 00:06:54 +02:00
index fe7e3e0628783d8d1be9635b689da8a9cb46c5d7..04ae258a2f8e98421340d29d5cceedec045171b7 100644
2024-04-28 21:34:11 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/block/CraftBlockEntityState.java
+++ b/src/main/java/org/bukkit/craftbukkit/block/CraftBlockEntityState.java
2024-10-21 00:06:54 +02:00
@@ -151,6 +151,19 @@ public abstract class CraftBlockEntityState<T extends BlockEntity> extends Craft
2024-09-15 21:39:53 +02:00
return this.snapshot.getUpdateTag(this.getRegistryAccess());
2024-04-28 21:34:11 +02:00
}
+ // 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);
2024-05-29 21:22:51 +02:00
+ if (!nbt.isEmpty()) {
+ // have to include the "id" if it's going to have block entity data
+ this.snapshot.saveId(nbt);
+ }
2024-04-28 21:34:11 +02:00
+ 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) {
2024-04-28 07:27:51 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java
2024-10-24 17:16:31 +02:00
index dfdabf86433d323966a748baef769cf0462d3f38..bb2b4528692aed8e3341428697a60c0abee13779 100644
2024-04-28 07:27:51 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java
2024-10-24 17:16:31 +02:00
@@ -291,7 +291,9 @@ public final class CraftItemStack extends ItemStack {
2024-07-23 10:11:00 +02:00
@Override
public void removeEnchantments() {
2024-09-27 19:58:11 +02:00
- this.handle.remove(DataComponents.ENCHANTMENTS);
2024-07-23 10:11:00 +02:00
+ if (this.handle != null) { // Paper - fix NPE
2024-09-27 19:58:11 +02:00
+ this.handle.set(DataComponents.ENCHANTMENTS, ItemEnchantments.EMPTY); // Paper - set to default instead of removing the component
2024-07-23 10:11:00 +02:00
+ } // Paper
}
@Override
2024-10-24 17:16:31 +02:00
@@ -353,7 +355,14 @@ public final class CraftItemStack extends ItemStack {
2024-04-28 07:27:51 +02:00
// 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
2024-09-15 21:39:53 +02:00
+ void skullCallback(final net.minecraft.world.item.component.ResolvableProfile profile) {
+ itemStack.set(DataComponents.PROFILE, profile);
2024-04-28 07:27:51 +02:00
+ }
+ };
+ // Paper end - support updating profile after resolving it
((CraftMetaItem) itemMeta).applyToItem(tag);
itemStack.applyComponents(tag.build());
}
2024-10-24 17:16:31 +02:00
@@ -401,15 +410,20 @@ public final class CraftItemStack extends ItemStack {
2024-06-13 16:45:27 +02:00
if (itemMeta == null) return true;
2024-04-28 07:27:51 +02:00
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
2024-09-15 21:39:53 +02:00
+ void skullCallback(final net.minecraft.world.item.component.ResolvableProfile resolvableProfile) {
+ item.set(DataComponents.PROFILE, resolvableProfile);
2024-04-28 07:27:51 +02:00
+ }
+ };
+ // Paper end - support updating profile after resolving it
((CraftMetaItem) itemMeta).applyToItem(tag);
2024-05-26 01:37:35 +02:00
- item.restorePatch(tag.build());
- }
2024-05-26 00:58:56 +02:00
- // 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());
2024-05-26 01:37:35 +02:00
+ item.restorePatch(DataComponentPatch.EMPTY); // Paper - properly apply the new patch from itemmeta
+ item.applyComponents(tag.build()); // Paper - properly apply the new patch from itemmeta
}
2024-05-26 00:58:56 +02:00
+ // 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
2024-06-15 18:28:18 +02:00
index 169fefb64e1af444f7c2efb1234cb6e7779fb717..cb49ff5c94f33f00f626a31d958d2025819d1da8 100644
2024-05-26 00:58:56 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaAxolotlBucket.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaAxolotlBucket.java
2024-06-15 18:28:18 +02:00
@@ -118,14 +118,13 @@ public class CraftMetaAxolotlBucket extends CraftMetaItem implements AxolotlBuck
2024-05-26 00:58:56 +02:00
@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();
}
2024-04-28 07:27:51 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBanner.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBanner.java
2024-08-17 21:39:11 +02:00
index d0a8cd89da3b8d87248494056470c306f8fb5ae8..fdc0c1d73bb523f003e4169589f1002375b9c88c 100644
2024-04-28 07:27:51 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBanner.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBanner.java
2024-06-15 18:28:18 +02:00
@@ -69,6 +69,7 @@ public class CraftMetaBanner extends CraftMetaItem implements BannerMeta {
2024-04-28 07:27:51 +02:00
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) {
2024-04-28 21:34:11 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBlockState.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBlockState.java
2024-10-21 00:06:54 +02:00
index 413e41f113226b8a2e9b30bb519076d78e451fa0..d688339a57f0b4f12588ced0f7860a0d77eae728 100644
2024-04-28 21:34:11 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBlockState.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBlockState.java
2024-10-21 00:06:54 +02:00
@@ -52,10 +52,24 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta
2024-05-26 01:37:35 +02:00
@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;
2024-10-21 00:06:54 +02:00
- private BlockVector position;
2024-05-26 01:37:35 +02:00
+ // Paper start - store data separately
2024-06-03 19:39:20 +02:00
+ DataComponentMap components;
+ CustomData blockEntityTag;
+ {
+ // this is because the fields are possibly assigned in the super constructor (via deserializeInternal)
+ // and a direct field initialization happens **after** the super constructor. So we only want to
+ // set them to empty if they weren't assigned by the super constructor (via deserializeInternal)
+ this.components = this.components != null ? this.components : DataComponentMap.EMPTY;
+ this.blockEntityTag = this.blockEntityTag != null ? this.blockEntityTag : CustomData.EMPTY;
+ }
2024-05-26 01:37:35 +02:00
+ private Material materialForBlockEntityType() {
2024-08-09 22:05:50 +02:00
+ return this.material;
2024-05-26 01:37:35 +02:00
+ }
+ // Paper end
private CompoundTag internalTag;
CraftMetaBlockState(CraftMetaItem meta, Material material) {
2024-10-21 00:06:54 +02:00
@@ -64,47 +78,61 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta
2024-05-26 01:37:35 +02:00
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;
2024-10-21 00:06:54 +02:00
- this.position = te.position;
2024-05-26 01:37:35 +02:00
+ // Paper end
}
CraftMetaBlockState(DataComponentPatch tag, Material material, final Set<DataComponentType<?>> extraHandledDcts) { // Paper
super(tag, extraHandledDcts); // Paper
this.material = material;
2024-10-21 00:06:54 +02:00
- getOrEmpty(tag, CraftMetaBlockState.BLOCK_ENTITY_TAG).ifPresent((blockTag) -> {
- CompoundTag nbt = blockTag.copyTag();
2024-05-26 01:37:35 +02:00
+ // Paper start - move to separate method to be re-called
+ this.updateBlockState(tag);
+ }
2024-10-21 00:06:54 +02:00
- this.blockEntityTag = CraftMetaBlockState.getBlockState(material, nbt);
- if (nbt.contains("x", CraftMagicNumbers.NBT.TAG_ANY_NUMBER) && nbt.contains("y", CraftMagicNumbers.NBT.TAG_ANY_NUMBER) && nbt.contains("z", CraftMagicNumbers.NBT.TAG_ANY_NUMBER)) {
- this.position = new BlockVector(nbt.getInt("x"), nbt.getInt("y"), nbt.getInt("z"));
- }
2024-05-26 01:37:35 +02:00
+ private void updateBlockState(final DataComponentPatch tag) {
+ // Paper end
2024-10-21 00:06:54 +02:00
+ getOrEmpty(tag, CraftMetaBlockState.BLOCK_ENTITY_TAG).ifPresent((nbt) -> {
2024-05-26 01:37:35 +02:00
+ 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) {
2024-05-26 22:49:00 +02:00
+ if (CraftMetaItem.DEFAULT_HANDLED_DCTS.contains(type)) continue;
2024-05-26 01:37:35 +02:00
+ getOrEmpty(tag, type).ifPresent(value -> {
+ map.set(type, value);
+ });
+ }
+ // Paper end
}
+ this.components = map.build(); // Paper
}
}
2024-10-21 00:06:54 +02:00
@@ -118,42 +146,43 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta
2024-05-26 01:37:35 +02:00
this.material = Material.AIR;
}
2024-08-09 22:05:50 +02:00
if (this.internalTag != null) {
- this.blockEntityTag = CraftMetaBlockState.getBlockState(this.material, this.internalTag);
+ this.setBlockState(CraftMetaBlockState.getBlockState(this.material, this.internalTag)); // Paper - general item meta fixes - pass through setter
this.internalTag = null;
}
2024-10-21 00:06:54 +02:00
- this.position = SerializableMeta.getObject(BlockVector.class, map, "blockPosition", true);
+ // Paper start - general item meta fixes - parse spigot legacy position and merge into block entity tag
+ final BlockVector legacyPosition = SerializableMeta.getObject(BlockVector.class, map, "blockPosition", true);
+ if (legacyPosition != null) {
+ this.blockEntityTag = this.blockEntityTag.update(t -> {
+ if (t.isEmpty()) {
+ BlockEntity.addEntityType(t, java.util.Objects.requireNonNull(CraftBlockStates.getBlockEntityType(this.materialForBlockEntityType())));
+ }
+ t.putInt("x", legacyPosition.getBlockX());
+ t.putInt("y", legacyPosition.getBlockY());
+ t.putInt("z", legacyPosition.getBlockZ());
+ });
+ }
+ // Paper end - general item meta fixes - parse spigot legacy position and merge into block entity tag
2024-05-26 01:37:35 +02:00
}
2024-10-21 00:06:54 +02:00
@Override
2024-05-26 01:37:35 +02:00
void applyToItem(CraftMetaItem.Applicator tag) {
2024-04-28 21:34:11 +02:00
super.applyToItem(tag);
2024-10-21 00:06:54 +02:00
- CompoundTag nbt = null;
2024-05-26 01:37:35 +02:00
- if (this.blockEntityTag != null) {
2024-10-21 00:06:54 +02:00
- nbt = this.blockEntityTag.getItemNBT();
-
- for (TypedDataComponent<?> component : this.blockEntityTag.collectComponents()) {
- tag.putIfAbsent(component);
- }
2024-05-26 01:37:35 +02:00
+ // Paper start - accurately replicate logic for creating ItemStack from BlockEntity
+ // taken from BlockEntity#saveToItem and BlockItem#setBlockEntityData
+ final CompoundTag nbt = this.blockEntityTag.copyTag();
2024-05-29 21:22:51 +02:00
+ if (nbt.contains("id", CraftMagicNumbers.NBT.TAG_STRING)) {
+ tag.put(CraftMetaBlockState.BLOCK_ENTITY_TAG, CustomData.of(nbt));
+ } else if (!nbt.isEmpty()) {
2024-05-26 01:37:35 +02:00
+ BlockEntity.addEntityType(nbt, java.util.Objects.requireNonNull(CraftBlockStates.getBlockEntityType(this.materialForBlockEntityType())));
+ tag.put(CraftMetaBlockState.BLOCK_ENTITY_TAG, CustomData.of(nbt));
2024-10-21 00:06:54 +02:00
}
2024-04-28 21:34:11 +02:00
2024-10-21 00:06:54 +02:00
- if (this.position != null) {
- if (nbt == null) {
- nbt = new CompoundTag();
2024-05-26 01:37:35 +02:00
- }
2024-10-21 00:06:54 +02:00
-
- nbt.putInt("x", this.position.getBlockX());
- nbt.putInt("y", this.position.getBlockY());
- nbt.putInt("z", this.position.getBlockZ());
- }
-
- if (nbt != null && !nbt.isEmpty()) {
- CraftBlockEntityState<?> tile = (this.blockEntityTag != null) ? this.blockEntityTag : CraftMetaBlockState.getBlockState(this.material, null);
- // See ItemBlock#setBlockEntityData
- tile.addEntityType(nbt);
-
- tag.put(CraftMetaBlockState.BLOCK_ENTITY_TAG, CustomData.of(nbt));
2024-05-26 01:37:35 +02:00
+ 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
2024-10-21 00:06:54 +02:00
@@ -162,23 +191,35 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta
2024-05-26 01:37:35 +02:00
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));
2024-10-21 00:06:54 +02:00
+ }
2024-05-26 01:37:35 +02:00
+ 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();
2024-10-21 00:06:54 +02:00
}
2024-05-26 01:37:35 +02:00
+ // 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
2024-10-21 00:06:54 +02:00
}
2024-05-26 01:37:35 +02:00
+ 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);
2024-10-21 00:06:54 +02:00
+ }
2024-05-26 01:37:35 +02:00
+ // Paper end - new serialization format
}
@Override
2024-10-21 00:06:54 +02:00
ImmutableMap.Builder<String, Object> serialize(ImmutableMap.Builder<String, Object> builder) {
super.serialize(builder);
builder.put("blockMaterial", this.material.name());
- if (this.position != null) {
- builder.put("blockPosition", this.position);
- }
return builder;
}
@@ -186,12 +227,10 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta
2024-05-26 01:37:35 +02:00
int applyHash() {
final int original;
int hash = original = super.applyHash();
- if (this.blockEntityTag != null) {
- hash = 61 * hash + this.blockEntityTag.hashCode();
- }
2024-10-21 00:06:54 +02:00
- if (this.position != null) {
- hash = 61 * hash + this.position.hashCode();
- }
2024-05-26 01:37:35 +02:00
+ // Paper start
+ hash = 61 * hash + this.blockEntityTag.hashCode();
+ hash = 61 * hash + this.components.hashCode();
+ // Paper end
return original != hash ? CraftMetaBlockState.class.hashCode() ^ hash : hash;
}
2024-10-21 00:06:54 +02:00
@@ -203,52 +242,75 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta
2024-05-26 01:37:35 +02:00
if (meta instanceof CraftMetaBlockState) {
CraftMetaBlockState that = (CraftMetaBlockState) meta;
2024-10-21 00:06:54 +02:00
- return Objects.equal(this.blockEntityTag, that.blockEntityTag) && Objects.equal(this.position, that.position);
2024-05-26 01:37:35 +02:00
+ return Objects.equal(this.blockEntityTag, that.blockEntityTag) && Objects.equal(this.components, that.components); // Paper
}
return true;
}
2024-10-21 00:06:54 +02:00
boolean isBlockStateEmpty() {
- return !(this.blockEntityTag != null || this.position != null);
+ return !(this.blockEntityTag != null);
}
2024-05-26 01:37:35 +02:00
@Override
boolean notUncommon(CraftMetaItem meta) {
2024-10-21 00:06:54 +02:00
- return super.notUncommon(meta) && (meta instanceof CraftMetaBlockState || this.isBlockStateEmpty());
2024-05-26 01:37:35 +02:00
+ return super.notUncommon(meta) && (meta instanceof CraftMetaBlockState || (this.blockEntityTag.isEmpty() && this.components.isEmpty())); // Paper
}
@Override
boolean isEmpty() {
2024-10-21 00:06:54 +02:00
- return super.isEmpty() && this.isBlockStateEmpty();
2024-05-26 01:37:35 +02:00
+ return super.isEmpty() && this.blockEntityTag.isEmpty() && this.components.isEmpty(); // Paper
}
@Override
public CraftMetaBlockState clone() {
CraftMetaBlockState meta = (CraftMetaBlockState) super.clone();
- if (this.blockEntityTag != null) {
- meta.blockEntityTag = this.blockEntityTag.copy();
- }
2024-10-21 00:06:54 +02:00
- if (this.position != null) {
- meta.position = this.position.clone();
- }
2024-05-26 01:37:35 +02:00
+ // 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);
2024-05-26 22:49:00 +02:00
+ final Applicator applicator = new Applicator() {};
+ super.applyToItem(applicator);
+ patchedMap.applyPatch(applicator.build());
2024-05-26 01:37:35 +02:00
+ 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
2024-05-11 23:48:37 +02:00
}
2024-05-26 01:37:35 +02:00
private static CraftBlockEntityState<?> getBlockState(Material material, CompoundTag blockEntityTag) {
2024-10-21 00:06:54 +02:00
@@ -278,7 +340,23 @@ public class CraftMetaBlockState extends CraftMetaItem implements BlockStateMeta
2024-05-26 01:37:35 +02:00
Class<?> blockStateType = CraftBlockStates.getBlockStateType(stateMaterial);
2024-08-09 22:05:50 +02:00
Preconditions.checkArgument(blockStateType == blockState.getClass() && blockState instanceof CraftBlockEntityState, "Invalid blockState for %s", this.material);
2024-05-05 21:56:46 +02:00
2024-05-26 01:37:35 +02:00
- this.blockEntityTag = (CraftBlockEntityState<?>) blockState;
2024-05-05 21:56:46 +02:00
+ // Paper start - when a new BlockState is set, the components from that block entity
+ // have to be used to update the fields on CraftMetaItem
2024-05-26 01:37:35 +02:00
+ 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();
2024-05-05 21:56:46 +02:00
+ patchedMap.setAll(map);
2024-05-29 21:22:51 +02:00
+ if (!data.isEmpty()) {
+ patchedMap.set(BLOCK_ENTITY_TAG.TYPE, CustomData.of(data));
+ }
2024-05-26 01:37:35 +02:00
+ final DataComponentPatch patch = patchedMap.asPatch();
+ this.updateFromPatch(patch, null);
2024-05-26 07:36:55 +02:00
+ // 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;
2024-05-26 01:37:35 +02:00
+ this.updateBlockState(patch);
2024-05-05 21:56:46 +02:00
+ // Paper end
}
2024-08-09 22:05:50 +02:00
private static Material shieldToBannerHack(CompoundTag tag) {
2024-05-26 00:58:56 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBook.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBook.java
2024-07-23 10:11:00 +02:00
index 257c835bc280eee9ee73ae75b5249bb568a687d0..70f20de37c1f8d57a8d9fe00dcd864fdd9948ec2 100644
2024-05-26 00:58:56 +02:00
--- 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
2024-07-14 15:33:52 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBookSigned.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBookSigned.java
2024-07-23 10:11:00 +02:00
index cbb3d80cc7cd81b2505dff999a0baede737165f7..040dac82e484cb44b3afd444b4bbd1fd994bfe7c 100644
2024-07-14 15:33:52 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBookSigned.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBookSigned.java
@@ -124,13 +124,13 @@ public class CraftMetaBookSigned extends CraftMetaItem implements BookMeta {
void applyToItem(CraftMetaItem.Applicator itemData) {
super.applyToItem(itemData);
+ List<Filterable<Component>> list = new ArrayList<>(); // Paper - General ItemMeta Fixes
if (this.pages != null) {
- List<Filterable<Component>> list = new ArrayList<>();
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(this.title == null ? FilteredText.EMPTY : FilteredText.passThrough(this.title)), this.author == null ? "" : this.author, this.generation, list, this.resolved)); // Paper - General ItemMeta Fixes
}
@Override
2024-05-26 00:58:56 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBundle.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBundle.java
2024-06-15 18:28:18 +02:00
index 2736a87a6c481da0575e6e29ea08faa539c24378..51da0db4da3549efd69f367e28450408968fa8d0 100644
2024-05-26 00:58:56 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBundle.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBundle.java
2024-06-15 18:28:18 +02:00
@@ -41,7 +41,7 @@ public class CraftMetaBundle extends CraftMetaItem implements BundleMeta {
2024-05-26 00:58:56 +02:00
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);
}
});
2024-06-15 18:28:18 +02:00
@@ -54,7 +54,7 @@ public class CraftMetaBundle extends CraftMetaItem implements BundleMeta {
2024-05-26 00:58:56 +02:00
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);
}
}
2024-06-15 18:28:18 +02:00
@@ -110,7 +110,7 @@ public class CraftMetaBundle extends CraftMetaItem implements BundleMeta {
2024-05-26 00:58:56 +02:00
@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
2024-10-24 17:16:31 +02:00
index dc6398cfbd6e749733c3a681e1eb8ce15c3b2c5e..51d7263cdd34359d9cdf72cc01ba654b519f838d 100644
2024-05-26 00:58:56 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaColorableArmor.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaColorableArmor.java
2024-06-15 18:28:18 +02:00
@@ -11,16 +11,30 @@ import org.bukkit.inventory.meta.ColorableArmorMeta;
@DelegateDeserialization(SerializableMeta.class)
public class CraftMetaColorableArmor extends CraftMetaArmor implements ColorableArmorMeta {
2024-05-26 00:58:56 +02:00
- private Color color = DEFAULT_LEATHER_COLOR;
2024-05-26 19:57:38 +02:00
+ private Integer color; // Paper - keep color component consistent with vanilla (top byte is ignored)
2024-05-26 00:58:56 +02:00
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) {
2024-06-15 18:28:18 +02:00
@@ -31,7 +45,11 @@ public class CraftMetaColorableArmor extends CraftMetaArmor implements Colorable
2024-05-26 00:58:56 +02:00
@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
2024-06-15 18:28:18 +02:00
@@ -52,16 +70,16 @@ public class CraftMetaColorableArmor extends CraftMetaArmor implements Colorable
2024-05-26 00:58:56 +02:00
@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
2024-06-15 18:28:18 +02:00
@@ -81,7 +99,7 @@ public class CraftMetaColorableArmor extends CraftMetaArmor implements Colorable
2024-05-26 04:55:51 +02:00
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;
}
2024-05-20 02:15:44 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaCompass.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaCompass.java
2024-08-25 22:25:13 +02:00
index 69a112b3a9726966aecbe687d905fd1a11cfa1e3..ab424926c282fb03eabd1eebd2b7980899ef28e3 100644
2024-05-20 02:15:44 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaCompass.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaCompass.java
2024-08-25 22:25:13 +02:00
@@ -31,11 +31,7 @@ public class CraftMetaCompass extends CraftMetaItem implements CompassMeta {
static final ItemMetaKey LODESTONE_POS_Z = new ItemMetaKey("LodestonePosZ");
static final ItemMetaKey LODESTONE_TRACKED = new ItemMetaKey("LodestoneTracked");
- private ResourceKey<net.minecraft.world.level.Level> lodestoneWorld;
- private int lodestoneX;
- private int lodestoneY;
- private int lodestoneZ;
2024-05-20 02:15:44 +02:00
- private boolean tracked = true;
2024-08-25 22:25:13 +02:00
+ private LodestoneTracker tracker; // Paper - use LodestoneTracker type
2024-05-20 02:15:44 +02:00
CraftMetaCompass(CraftMetaItem meta) {
super(meta);
2024-08-25 22:25:13 +02:00
@@ -43,24 +39,13 @@ public class CraftMetaCompass extends CraftMetaItem implements CompassMeta {
return;
}
CraftMetaCompass compassMeta = (CraftMetaCompass) meta;
- this.lodestoneWorld = compassMeta.lodestoneWorld;
- this.lodestoneX = compassMeta.lodestoneX;
- this.lodestoneY = compassMeta.lodestoneY;
- this.lodestoneZ = compassMeta.lodestoneZ;
- this.tracked = compassMeta.tracked;
+ this.tracker = compassMeta.tracker; // Paper - use LodestoneTracker type
}
CraftMetaCompass(DataComponentPatch tag, java.util.Set<net.minecraft.core.component.DataComponentType<?>> extraHandledDcts) { // Paper
super(tag, extraHandledDcts); // Paper
getOrEmpty(tag, CraftMetaCompass.LODESTONE_TARGET).ifPresent((lodestoneTarget) -> {
- lodestoneTarget.target().ifPresent((target) -> {
- this.lodestoneWorld = target.dimension();
- BlockPos pos = target.pos();
- this.lodestoneX = pos.getX();
- this.lodestoneY = pos.getY();
- this.lodestoneZ = pos.getZ();
- });
- this.tracked = lodestoneTarget.tracked();
+ this.tracker = lodestoneTarget; // Paper - use LodestoneTracker type
});
}
@@ -68,10 +53,13 @@ public class CraftMetaCompass extends CraftMetaItem implements CompassMeta {
super(map);
String lodestoneWorldString = SerializableMeta.getString(map, CraftMetaCompass.LODESTONE_POS_WORLD.BUKKIT, true);
if (lodestoneWorldString != null) {
- this.lodestoneWorld = ResourceKey.create(Registries.DIMENSION, ResourceLocation.tryParse(lodestoneWorldString));
- this.lodestoneX = (Integer) map.get(CraftMetaCompass.LODESTONE_POS_X.BUKKIT);
- this.lodestoneY = (Integer) map.get(CraftMetaCompass.LODESTONE_POS_Y.BUKKIT);
- this.lodestoneZ = (Integer) map.get(CraftMetaCompass.LODESTONE_POS_Z.BUKKIT);
+ // Paper start - use LodestoneTracker type
+ ResourceKey<net.minecraft.world.level.Level> lodestoneWorld = ResourceKey.create(Registries.DIMENSION, ResourceLocation.tryParse(lodestoneWorldString));
+ int lodestoneX = (Integer) map.get(CraftMetaCompass.LODESTONE_POS_X.BUKKIT);
+ int lodestoneY = (Integer) map.get(CraftMetaCompass.LODESTONE_POS_Y.BUKKIT);
+ int lodestoneZ = (Integer) map.get(CraftMetaCompass.LODESTONE_POS_Z.BUKKIT);
+ this.tracker = new LodestoneTracker(Optional.of(new GlobalPos(lodestoneWorld, new BlockPos(lodestoneX, lodestoneY, lodestoneZ))), true);
+ // Paper end - use LodestoneTracker type
} else {
// legacy
Location lodestone = SerializableMeta.getObject(Location.class, map, CraftMetaCompass.LODESTONE_POS.BUKKIT, true);
@@ -79,21 +67,22 @@ public class CraftMetaCompass extends CraftMetaItem implements CompassMeta {
2024-05-20 02:15:44 +02:00
this.setLodestone(lodestone);
}
}
- this.tracked = SerializableMeta.getBoolean(map, CraftMetaCompass.LODESTONE_TRACKED.BUKKIT);
2024-08-25 22:25:13 +02:00
+ // Paper start - use LodestoneTracker type
+ final Optional<Boolean> tracked = SerializableMeta.getObjectOptionally(Boolean.class, map, CraftMetaCompass.LODESTONE_TRACKED.BUKKIT, true);
+ final Optional<GlobalPos> trackedPos = this.tracker != null ? this.tracker.target() : Optional.empty();
+ tracked.ifPresent(isTracked -> this.tracker = new LodestoneTracker(trackedPos, isTracked));
+ // Paper end - use LodestoneTracker type
2024-05-20 02:15:44 +02:00
}
@Override
2024-08-25 22:25:13 +02:00
void applyToItem(CraftMetaItem.Applicator tag) {
super.applyToItem(tag);
- Optional<GlobalPos> target = Optional.empty();
- if (this.lodestoneWorld != null) {
- target = Optional.of(new GlobalPos(this.lodestoneWorld, new BlockPos(this.lodestoneX, this.lodestoneY, this.lodestoneZ)));
- }
-
- if (target.isPresent() || this.hasLodestoneTracked()) {
- tag.put(CraftMetaCompass.LODESTONE_TARGET, new LodestoneTracker(target, this.tracked));
+ // Paper start - use LodestoneTracker type
+ if (this.tracker != null) {
+ tag.put(CraftMetaCompass.LODESTONE_TARGET, this.tracker);
}
+ // Paper end - use LodestoneTracker type
2024-05-20 02:15:44 +02:00
}
2024-08-25 22:25:13 +02:00
@Override
@@ -102,7 +91,7 @@ public class CraftMetaCompass extends CraftMetaItem implements CompassMeta {
}
boolean isCompassEmpty() {
- return !(this.hasLodestone() || this.hasLodestoneTracked());
+ return this.tracker == null; // Paper - use LodestoneTracker type
}
@Override
@@ -113,58 +102,69 @@ public class CraftMetaCompass extends CraftMetaItem implements CompassMeta {
@Override
public boolean hasLodestone() {
- return this.lodestoneWorld != null;
+ return this.tracker != null && this.tracker.target().isPresent(); // Paper - use LodestoneTracker type
2024-05-20 02:15:44 +02:00
}
2024-08-25 22:25:13 +02:00
@Override
public Location getLodestone() {
- if (this.lodestoneWorld == null) {
+ if (this.tracker == null || this.tracker.target().isEmpty()) { // Paper - use LodestoneTracker type
return null;
}
- ServerLevel worldServer = MinecraftServer.getServer().getLevel(this.lodestoneWorld);
+ ServerLevel worldServer = MinecraftServer.getServer().getLevel(this.tracker.target().get().dimension()); // Paper - use LodestoneTracker type
World world = worldServer != null ? worldServer.getWorld() : null;
- return new Location(world, this.lodestoneX, this.lodestoneY, this.lodestoneZ); // world may be null here, if the referenced world is not loaded
+ return org.bukkit.craftbukkit.util.CraftLocation.toBukkit(this.tracker.target().get().pos(), world); // world may be null here, if the referenced world is not loaded // Paper - use LodestoneTracker type
}
@Override
public void setLodestone(Location lodestone) {
Preconditions.checkArgument(lodestone == null || lodestone.getWorld() != null, "world is null");
if (lodestone == null) {
- this.lodestoneWorld = null;
+ // Paper start - use LodestoneTracker type
+ if (this.tracker != null) {
+ this.tracker = new LodestoneTracker(java.util.Optional.empty(), this.tracker.tracked()); // Paper - use LodestoneTracker type
+ }
+ // Paper end - use LodestoneTracker type
} else {
- this.lodestoneWorld = ((CraftWorld) lodestone.getWorld()).getHandle().dimension();
- this.lodestoneX = lodestone.getBlockX();
- this.lodestoneY = lodestone.getBlockY();
- this.lodestoneZ = lodestone.getBlockZ();
+ // Paper start - use LodestoneTracker type
+ GlobalPos pos = GlobalPos.of(
+ ((CraftWorld) lodestone.getWorld()).getHandle().dimension(),
+ io.papermc.paper.util.MCUtil.toBlockPosition(lodestone)
+ );
+ boolean tracked = this.tracker == null || this.tracker.tracked();
+ this.tracker = new LodestoneTracker(Optional.of(pos), tracked);
+ // Paper end - use LodestoneTracker type
}
}
- boolean hasLodestoneTracked() {
- return !this.tracked;
- }
-
2024-05-20 02:15:44 +02:00
@Override
public boolean isLodestoneTracked() {
- return this.tracked;
2024-08-25 22:25:13 +02:00
+ return this.tracker != null && this.tracker.tracked(); // Paper - use LodestoneTracker type
}
@Override
public void setLodestoneTracked(boolean tracked) {
- this.tracked = tracked;
+ final Optional<GlobalPos> trackedPos = this.tracker != null ? this.tracker.target() : Optional.empty(); // Paper - use LodestoneTracker type
+ this.tracker = new LodestoneTracker(trackedPos, tracked); // Paper - use LodestoneTracker type
+ }
+
+ // Paper start - Add more lodestone compass methods
+ @Override
+ public boolean isLodestoneCompass() {
+ return this.tracker != null;
+ }
+
+ @Override
+ public void clearLodestone() {
+ this.tracker = null;
2024-05-20 02:15:44 +02:00
}
2024-08-25 22:25:13 +02:00
+ // Paper end - Add more lodestone compass methods
2024-05-20 02:15:44 +02:00
@Override
2024-08-25 22:25:13 +02:00
int applyHash() {
final int original;
int hash = original = super.applyHash();
- if (this.hasLodestone()) {
- hash = 73 * hash + this.lodestoneWorld.hashCode();
- hash = 73 * hash + this.lodestoneX;
- hash = 73 * hash + this.lodestoneY;
- hash = 73 * hash + this.lodestoneZ;
- }
- if (this.hasLodestoneTracked()) {
- hash = 73 * hash + (this.isLodestoneTracked() ? 1231 : 1237);
+ if (this.isLodestoneCompass()) {
+ hash = 73 * hash + this.tracker.hashCode(); // Paper - use LodestoneTracker type
}
return original != hash ? CraftMetaCompass.class.hashCode() ^ hash : hash;
@@ -178,10 +178,7 @@ public class CraftMetaCompass extends CraftMetaItem implements CompassMeta {
if (meta instanceof CraftMetaCompass) {
CraftMetaCompass that = (CraftMetaCompass) meta;
- return (this.hasLodestone() ? that.hasLodestone() && this.lodestoneWorld.equals(that.lodestoneWorld)
- && this.lodestoneX == that.lodestoneX && this.lodestoneY == that.lodestoneY
- && this.lodestoneZ == that.lodestoneZ : !that.hasLodestone())
- && this.tracked == that.tracked;
+ return java.util.Objects.equals(this.tracker, that.tracker); // Paper - use LodestoneTracker type
}
return true;
}
@@ -195,14 +192,16 @@ public class CraftMetaCompass extends CraftMetaItem implements CompassMeta {
Builder<String, Object> serialize(Builder<String, Object> builder) {
super.serialize(builder);
- if (this.hasLodestone()) {
- builder.put(CraftMetaCompass.LODESTONE_POS_WORLD.BUKKIT, this.lodestoneWorld.location().toString());
- builder.put(CraftMetaCompass.LODESTONE_POS_X.BUKKIT, this.lodestoneX);
- builder.put(CraftMetaCompass.LODESTONE_POS_Y.BUKKIT, this.lodestoneY);
- builder.put(CraftMetaCompass.LODESTONE_POS_Z.BUKKIT, this.lodestoneZ);
- }
- if (this.hasLodestoneTracked()) {
- builder.put(CraftMetaCompass.LODESTONE_TRACKED.BUKKIT, this.tracked);
+ if (this.isLodestoneCompass()) { // Paper - use LodestoneTracker type
+ // Paper start - use LodestoneTracker type
+ if (this.tracker.target().isPresent()) {
+ builder.put(CraftMetaCompass.LODESTONE_POS_WORLD.BUKKIT, this.tracker.target().get().dimension().location().toString());
+ builder.put(CraftMetaCompass.LODESTONE_POS_X.BUKKIT, this.tracker.target().get().pos().getX());
+ builder.put(CraftMetaCompass.LODESTONE_POS_Y.BUKKIT, this.tracker.target().get().pos().getY());
+ builder.put(CraftMetaCompass.LODESTONE_POS_Z.BUKKIT, this.tracker.target().get().pos().getZ());
+ }
+ builder.put(CraftMetaCompass.LODESTONE_TRACKED.BUKKIT, this.tracker.tracked());
+ // Paper end - use LodestoneTracker type
}
return builder;
2024-05-26 00:58:56 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaCrossbow.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaCrossbow.java
2024-06-15 18:28:18 +02:00
index 0807c2172c5a4bee675cef265a45a9350e98b880..88ea260fb84a5f8eaab3a23a9a65d0411215a6a1 100644
2024-05-26 00:58:56 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaCrossbow.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaCrossbow.java
2024-06-15 18:28:18 +02:00
@@ -117,7 +117,7 @@ public class CraftMetaCrossbow extends CraftMetaItem implements CrossbowMeta {
2024-05-26 00:58:56 +02:00
@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;
}
2024-04-28 05:57:42 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaFirework.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaFirework.java
2024-09-15 21:39:53 +02:00
index 566d893a413fd04b99e83dc2da8fe958a48492a8..a944803771d514572f94b4e98a6d4435a009c078 100644
2024-04-28 05:57:42 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaFirework.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaFirework.java
2024-08-17 21:39:11 +02:00
@@ -55,7 +55,7 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta {
2024-05-26 00:58:56 +02:00
this.power = that.power;
- if (that.hasEffects()) {
+ if (that.effects != null) { // Paper
this.effects = new ArrayList<>(that.effects);
}
}
2024-08-17 21:39:11 +02:00
@@ -88,7 +88,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
}
static FireworkEffect getEffect(FireworkExplosion explosion) {
@@ -98,19 +98,14 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta {
2024-05-26 00:58:56 +02:00
.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));
2024-05-26 19:57:38 +02:00
+ 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
2024-05-26 00:58:56 +02:00
}
for (int color : explosion.fadeColors()) {
- effect.withFade(Color.fromRGB(color));
+ effect.withFade(Color.fromRGB(color & 0xFFFFFF)); // Paper
}
return effect.build();
2024-08-17 21:39:11 +02:00
@@ -162,7 +157,7 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta {
2024-04-28 18:06:14 +02:00
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;
}
2024-08-17 21:39:11 +02:00
@@ -174,6 +169,15 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta {
2024-04-28 18:06:14 +02:00
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);
}
}
2024-09-15 21:39:53 +02:00
@@ -215,7 +219,7 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta {
2024-05-26 00:58:56 +02:00
}
boolean isFireworkEmpty() {
- return !(this.hasEffects() || this.hasPower());
+ return !(this.effects != null || this.hasPower()); // Paper - empty effects list should stay on the item
}
2024-09-15 21:39:53 +02:00
@Override
@@ -232,7 +236,7 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta {
2024-05-26 00:58:56 +02:00
if (meta instanceof CraftMetaFirework that) {
2024-08-17 21:39:11 +02:00
return (Objects.equals(this.power, that.power))
2024-05-26 00:58:56 +02:00
- && (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;
2024-09-15 21:39:53 +02:00
@@ -250,7 +254,7 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta {
2024-05-26 00:58:56 +02:00
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;
2024-09-15 21:39:53 +02:00
@@ -260,7 +264,7 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta {
2024-05-26 00:58:56 +02:00
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));
}
2024-09-15 21:39:53 +02:00
@@ -285,6 +289,7 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta {
2024-04-28 18:06:14 +02:00
@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>();
}
2024-09-15 21:39:53 +02:00
@@ -294,6 +299,10 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta {
2024-04-28 18:06:14 +02:00
@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;
}
2024-09-15 21:39:53 +02:00
@@ -312,7 +321,7 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta {
2024-04-28 18:06:14 +02:00
@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
}
2024-08-17 21:39:11 +02:00
@Override
2024-04-28 07:27:51 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java
2024-10-31 23:44:34 +01:00
index 14aab3a2adfde7a236610be2b928651bc01067f5..5eb1f0126c3d3bca9bf3e769bec4ec532cc0d9ba 100644
2024-04-28 07:27:51 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java
2024-10-24 17:16:31 +02:00
@@ -199,9 +199,10 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-04-28 07:27:51 +02:00
}
}
- static final class Applicator {
+ static abstract class Applicator { // Paper - support updating profile after resolving it
2024-05-11 23:48:37 +02:00
- private final DataComponentPatch.Builder builder = DataComponentPatch.builder();
+ final DataComponentPatch.Builder builder = DataComponentPatch.builder(); // Paper - private -> package-private
2024-09-15 21:39:53 +02:00
+ void skullCallback(net.minecraft.world.item.component.ResolvableProfile resolvableProfile) {} // Paper - support updating profile after resolving it
2024-04-28 07:27:51 +02:00
<T> Applicator put(ItemMetaKeyType<T> key, T value) {
this.builder.set(key.TYPE, value);
2024-10-24 17:16:31 +02:00
@@ -308,7 +309,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-06-17 21:12:42 +02:00
private CraftToolComponent tool;
2024-10-24 17:16:31 +02:00
private CraftEquippableComponent equippable;
2024-06-17 21:12:42 +02:00
private CraftJukeboxComponent jukebox;
- private int damage;
+ private Integer damage; // Paper - may not be set
private Integer maxDamage;
private static final Set<DataComponentType> HANDLED_TAGS = Sets.newHashSet();
2024-10-24 17:16:31 +02:00
@@ -341,7 +342,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-05-11 16:16:21 +02:00
this.enchantments = new EnchantmentMap(meta.enchantments); // Paper
}
- if (meta.hasAttributeModifiers()) {
+ if (meta.attributeModifiers != null) { // Paper
this.attributeModifiers = LinkedHashMultimap.create(meta.attributeModifiers);
}
2024-10-24 17:16:31 +02:00
@@ -390,6 +391,11 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-05-05 21:56:46 +02:00
}
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;
});
2024-10-24 17:16:31 +02:00
@@ -906,7 +912,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-05-11 18:14:16 +02:00
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()) {
2024-10-24 17:16:31 +02:00
@@ -1037,7 +1043,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-06-17 21:12:42 +02:00
itemTag.put(CraftMetaItem.JUKEBOX_PLAYABLE, this.jukebox.getHandle());
}
- if (this.hasDamage()) {
+ if (this.hasDamageValue()) { // Paper - preserve empty/0 damage
itemTag.put(CraftMetaItem.DAMAGE, this.damage);
}
2024-10-24 17:16:31 +02:00
@@ -1087,7 +1093,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-10-21 00:06:54 +02:00
}
void applyEnchantments(Map<Enchantment, Integer> enchantments, CraftMetaItem.Applicator tag, ItemMetaKeyType<ItemEnchantments> key, ItemFlag itemFlag) {
- if (enchantments == null && !this.hasItemFlag(itemFlag)) {
+ if (enchantments == null /*&& !this.hasItemFlag(itemFlag)*/) { // Paper - general item meta fixes - only emit enchantment component if enchantments are defined
return;
}
2024-10-24 17:16:31 +02:00
@@ -1104,10 +1110,8 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-05-10 21:05:06 +02:00
}
2024-05-05 18:08:55 +02:00
void applyModifiers(Multimap<Attribute, AttributeModifier> modifiers, CraftMetaItem.Applicator tag) {
2024-05-10 21:05:06 +02:00
- if (modifiers == null || modifiers.isEmpty()) {
2024-05-05 18:08:55 +02:00
- if (this.hasItemFlag(ItemFlag.HIDE_ATTRIBUTES)) {
- tag.put(CraftMetaItem.ATTRIBUTES, new ItemAttributeModifiers(Collections.emptyList(), false));
- }
2024-05-10 21:05:06 +02:00
+ if (modifiers == null/* || modifiers.isEmpty()*/) { // Paper - empty modifiers has a specific meaning, they should still be saved
2024-05-05 18:08:55 +02:00
+ // Paper - don't save ItemFlag if the underlying data isn't present
return;
}
2024-10-24 17:16:31 +02:00
@@ -1144,7 +1148,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-06-17 21:12:42 +02:00
@Overridden
boolean isEmpty() {
2024-10-24 17:16:31 +02:00
- return !(this.hasDisplayName() || this.hasItemName() || this.hasLocalizedName() || this.hasEnchants() || (this.lore != null) || this.hasCustomModelData() || this.hasEnchantable() || this.hasBlockData() || this.hasRepairCost() || !this.unhandledTags.build().isEmpty() || !this.removedTags.isEmpty() || !this.persistentDataContainer.isEmpty() || this.hideFlag != 0 || this.isHideTooltip() || this.hasTooltipStyle() || this.hasItemModel() || this.isUnbreakable() || this.hasEnchantmentGlintOverride() || this.isGlider() || this.hasDamageResistant() || this.hasMaxStackSize() || this.hasRarity() || this.hasUseRemainder() || this.hasUseCooldown() || this.hasFood() || this.hasTool() || this.hasJukeboxPlayable() || this.hasEquippable() || 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.hasEnchantable() || this.hasBlockData() || this.hasRepairCost() || !this.unhandledTags.build().isEmpty() || !this.removedTags.isEmpty() || !this.persistentDataContainer.isEmpty() || this.hideFlag != 0 || this.isHideTooltip() || this.hasTooltipStyle() || this.hasItemModel() || this.isUnbreakable() || this.hasEnchantmentGlintOverride() || this.isGlider() || this.hasDamageResistant() || this.hasMaxStackSize() || this.hasRarity() || this.hasUseRemainder() || this.hasUseCooldown() || this.hasFood() || this.hasTool() || this.hasJukeboxPlayable() || this.hasEquippable() || this.hasDamageValue() || this.hasMaxDamage() || this.hasAttributeModifiers() || this.customTag != null || this.canPlaceOnPredicates != null || this.canBreakPredicates != null); // Paper
2024-06-17 21:12:42 +02:00
}
// Paper start
2024-10-24 17:16:31 +02:00
@@ -1240,6 +1244,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-04-28 18:06:14 +02:00
@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
2024-10-24 17:16:31 +02:00
@@ -1298,7 +1303,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-06-15 17:26:55 +02:00
@Override
public void removeEnchantments() {
if (this.hasEnchants()) {
- this.enchantments.clear();
+ this.enchantments = null; // Paper - Correctly clear enchantments
}
}
2024-10-24 17:16:31 +02:00
@@ -1364,6 +1369,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-04-28 18:06:14 +02:00
// 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 {
2024-10-24 17:16:31 +02:00
@@ -1379,6 +1385,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-04-28 18:06:14 +02:00
// 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 {
2024-10-26 19:11:37 +02:00
@@ -1596,6 +1603,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
@Override
public void setUseRemainder(ItemStack useRemainder) {
+ Preconditions.checkArgument(useRemainder == null || !useRemainder.isEmpty(), "Item cannot be empty"); // Paper
this.useRemainder = useRemainder;
}
@@ -1606,7 +1614,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
@Override
public UseCooldownComponent getUseCooldown() {
- return (this.hasUseCooldown()) ? new CraftUseCooldownComponent(this.useCooldown) : new CraftUseCooldownComponent(new UseCooldown(0));
+ return (this.hasUseCooldown()) ? new CraftUseCooldownComponent(this.useCooldown) : new CraftUseCooldownComponent(new UseCooldown(1.0F)); // Paper - Create a valid use_cooldown component
}
@Override
@@ -1692,7 +1700,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-05-26 00:58:56 +02:00
@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) {
2024-10-26 19:11:37 +02:00
@@ -1705,6 +1713,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-05-26 00:58:56 +02:00
@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;
}
2024-10-26 19:11:37 +02:00
@@ -1712,22 +1721,33 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-05-26 00:58:56 +02:00
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()) {
2024-06-18 11:11:14 +02:00
Preconditions.checkArgument(!(entry.getValue().getKey().equals(modifier.getKey()) && entry.getKey() == attribute), "Cannot register AttributeModifier. Modifier is already applied! %s", modifier); // Paper - attribute modifiers with same namespaced key but on different attributes are fine
2024-05-26 00:58:56 +02:00
}
+ } // Paper
+ this.checkAttributeList(); // Paper - moved down
return this.attributeModifiers.put(attribute, modifier);
}
2024-07-16 20:36:07 +02:00
@Override
public void setAttributeModifiers(@Nullable Multimap<Attribute, AttributeModifier> attributeModifiers) {
- if (attributeModifiers == null || attributeModifiers.isEmpty()) {
+ // Paper start - distinguish between null and empty
+ if (attributeModifiers == null) {
+ this.attributeModifiers = null;
+ return;
+ }
+ if (attributeModifiers.isEmpty()) {
+ // Paper end - distinguish between null and empty
this.attributeModifiers = LinkedHashMultimap.create();
2024-05-26 00:58:56 +02:00
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()) {
2024-10-26 19:11:37 +02:00
@@ -1737,6 +1757,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-05-26 00:58:56 +02:00
iterator.remove();
continue;
}
+ this.checkAttributeList(); // Paper - moved down
this.attributeModifiers.put(next.getKey(), next.getValue());
}
}
2024-10-26 19:11:37 +02:00
@@ -1744,13 +1765,13 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-05-26 00:58:56 +02:00
@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();
2024-10-26 19:11:37 +02:00
@@ -1770,7 +1791,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-05-26 00:58:56 +02:00
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();
2024-10-26 19:11:37 +02:00
@@ -1792,7 +1813,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-04-28 07:27:51 +02:00
@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();
2024-10-24 17:16:31 +02:00
net.minecraft.nbt.Tag nbt = DataComponentPatch.CODEC.encodeStart(MinecraftServer.getDefaultRegistryAccess().createSerializationContext(NbtOps.INSTANCE), patch).getOrThrow();
2024-10-26 19:11:37 +02:00
@@ -1801,7 +1822,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-05-11 23:48:37 +02:00
@Override
public String getAsComponentString() {
- CraftMetaItem.Applicator tag = new CraftMetaItem.Applicator();
+ CraftMetaItem.Applicator tag = new CraftMetaItem.Applicator() {}; // Paper
this.applyToItem(tag);
DataComponentPatch patch = tag.build();
2024-10-26 19:11:37 +02:00
@@ -1841,6 +1862,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-05-11 16:16:21 +02:00
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;
2024-10-26 19:11:37 +02:00
@@ -1856,19 +1878,33 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-06-17 21:12:42 +02:00
@Override
public boolean hasDamage() {
- return this.damage > 0;
+ return this.damage != null && this.damage > 0; // Paper - null check
}
@Override
public int getDamage() {
- return this.damage;
+ return this.damage == null ? 0 : this.damage; // Paper - null check
}
2024-05-26 00:58:56 +02:00
@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;
}
2024-06-17 21:12:42 +02:00
+ // Paper start - preserve empty/0 damage
+ @Override
+ public boolean hasDamageValue() {
+ return this.damage != null;
+ }
+
+ @Override
+ public void resetDamage() {
+ this.damage = null;
+ }
+ // Paper end - preserve empty/0 damage
+
@Override
public boolean hasMaxDamage() {
return this.maxDamage != null;
2024-10-26 19:11:37 +02:00
@@ -1882,6 +1918,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-05-26 00:58:56 +02:00
@Override
public void setMaxDamage(Integer maxDamage) {
+ Preconditions.checkArgument(maxDamage == null || maxDamage > 0, "Max damage should be positive"); // Paper
this.maxDamage = maxDamage;
}
2024-10-26 19:11:37 +02:00
@@ -1914,7 +1951,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-10-24 17:16:31 +02:00
&& (this.hasEnchantable() ? that.hasEnchantable() && this.enchantableValue.equals(that.enchantableValue) : !that.hasEnchantable())
2024-05-11 16:16:21 +02:00
&& (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))
2024-06-15 18:28:18 +02:00
&& (this.removedTags.equals(that.removedTags))
2024-05-11 16:16:21 +02:00
&& (Objects.equals(this.customTag, that.customTag))
2024-10-26 19:11:37 +02:00
@@ -1935,7 +1972,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-06-17 21:12:42 +02:00
&& (this.hasTool() ? that.hasTool() && this.tool.equals(that.tool) : !that.hasTool())
2024-10-24 17:16:31 +02:00
&& (this.hasEquippable() ? that.hasEquippable() && this.equippable.equals(that.equippable) : !that.hasEquippable())
2024-06-17 21:12:42 +02:00
&& (this.hasJukeboxPlayable() ? that.hasJukeboxPlayable() && this.jukebox.equals(that.jukebox) : !that.hasJukeboxPlayable())
- && (this.hasDamage() ? that.hasDamage() && this.damage == that.damage : !that.hasDamage())
+ && (Objects.equals(this.damage, that.damage)) // Paper - preserve empty/0 damage
&& (this.hasMaxDamage() ? that.hasMaxDamage() && this.maxDamage.equals(that.maxDamage) : !that.hasMaxDamage())
&& (this.canPlaceOnPredicates != null ? that.canPlaceOnPredicates != null && this.canPlaceOnPredicates.equals(that.canPlaceOnPredicates) : that.canPlaceOnPredicates == null) // Paper
&& (this.canBreakPredicates != null ? that.canBreakPredicates != null && this.canBreakPredicates.equals(that.canBreakPredicates) : that.canBreakPredicates == null) // Paper
2024-10-26 19:11:37 +02:00
@@ -1988,9 +2025,9 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-06-13 16:45:27 +02:00
hash = 61 * hash + (this.hasTool() ? this.tool.hashCode() : 0);
2024-06-14 11:59:44 +02:00
hash = 61 * hash + (this.hasJukeboxPlayable() ? this.jukebox.hashCode() : 0);
2024-10-24 17:16:31 +02:00
hash = 61 * hash + (this.hasEquippable() ? this.equippable.hashCode() : 0);
2024-06-17 21:12:42 +02:00
- hash = 61 * hash + (this.hasDamage() ? this.damage : 0);
2024-05-26 00:58:56 +02:00
- hash = 61 * hash + (this.hasMaxDamage() ? 1231 : 1237);
2024-05-11 16:16:21 +02:00
- hash = 61 * hash + (this.hasAttributeModifiers() ? this.attributeModifiers.hashCode() : 0);
2024-06-17 21:12:42 +02:00
+ hash = 61 * hash + (this.hasDamageValue() ? this.damage : -1); // Paper - preserve empty/0 damage
2024-05-26 00:58:56 +02:00
+ hash = 61 * hash + (this.hasMaxDamage() ? this.maxDamage.hashCode() : 0); // Paper - max damage is not a boolean
2024-05-11 16:16:21 +02:00
+ 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;
2024-10-26 19:11:37 +02:00
@@ -2011,7 +2048,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-05-11 16:16:21 +02:00
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) {
2024-10-26 19:11:37 +02:00
@@ -2178,7 +2215,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-06-17 21:12:42 +02:00
builder.put(CraftMetaItem.JUKEBOX_PLAYABLE.BUKKIT, this.jukebox);
}
- if (this.hasDamage()) {
+ if (this.hasDamageValue()) { // Paper - preserve empty/0 damage
builder.put(CraftMetaItem.DAMAGE.BUKKIT, this.damage);
}
2024-10-26 19:11:37 +02:00
@@ -2279,7 +2316,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-05-11 18:14:16 +02:00
}
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;
}
2024-10-26 19:11:37 +02:00
@@ -2361,7 +2398,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-05-05 21:56:46 +02:00
// 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,
2024-10-26 19:11:37 +02:00
@@ -2437,7 +2474,12 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-05-26 01:37:35 +02:00
// 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();
}
2024-05-26 00:58:56 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaLeatherArmor.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaLeatherArmor.java
2024-10-24 17:16:31 +02:00
index e76749bffaf5a0bbd3a8d35f882edcc3598351b9..49889026661fb2a558e14569324016d637de27a0 100644
2024-05-26 00:58:56 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaLeatherArmor.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaLeatherArmor.java
2024-06-15 18:28:18 +02:00
@@ -18,16 +18,30 @@ class CraftMetaLeatherArmor extends CraftMetaItem implements LeatherArmorMeta {
2024-05-26 00:58:56 +02:00
static final ItemMetaKeyType<DyedItemColor> COLOR = new ItemMetaKeyType<>(DataComponents.DYED_COLOR, "color");
- private Color color = DEFAULT_LEATHER_COLOR;
2024-05-26 19:57:38 +02:00
+ private Integer color; // Paper - keep color component consistent with vanilla (top byte is ignored)
2024-05-26 00:58:56 +02:00
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) {
2024-06-15 18:28:18 +02:00
@@ -38,7 +52,11 @@ class CraftMetaLeatherArmor extends CraftMetaItem implements LeatherArmorMeta {
2024-05-26 00:58:56 +02:00
@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
2024-06-15 18:28:18 +02:00
@@ -66,16 +84,16 @@ class CraftMetaLeatherArmor extends CraftMetaItem implements LeatherArmorMeta {
2024-05-26 00:58:56 +02:00
@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
2024-06-15 18:28:18 +02:00
@@ -95,7 +113,7 @@ class CraftMetaLeatherArmor extends CraftMetaItem implements LeatherArmorMeta {
2024-05-26 04:55:51 +02:00
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;
}
2024-06-15 18:28:18 +02:00
@@ -115,14 +133,16 @@ class CraftMetaLeatherArmor extends CraftMetaItem implements LeatherArmorMeta {
2024-05-26 00:58:56 +02:00
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()) {
2024-06-15 18:28:18 +02:00
@@ -145,6 +165,7 @@ class CraftMetaLeatherArmor extends CraftMetaItem implements LeatherArmorMeta {
2024-05-26 00:58:56 +02:00
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
2024-06-15 18:28:18 +02:00
index 149356981e586e4f67d4543d3df94a2ea99333fc..06c72ed83dab9492b70bfd13f7f9c1a4cbef9e2f 100644
2024-05-26 00:58:56 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaMap.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaMap.java
2024-06-15 18:28:18 +02:00
@@ -29,7 +29,7 @@ class CraftMetaMap extends CraftMetaItem implements MapMeta {
2024-05-26 00:58:56 +02:00
private Integer mapId;
private byte scaling = CraftMetaMap.SCALING_EMPTY;
- private Color color;
2024-05-26 19:57:38 +02:00
+ private Integer color; // Paper - keep color component consistent with vanilla (top byte is ignored)
2024-05-26 00:58:56 +02:00
CraftMetaMap(CraftMetaItem meta) {
super(meta);
2024-06-15 18:28:18 +02:00
@@ -57,7 +57,7 @@ class CraftMetaMap extends CraftMetaItem implements MapMeta {
2024-05-26 00:58:56 +02:00
getOrEmpty(tag, CraftMetaMap.MAP_COLOR).ifPresent((mapColor) -> {
try {
- this.color = Color.fromRGB(mapColor.rgb());
+ this.color = mapColor.rgb(); // Paper
} catch (IllegalArgumentException ex) {
// Invalid colour
}
2024-06-15 18:28:18 +02:00
@@ -101,7 +101,7 @@ class CraftMetaMap extends CraftMetaItem implements MapMeta {
2024-05-26 00:58:56 +02:00
}
if (this.hasColor()) {
- tag.put(CraftMetaMap.MAP_COLOR, new MapItemColor(this.color.asRGB()));
+ tag.put(CraftMetaMap.MAP_COLOR, new MapItemColor(this.color)); // Paper
}
}
2024-06-15 18:28:18 +02:00
@@ -121,6 +121,7 @@ class CraftMetaMap extends CraftMetaItem implements MapMeta {
2024-05-26 00:58:56 +02:00
@Override
public int getMapId() {
+ Preconditions.checkState(this.hasMapId(), "Item does not have map associated - check hasMapId() first!"); // Paper - fix NPE
return this.mapId;
}
2024-06-15 18:28:18 +02:00
@@ -181,12 +182,12 @@ class CraftMetaMap extends CraftMetaItem implements MapMeta {
2024-05-26 00:58:56 +02:00
@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
2024-10-24 17:16:31 +02:00
index b118d8ecb505d187c02bb158f14df333f487a87f..fa1d1a7f37aadf2750f03a0e215fb25fa948f9aa 100644
2024-05-26 00:58:56 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaOminousBottle.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaOminousBottle.java
2024-10-24 17:16:31 +02:00
@@ -70,6 +70,7 @@ public class CraftMetaOminousBottle extends CraftMetaItem implements OminousBott
2024-05-26 00:58:56 +02:00
@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
2024-10-24 17:16:31 +02:00
index 6f0eebcaffa20337cf5a7f8485f891c690d948ae..18dc2e83dab0821e5129bd68361de189363823b3 100644
2024-05-26 00:58:56 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaPotion.java
2024-10-24 17:16:31 +02:00
@@ -38,7 +38,7 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
2024-05-26 00:58:56 +02:00
private PotionType type;
private List<PotionEffect> customEffects;
- private Color color;
2024-05-26 19:57:38 +02:00
+ private Integer color; // Paper - keep color component consistent with vanilla (top byte is ignored)
2024-10-24 17:16:31 +02:00
private String customName;
2024-05-26 00:58:56 +02:00
CraftMetaPotion(CraftMetaItem meta) {
2024-10-24 17:16:31 +02:00
@@ -63,7 +63,7 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
2024-05-26 00:58:56 +02:00
potionContents.customColor().ifPresent((customColor) -> {
try {
- this.color = Color.fromRGB(customColor);
+ this.color = customColor; // Paper
} catch (IllegalArgumentException ex) {
// Invalid colour
}
2024-10-24 17:16:31 +02:00
@@ -132,7 +132,7 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
2024-10-21 00:06:54 +02:00
}
2024-05-26 00:58:56 +02:00
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
2024-10-24 17:16:31 +02:00
Optional<String> customName = Optional.ofNullable(this.customName);
2024-05-26 00:58:56 +02:00
List<MobEffectInstance> effectList = new ArrayList<>();
2024-10-24 17:16:31 +02:00
@@ -297,12 +297,12 @@ class CraftMetaPotion extends CraftMetaItem implements PotionMeta {
2024-05-26 00:58:56 +02:00
@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
2024-08-09 22:05:50 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaShield.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaShield.java
2024-08-10 15:08:00 +02:00
index 967d8940aec0065bce496d5d7a8c73de5733bd2c..e229ca6acb6dbc3185f326f6653b3d66d835a9e5 100644
2024-08-09 22:05:50 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaShield.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaShield.java
@@ -28,17 +28,29 @@ public class CraftMetaShield extends CraftMetaItem implements ShieldMeta, BlockS
static final ItemMetaKeyType<net.minecraft.world.item.DyeColor> BASE_COLOR = new ItemMetaKeyType<>(DataComponents.BASE_COLOR, "Base", "base-color");
- private Banner banner;
+ // Paper start - general item meta fixes - decoupled base colour and patterns
+ private @org.jetbrains.annotations.Nullable List<Pattern> patterns;
+ private @org.jetbrains.annotations.Nullable DyeColor baseColor;
+
+ // An empty pattern list is the same as the default on the Shield item, and will hence not be present in the data components of the stack.
+ private boolean hasPatterns() {
+ return this.patterns != null && !this.patterns.isEmpty();
+ }
+ // Paper end - general item meta fixes - decoupled base colour and patterns
CraftMetaShield(CraftMetaItem meta) {
super(meta);
if (meta instanceof CraftMetaShield craftMetaShield) {
- if (craftMetaShield.banner != null) {
- this.banner = (Banner) craftMetaShield.banner.copy();
- }
+ // Paper start - general item meta fixes - decoupled base colour and patterns
+ if (craftMetaShield.patterns != null) this.patterns = new ArrayList<>(craftMetaShield.getPatterns());
+ if (craftMetaShield.baseColor != null) this.baseColor = craftMetaShield.baseColor;
+ // Paper end - general item meta fixes - decoupled base colour and patterns
} else if (meta instanceof CraftMetaBlockState state && state.hasBlockState() && state.getBlockState() instanceof Banner banner) {
- this.banner = (Banner) banner.copy();
+ // Paper start - general item meta fixes - decoupled base colour and patterns
+ this.patterns = banner.getPatterns();
+ this.baseColor = banner.getBaseColor();
+ // Paper end - general item meta fixes - decoupled base colour and patterns
}
}
@@ -46,7 +58,7 @@ public class CraftMetaShield extends CraftMetaItem implements ShieldMeta, BlockS
super(tag, extraHandledDcts); // Paper - improve checking handled tags in item meta
getOrEmpty(tag, CraftMetaShield.BASE_COLOR).ifPresent((color) -> {
- this.banner = CraftMetaShield.getBlockState(DyeColor.getByWoolData((byte) color.getId()));
+ this.baseColor = DyeColor.getByWoolData((byte) color.getId()); // Paper - general item meta fixes - decoupled base colour and patterns
});
getOrEmpty(tag, CraftMetaBanner.PATTERNS).ifPresent((entityTag) -> {
@@ -68,7 +80,7 @@ public class CraftMetaShield extends CraftMetaItem implements ShieldMeta, BlockS
String baseColor = SerializableMeta.getString(map, CraftMetaShield.BASE_COLOR.BUKKIT, true);
if (baseColor != null) {
- this.banner = CraftMetaShield.getBlockState(DyeColor.valueOf(baseColor));
+ this.baseColor = DyeColor.valueOf(baseColor); // Paper - general item meta fixes - decoupled base colour and patterns
}
Iterable<?> rawPatternList = SerializableMeta.getObject(Iterable.class, map, CraftMetaBanner.PATTERNS.BUKKIT, true);
@@ -86,13 +98,14 @@ public class CraftMetaShield extends CraftMetaItem implements ShieldMeta, BlockS
void applyToItem(CraftMetaItem.Applicator tag) {
super.applyToItem(tag);
- if (this.banner != null) {
- tag.put(CraftMetaShield.BASE_COLOR, net.minecraft.world.item.DyeColor.byId(this.banner.getBaseColor().getWoolData()));
-
- if (this.banner.numberOfPatterns() > 0) {
+ // Paper start - general item meta fixes - decoupled base colour and patterns
+ if (this.baseColor != null) tag.put(CraftMetaShield.BASE_COLOR, net.minecraft.world.item.DyeColor.byId(this.baseColor.getWoolData()));
+ if (this.patterns != null && !this.patterns.isEmpty()) {
+ {
+ // Paper end - general item meta fixes - decoupled base colour and patterns
List<BannerPatternLayers.Layer> newPatterns = new ArrayList<>();
- for (Pattern p : this.banner.getPatterns()) {
+ for (Pattern p : this.patterns) { // Paper - general item meta fixes - decoupled base colour and patterns
newPatterns.add(new BannerPatternLayers.Layer(CraftPatternType.bukkitToMinecraftHolder(p.getPattern()), net.minecraft.world.item.DyeColor.byId(p.getColor().getWoolData())));
}
@@ -103,108 +116,84 @@ public class CraftMetaShield extends CraftMetaItem implements ShieldMeta, BlockS
@Override
public List<Pattern> getPatterns() {
- if (this.banner == null) {
+ if (this.patterns == null) { // Paper - general item meta fixes - decoupled base colour and patterns
return new ArrayList<>();
}
- return this.banner.getPatterns();
+ return new ArrayList<>(this.patterns); // Paper - general item meta fixes - decoupled base colour and patterns
}
@Override
public void setPatterns(List<Pattern> patterns) {
- if (this.banner == null) {
- if (patterns.isEmpty()) {
- return;
- }
-
- this.banner = CraftMetaShield.getBlockState(null);
- }
-
- this.banner.setPatterns(patterns);
+ this.patterns = new ArrayList<>(patterns); // Paper - general item meta fixes - decoupled base colour and patterns
}
@Override
public void addPattern(Pattern pattern) {
- if (this.banner == null) {
- this.banner = CraftMetaShield.getBlockState(null);
- }
-
- this.banner.addPattern(pattern);
+ // Paper start - general item meta fixes - decoupled base colour and patterns
+ if (this.patterns == null) this.patterns = new ArrayList<>();
+ this.patterns.add(pattern);
+ // Paper end - general item meta fixes - decoupled base colour and patterns
}
@Override
public Pattern getPattern(int i) {
- if (this.banner == null) {
+ if (this.patterns == null) { // Paper - general item meta fixes - decoupled base colour and patterns
throw new IndexOutOfBoundsException(i);
}
- return this.banner.getPattern(i);
+ return this.patterns.get(i); // Paper - general item meta fixes - decoupled base colour and patterns
}
@Override
public Pattern removePattern(int i) {
- if (this.banner == null) {
+ if (this.patterns == null) { // Paper - general item meta fixes - decoupled base colour and patterns
throw new IndexOutOfBoundsException(i);
}
- return this.banner.removePattern(i);
+ return this.patterns.remove(i); // Paper - general item meta fixes - decoupled base colour and patterns
}
@Override
public void setPattern(int i, Pattern pattern) {
- if (this.banner == null) {
+ if (this.patterns == null) { // Paper - general item meta fixes - decoupled base colour and patterns
throw new IndexOutOfBoundsException(i);
}
- this.banner.setPattern(i, pattern);
+ this.patterns.set(i, pattern); // Paper - general item meta fixes - decoupled base colour and patterns
}
@Override
public int numberOfPatterns() {
- if (this.banner == null) {
+ if (this.patterns == null) { // Paper - general item meta fixes - decoupled base colour and patterns
return 0;
}
- return this.banner.numberOfPatterns();
+ return this.patterns.size(); // Paper - general item meta fixes - decoupled base colour and patterns
}
@Override
public DyeColor getBaseColor() {
- if (this.banner == null) {
- return null;
- }
-
- return this.banner.getBaseColor();
+ return this.baseColor; // Paper - general item meta fixes - decoupled base colour and patterns
}
@Override
public void setBaseColor(DyeColor baseColor) {
- if (baseColor == null) {
- if (this.banner.numberOfPatterns() > 0) {
- this.banner.setBaseColor(DyeColor.WHITE);
- } else {
- this.banner = null;
- }
- } else {
- if (this.banner == null) {
- this.banner = CraftMetaShield.getBlockState(baseColor);
- }
-
- this.banner.setBaseColor(baseColor);
- }
+ this.baseColor = baseColor; // Paper - general item meta fixes - decoupled base colour and patterns
}
@Override
ImmutableMap.Builder<String, Object> serialize(ImmutableMap.Builder<String, Object> builder) {
super.serialize(builder);
- if (this.banner != null) {
- builder.put(CraftMetaShield.BASE_COLOR.BUKKIT, this.banner.getBaseColor().toString());
-
- if (this.banner.numberOfPatterns() > 0) {
- builder.put(CraftMetaBanner.PATTERNS.BUKKIT, this.banner.getPatterns());
- }
+ // Paper start - general item meta fixes - decoupled base colour and patterns
+ if (this.baseColor != null) {
+ builder.put(CraftMetaShield.BASE_COLOR.BUKKIT, this.baseColor.toString());
+ }
+ if (hasPatterns()) {
+ builder.put(CraftMetaBanner.PATTERNS.BUKKIT, this.patterns);
}
+ // Paper end - general item meta fixes - decoupled base colour and patterns
return builder;
}
@@ -213,8 +202,13 @@ public class CraftMetaShield extends CraftMetaItem implements ShieldMeta, BlockS
int applyHash() {
final int original;
int hash = original = super.applyHash();
- if (this.banner != null) {
- hash = 61 * hash + this.banner.hashCode();
+ // Paper start - general item meta fixes - decoupled base colour and patterns
+ if (this.baseColor != null) {
+ hash = 61 * hash + this.baseColor.hashCode();
+ }
+ if (hasPatterns()) {
+ hash = 61 * hash + this.patterns.hashCode();
+ // Paper end - general item meta fixes - decoupled base colour and patterns
}
return original != hash ? CraftMetaShield.class.hashCode() ^ hash : hash;
}
@@ -225,29 +219,33 @@ public class CraftMetaShield extends CraftMetaItem implements ShieldMeta, BlockS
return false;
}
if (meta instanceof CraftMetaShield that) {
- return Objects.equal(this.banner, that.banner);
2024-08-10 15:08:00 +02:00
+ return Objects.equal(this.baseColor, that.baseColor) && Objects.equal(this.patterns, that.patterns); // Paper - general item meta fixes - decoupled base colour and patterns
2024-08-09 22:05:50 +02:00
}
return true;
}
@Override
boolean notUncommon(CraftMetaItem meta) {
- return super.notUncommon(meta) && (meta instanceof CraftMetaShield || this.banner == null);
+ return super.notUncommon(meta) && (meta instanceof CraftMetaShield || (this.baseColor == null && !hasPatterns())); // Paper - general item meta fixes - decoupled base colour and patterns
}
@Override
boolean isEmpty() {
- return super.isEmpty() && this.banner == null;
+ return super.isEmpty() && this.baseColor == null && !hasPatterns(); // Paper - general item meta fixes - decoupled base colour and patterns
}
@Override
public boolean hasBlockState() {
- return this.banner != null;
+ return this.baseColor != null || hasPatterns(); // Paper - general item meta fixes - decoupled base colour and patterns
}
@Override
public BlockState getBlockState() {
- return (this.banner != null) ? this.banner.copy() : CraftMetaShield.getBlockState(null);
+ // Paper start - general item meta fixes - decoupled base colour and patterns
+ final Banner banner = CraftMetaShield.getBlockState(this.baseColor);
+ if (this.patterns != null) banner.setPatterns(this.patterns);
+ return banner;
+ // Paper end - general item meta fixes - decoupled base colour and patterns
}
@Override
@@ -255,13 +253,18 @@ public class CraftMetaShield extends CraftMetaItem implements ShieldMeta, BlockS
Preconditions.checkArgument(blockState != null, "blockState must not be null");
Preconditions.checkArgument(blockState instanceof Banner, "Invalid blockState");
- this.banner = (Banner) blockState;
+ // Paper start - general item meta fixes - decoupled base colour and patterns
+ final Banner banner = (Banner) blockState;
+ this.baseColor = banner.getBaseColor();
+ this.patterns = banner.getPatterns();
+ // Paper end - general item meta fixes - decoupled base colour and patterns
}
// Paper start - add method to clear block state
@Override
public void clearBlockState() {
- this.banner = null;
+ this.baseColor = null;
+ this.patterns = null;
}
// Paper end - add method to clear block state
@@ -275,9 +278,10 @@ public class CraftMetaShield extends CraftMetaItem implements ShieldMeta, BlockS
@Override
public CraftMetaShield clone() {
CraftMetaShield meta = (CraftMetaShield) super.clone();
- if (this.banner != null) {
- meta.banner = (Banner) this.banner.copy();
- }
+ // Paper start - general item meta fixes - decoupled base colour and patterns
+ meta.baseColor = this.baseColor;
+ meta.patterns = this.patterns == null ? null : new ArrayList<>(this.patterns);
+ // Paper start - general item meta fixes - decoupled base colour and patterns
return meta;
}
2024-04-28 07:27:51 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java
2024-09-15 21:39:53 +02:00
index 302906467b12189e21633369c005736863f46dd5..5a4e5b8736ad2e2084e757e9d2034044e177d8f0 100644
2024-04-28 07:27:51 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java
2024-09-15 21:39:53 +02:00
@@ -112,10 +112,10 @@ class CraftMetaSkull extends CraftMetaItem implements SkullMeta {
2024-04-28 07:27:51 +02:00
// 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);
2024-09-15 21:39:53 +02:00
- tag.put(CraftMetaSkull.SKULL_PROFILE, this.profile);
2024-04-28 07:27:51 +02:00
- });
+ tag.skullCallback(this.profile); // Paper - actually set profile on itemstack
+ }, ((org.bukkit.craftbukkit.CraftServer) org.bukkit.Bukkit.getServer()).getServer()); // Paper - run on main thread
}
}
2024-05-26 00:58:56 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSpawnEgg.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSpawnEgg.java
2024-07-23 10:11:00 +02:00
index 8ddf091b3ff1262b6c97e8fe72e0a80db5e1037d..be92ccda66f514459773916cc16b6c230e863444 100644
2024-05-26 00:58:56 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSpawnEgg.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSpawnEgg.java
2024-06-15 18:28:18 +02:00
@@ -121,6 +121,7 @@ public class CraftMetaSpawnEgg extends CraftMetaItem implements SpawnEggMeta {
2024-05-26 00:58:56 +02:00
@Override
public EntitySnapshot getSpawnedEntity() {
+ if (this.entityTag == null) return null; // Paper - fix NPE
return CraftEntitySnapshot.create(this.entityTag);
}
2024-06-15 18:28:18 +02:00
@@ -138,7 +139,7 @@ public class CraftMetaSpawnEgg extends CraftMetaItem implements SpawnEggMeta {
2024-05-26 00:58:56 +02:00
if (meta instanceof CraftMetaSpawnEgg) {
CraftMetaSpawnEgg that = (CraftMetaSpawnEgg) meta;
2024-06-13 16:45:27 +02:00
- 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
2024-05-26 00:58:56 +02:00
}
return true;
}
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaTropicalFishBucket.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaTropicalFishBucket.java
2024-06-15 18:28:18 +02:00
index 17705059b81942e4df43a4a5180092e09c985ade..80e6b85a107d5236edba99540cb5074e2dc1485a 100644
2024-05-26 00:58:56 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaTropicalFishBucket.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaTropicalFishBucket.java
2024-06-15 18:28:18 +02:00
@@ -120,6 +120,7 @@ class CraftMetaTropicalFishBucket extends CraftMetaItem implements TropicalFishB
2024-05-26 00:58:56 +02:00
@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);
}
2024-06-15 18:28:18 +02:00
@@ -133,6 +134,7 @@ class CraftMetaTropicalFishBucket extends CraftMetaItem implements TropicalFishB
2024-05-26 00:58:56 +02:00
@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);
}
2024-06-15 18:28:18 +02:00
@@ -146,6 +148,7 @@ class CraftMetaTropicalFishBucket extends CraftMetaItem implements TropicalFishB
2024-05-26 00:58:56 +02:00
@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);
}
2024-04-28 14:48:20 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/SerializableMeta.java b/src/main/java/org/bukkit/craftbukkit/inventory/SerializableMeta.java
2024-08-09 22:05:50 +02:00
index e00b757d6059715e8697428008fcb3e6e7abbe2e..dcf02bd0f7f4c67f5ab98003cc932b960704eef1 100644
2024-04-28 14:48:20 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/SerializableMeta.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/SerializableMeta.java
2024-08-09 22:05:50 +02:00
@@ -136,4 +136,21 @@ public final class SerializableMeta implements ConfigurationSerializable {
2024-04-28 14:48:20 +02:00
}
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
}
2024-10-26 19:11:37 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/components/CraftEquippableComponent.java b/src/main/java/org/bukkit/craftbukkit/inventory/components/CraftEquippableComponent.java
index 1f1589bcdb9e3abde7d25eac65326df1e7ed75ab..9b4c2502e45af4e7a58ff352f0f99c34d233f755 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/components/CraftEquippableComponent.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/components/CraftEquippableComponent.java
@@ -170,7 +170,7 @@ public final class CraftEquippableComponent implements EquippableComponent {
@Override
public void setAllowedEntities(Tag<EntityType> tag) {
- Preconditions.checkArgument(tag instanceof CraftEntityTag, "tag must be an entity tag");
+ Preconditions.checkArgument(tag == null || tag instanceof CraftEntityTag, "tag must be an entity tag"); // Paper
this.handle = new Equippable(this.handle.slot(), this.handle.equipSound(), this.handle.model(), this.handle.cameraOverlay(),
(tag != null) ? Optional.of(((CraftEntityTag) tag).getHandle()) : Optional.empty(),
2024-06-20 18:32:32 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/components/CraftToolComponent.java b/src/main/java/org/bukkit/craftbukkit/inventory/components/CraftToolComponent.java
2024-10-24 17:16:31 +02:00
index 9a4a1d903c22e9d2a0cbda95ceb8b1dcfb29112e..4f7914f96207feda67cd910213bb624df4802a06 100644
2024-06-20 18:32:32 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/components/CraftToolComponent.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/components/CraftToolComponent.java
2024-10-24 17:16:31 +02:00
@@ -106,6 +106,7 @@ public final class CraftToolComponent implements ToolComponent {
2024-06-20 18:32:32 +02:00
public ToolRule addRule(Material block, Float speed, Boolean correctForDrops) {
Preconditions.checkArgument(block != null, "block must not be null");
Preconditions.checkArgument(block.isBlock(), "block must be a block type, given %s", block.getKey());
+ Preconditions.checkArgument(speed == null || speed > 0, "speed must be positive"); // Paper - validate speed
Holder.Reference<Block> nmsBlock = CraftBlockType.bukkitToMinecraft(block).builtInRegistryHolder();
return this.addRule(HolderSet.direct(nmsBlock), speed, correctForDrops);
2024-10-24 17:16:31 +02:00
@@ -113,6 +114,7 @@ public final class CraftToolComponent implements ToolComponent {
2024-06-20 18:32:32 +02:00
@Override
public ToolRule addRule(Collection<Material> blocks, Float speed, Boolean correctForDrops) {
+ Preconditions.checkArgument(speed == null || speed > 0, "speed must be positive"); // Paper - validate speed
List<Holder.Reference<Block>> nmsBlocks = new ArrayList<>(blocks.size());
for (Material material : blocks) {
2024-10-24 17:16:31 +02:00
@@ -126,6 +128,7 @@ public final class CraftToolComponent implements ToolComponent {
2024-06-20 18:32:32 +02:00
@Override
public ToolRule addRule(Tag<Material> tag, Float speed, Boolean correctForDrops) {
Preconditions.checkArgument(tag instanceof CraftBlockTag, "tag must be a block tag");
+ Preconditions.checkArgument(speed == null || speed > 0, "speed must be positive"); // Paper - validate speed
return this.addRule(((CraftBlockTag) tag).getHandle(), speed, correctForDrops);
}
2024-10-24 17:16:31 +02:00
@@ -258,6 +261,7 @@ public final class CraftToolComponent implements ToolComponent {
2024-06-20 18:32:32 +02:00
@Override
public void setSpeed(Float speed) {
+ Preconditions.checkArgument(speed == null || speed > 0, "speed must be positive"); // Paper - validate speed
this.handle = new Tool.Rule(this.handle.blocks(), Optional.ofNullable(speed), this.handle.correctForDrops());
}
2024-10-26 19:11:37 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/components/CraftUseCooldownComponent.java b/src/main/java/org/bukkit/craftbukkit/inventory/components/CraftUseCooldownComponent.java
index 952e7fa022161d1d21ba1a4f43abe5ee221dee4b..0f86ff23cc6a2f617fc5ef67e576e1e3cb29034c 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/components/CraftUseCooldownComponent.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/components/CraftUseCooldownComponent.java
@@ -55,7 +55,7 @@ public final class CraftUseCooldownComponent implements UseCooldownComponent {
@Override
public void setCooldownSeconds(float eatSeconds) {
- Preconditions.checkArgument(eatSeconds >= 0, "eatSeconds cannot be less than 0");
+ Preconditions.checkArgument(eatSeconds > 0, "eatSeconds must be positive"); // Paper
this.handle = new UseCooldown(eatSeconds, this.handle.cooldownGroup());
}
2024-04-28 14:20:52 +02:00
diff --git a/src/test/java/org/bukkit/craftbukkit/inventory/DeprecatedItemMetaCustomValueTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/DeprecatedItemMetaCustomValueTest.java
2024-10-21 00:06:54 +02:00
index 9cc1ef5c9221dd7d2069b280f0c91ce9439a995a..1c80fe7549d70ae16c7b755c22752549261f072a 100644
2024-04-28 14:20:52 +02:00
--- a/src/test/java/org/bukkit/craftbukkit/inventory/DeprecatedItemMetaCustomValueTest.java
+++ b/src/test/java/org/bukkit/craftbukkit/inventory/DeprecatedItemMetaCustomValueTest.java
2024-10-21 00:06:54 +02:00
@@ -94,7 +94,7 @@ public class DeprecatedItemMetaCustomValueTest {
2024-04-28 14:20:52 +02:00
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
2024-10-21 00:06:54 +02:00
index 130c4500a5e854480962c8f720b1df4c67d43c33..f33b49915d1f1f0838c49ac943e8d4d619450f6b 100644
2024-04-28 14:20:52 +02:00
--- a/src/test/java/org/bukkit/craftbukkit/inventory/PersistentDataContainerTest.java
+++ b/src/test/java/org/bukkit/craftbukkit/inventory/PersistentDataContainerTest.java
2024-10-21 00:06:54 +02:00
@@ -128,7 +128,7 @@ public class PersistentDataContainerTest {
2024-04-28 14:20:52 +02:00
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
2024-10-21 00:06:54 +02:00
@@ -474,7 +474,7 @@ public class PersistentDataContainerTest {
2024-04-28 14:20:52 +02:00
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