Skip to content

Commit

Permalink
distinguish between null and empty map in API
Browse files Browse the repository at this point in the history
  • Loading branch information
Machine-Maker authored and Bjarne Koll committed Jun 1, 2024
1 parent 5d8e53d commit 4673bc2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 20 deletions.
16 changes: 16 additions & 0 deletions patches/api/0481-General-ItemMeta-fixes.patch
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,19 @@ index 637fa73d4366c2d88e2716e5c8d3465706d788a7..0f03c546ecdd6383fb36a362d18d6fb5
this.flicker = flicker;
this.trail = trail;
this.colors = colors;
diff --git a/src/main/java/org/bukkit/inventory/meta/ItemMeta.java b/src/main/java/org/bukkit/inventory/meta/ItemMeta.java
index a317111df3b6cda00dc700c68f4c2b789c51885d..58fe78232e8e00466c641b2ff0388dc4d38b9c26 100644
--- a/src/main/java/org/bukkit/inventory/meta/ItemMeta.java
+++ b/src/main/java/org/bukkit/inventory/meta/ItemMeta.java
@@ -611,8 +611,9 @@ public interface ItemMeta extends Cloneable, ConfigurationSerializable, Persiste

/**
* Set all {@link Attribute}s and their {@link AttributeModifier}s.
- * To clear all currently set Attributes and AttributeModifiers use
- * null or an empty Multimap.
+ * To clear all custom attribute modifiers, use {@code null}. To set
+ * no modifiers (which will override the default modifiers), use an
+ * empty map.
* If not null nor empty, this will filter all entries that are not-null
* and add them to the ItemStack.
*
46 changes: 28 additions & 18 deletions patches/server/1037-General-ItemMeta-fixes.patch
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ index 8e2b3dd109dca3089cbce82cd3788874613a3230..893efb2c4a07c33d41e934279dd914a9
// CraftBukkit end

diff --git a/src/main/java/net/minecraft/world/level/block/entity/BlockEntity.java b/src/main/java/net/minecraft/world/level/block/entity/BlockEntity.java
index 65170cbb50d8d5030fc5e33b6389c554aec6ae31..6349f2e0a5ba30d250f5ffe43771f325c0999a76 100644
index 139cc0123921bf981d10334d9bd7378d19ec5f3b..c0563260277f9f4bd9ff08993b2efb4bca9a0c60 100644
--- a/src/main/java/net/minecraft/world/level/block/entity/BlockEntity.java
+++ b/src/main/java/net/minecraft/world/level/block/entity/BlockEntity.java
@@ -140,6 +140,11 @@ public abstract class BlockEntity {
Expand Down Expand Up @@ -832,7 +832,7 @@ index 8e0dd4b7a7a25a8beb27b507047bc48d8227627c..77489c3ffaa3a72d4cf105499a77150f
}
}
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java
index 12a193db7475870e5107c86c7611bb4b92feacb8..c235b80b94fdb6c77766016114713cd501ffd67c 100644
index 12a193db7475870e5107c86c7611bb4b92feacb8..8738e49b2a31ab100fed8b3ecc48e7960916af0d 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java
@@ -172,9 +172,10 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
Expand Down Expand Up @@ -950,7 +950,7 @@ index 12a193db7475870e5107c86c7611bb4b92feacb8..c235b80b94fdb6c77766016114713cd5
return this.attributeModifiers.containsKey(attribute) ? ImmutableList.copyOf(this.attributeModifiers.get(attribute)) : null;
}

@@ -1341,10 +1349,12 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
@@ -1341,22 +1349,33 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
public boolean addAttributeModifier(@Nonnull Attribute attribute, @Nonnull AttributeModifier modifier) {
Preconditions.checkNotNull(attribute, "Attribute cannot be null");
Preconditions.checkNotNull(modifier, "AttributeModifier cannot be null");
Expand All @@ -964,7 +964,17 @@ index 12a193db7475870e5107c86c7611bb4b92feacb8..c235b80b94fdb6c77766016114713cd5
return this.attributeModifiers.put(attribute, modifier);
}

@@ -1355,8 +1365,11 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
@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();
return;
}

Expand All @@ -978,15 +988,15 @@ index 12a193db7475870e5107c86c7611bb4b92feacb8..c235b80b94fdb6c77766016114713cd5

Iterator<Map.Entry<Attribute, AttributeModifier>> iterator = attributeModifiers.entries().iterator();
while (iterator.hasNext()) {
@@ -1366,6 +1379,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
@@ -1366,6 +1385,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
iterator.remove();
continue;
}
+ this.checkAttributeList(); // Paper - moved down
this.attributeModifiers.put(next.getKey(), next.getValue());
}
}
@@ -1373,13 +1387,13 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
@@ -1373,13 +1393,13 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
@Override
public boolean removeAttributeModifier(@Nonnull Attribute attribute) {
Preconditions.checkNotNull(attribute, "Attribute cannot be null");
Expand All @@ -1002,7 +1012,7 @@ index 12a193db7475870e5107c86c7611bb4b92feacb8..c235b80b94fdb6c77766016114713cd5
int removed = 0;
Iterator<Map.Entry<Attribute, AttributeModifier>> iter = this.attributeModifiers.entries().iterator();

@@ -1399,7 +1413,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
@@ -1399,7 +1419,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
public boolean removeAttributeModifier(@Nonnull Attribute attribute, @Nonnull AttributeModifier modifier) {
Preconditions.checkNotNull(attribute, "Attribute cannot be null");
Preconditions.checkNotNull(modifier, "AttributeModifier cannot be null");
Expand All @@ -1011,7 +1021,7 @@ index 12a193db7475870e5107c86c7611bb4b92feacb8..c235b80b94fdb6c77766016114713cd5
int removed = 0;
Iterator<Map.Entry<Attribute, AttributeModifier>> iter = this.attributeModifiers.entries().iterator();

@@ -1421,7 +1435,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
@@ -1421,7 +1441,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {

@Override
public String getAsString() {
Expand All @@ -1020,7 +1030,7 @@ index 12a193db7475870e5107c86c7611bb4b92feacb8..c235b80b94fdb6c77766016114713cd5
this.applyToItem(tag);
DataComponentPatch patch = tag.build();
Tag nbt = DataComponentPatch.CODEC.encodeStart(MinecraftServer.getDefaultRegistryAccess().createSerializationContext(NbtOps.INSTANCE), patch).getOrThrow();
@@ -1430,7 +1444,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
@@ -1430,7 +1450,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {

@Override
public String getAsComponentString() {
Expand All @@ -1029,15 +1039,15 @@ index 12a193db7475870e5107c86c7611bb4b92feacb8..c235b80b94fdb6c77766016114713cd5
this.applyToItem(tag);
DataComponentPatch patch = tag.build();

@@ -1470,6 +1484,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
@@ -1470,6 +1490,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
if (first == null || second == null) {
return false;
}
+ if (first.isEmpty() && second.isEmpty()) return true; // Paper - empty modifiers are equivalent
for (Map.Entry<Attribute, AttributeModifier> entry : first.entries()) {
if (!second.containsEntry(entry.getKey(), entry.getValue())) {
return false;
@@ -1495,6 +1510,8 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
@@ -1495,6 +1516,8 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {

@Override
public void setDamage(int damage) {
Expand All @@ -1046,15 +1056,15 @@ index 12a193db7475870e5107c86c7611bb4b92feacb8..c235b80b94fdb6c77766016114713cd5
this.damage = damage;
}

@@ -1511,6 +1528,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
@@ -1511,6 +1534,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {

@Override
public void setMaxDamage(Integer maxDamage) {
+ Preconditions.checkArgument(maxDamage == null || maxDamage > 0, "Max damage should be positive"); // Paper
this.maxDamage = maxDamage;
}

@@ -1542,7 +1560,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
@@ -1542,7 +1566,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
&& (this.hasCustomModelData() ? that.hasCustomModelData() && this.customModelData.equals(that.customModelData) : !that.hasCustomModelData())
&& (this.hasBlockData() ? that.hasBlockData() && this.blockData.equals(that.blockData) : !that.hasBlockData())
&& (this.hasRepairCost() ? that.hasRepairCost() && this.repairCost == that.repairCost : !that.hasRepairCost())
Expand All @@ -1063,7 +1073,7 @@ index 12a193db7475870e5107c86c7611bb4b92feacb8..c235b80b94fdb6c77766016114713cd5
&& (this.unhandledTags.equals(that.unhandledTags))
&& (Objects.equals(this.customTag, that.customTag))
&& (this.persistentDataContainer.equals(that.persistentDataContainer))
@@ -1598,8 +1616,8 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
@@ -1598,8 +1622,8 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
hash = 61 * hash + (this.hasRarity() ? this.rarity.hashCode() : 0);
hash = 61 * hash + (this.hasFood() ? this.food.hashCode() : 0);
hash = 61 * hash + (this.hasDamage() ? this.damage : 0);
Expand All @@ -1074,7 +1084,7 @@ index 12a193db7475870e5107c86c7611bb4b92feacb8..c235b80b94fdb6c77766016114713cd5
hash = 61 * hash + (this.canPlaceOnPredicates != null ? this.canPlaceOnPredicates.hashCode() : 0); // Paper
hash = 61 * hash + (this.canBreakPredicates != null ? this.canBreakPredicates.hashCode() : 0); // Paper
hash = 61 * hash + this.version;
@@ -1619,7 +1637,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
@@ -1619,7 +1643,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
if (this.enchantments != null) {
clone.enchantments = new EnchantmentMap(this.enchantments); // Paper
}
Expand All @@ -1083,7 +1093,7 @@ index 12a193db7475870e5107c86c7611bb4b92feacb8..c235b80b94fdb6c77766016114713cd5
clone.attributeModifiers = LinkedHashMultimap.create(this.attributeModifiers);
}
if (this.customTag != null) {
@@ -1823,7 +1841,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
@@ -1823,7 +1847,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
}

static void serializeModifiers(Multimap<Attribute, AttributeModifier> modifiers, ImmutableMap.Builder<String, Object> builder, ItemMetaKey key) {
Expand All @@ -1092,7 +1102,7 @@ index 12a193db7475870e5107c86c7611bb4b92feacb8..c235b80b94fdb6c77766016114713cd5
return;
}

@@ -1905,7 +1923,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
@@ -1905,7 +1929,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
// Paper start - improve checking handled tags
@org.jetbrains.annotations.VisibleForTesting
public static final Map<Class<? extends CraftMetaItem>, Set<DataComponentType<?>>> HANDLED_DCTS_PER_TYPE = new HashMap<>();
Expand All @@ -1101,7 +1111,7 @@ index 12a193db7475870e5107c86c7611bb4b92feacb8..c235b80b94fdb6c77766016114713cd5
CraftMetaItem.NAME.TYPE,
CraftMetaItem.ITEM_NAME.TYPE,
CraftMetaItem.LORE.TYPE,
@@ -1971,7 +1989,12 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
@@ -1971,7 +1995,12 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
// Paper end - improve checking handled data component types

protected static <T> Optional<? extends T> getOrEmpty(DataComponentPatch tag, ItemMetaKeyType<T> type) {
Expand Down
4 changes: 2 additions & 2 deletions patches/server/1045-Fix-equipment-slot-and-group-API.patch
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ index 9d74577af071954e1e37201a96368c1360076209..eafa54c870c3e2aef30c3f9f96f51660
throw new IllegalArgumentException("Not implemented. This is a bug");
}
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java
index c235b80b94fdb6c77766016114713cd501ffd67c..d5789326d70bb8b029c5448270bbaa6faf52e6e1 100644
index 8738e49b2a31ab100fed8b3ecc48e7960916af0d..2b586d1fd649fcb62c7c62c8a96b222be1826ac3 100644
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java
@@ -1331,7 +1331,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
Expand All @@ -44,7 +44,7 @@ index c235b80b94fdb6c77766016114713cd501ffd67c..d5789326d70bb8b029c5448270bbaa6f
result.put(entry.getKey(), entry.getValue());
}
}
@@ -1399,9 +1399,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
@@ -1405,9 +1405,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {

while (iter.hasNext()) {
Map.Entry<Attribute, AttributeModifier> entry = iter.next();
Expand Down

0 comments on commit 4673bc2

Please sign in to comment.