2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Thu, 28 May 2015 23:00:19 -0400
Subject: [PATCH] Handle Item Meta Inconsistencies
First, Enchantment order would blow away seeing 2 items as the same,
however the Client forces enchantment list in a certain order, as well
as does the /enchant command. Anvils can insert it into forced order,
causing 2 same items to be considered different.
This change makes unhandled NBT Tags and Enchantments use a sorted tree map,
so they will always be in a consistent order.
Additionally, the old enchantment API was never updated when ItemMeta
was added, resulting in 2 different ways to modify an items enchantments.
For consistency, the old API methods now forward to use the
ItemMeta API equivalents, and should deprecate the old API's.
2024-04-24 03:25:14 +02:00
diff --git a/src/main/java/net/minecraft/world/item/enchantment/ItemEnchantments.java b/src/main/java/net/minecraft/world/item/enchantment/ItemEnchantments.java
index b49eb019cce58049c2b3a0e80e3d08998b16f7ea..dd6bf199f22476ef1d0f704883b748be97b0065e 100644
--- a/src/main/java/net/minecraft/world/item/enchantment/ItemEnchantments.java
+++ b/src/main/java/net/minecraft/world/item/enchantment/ItemEnchantments.java
@@ -27,15 +27,21 @@ import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.component.TooltipProvider;
+// Paper start
+import it.unimi.dsi.fastutil.objects.Object2IntAVLTreeMap;
+// Paper end
2021-06-11 14:02:28 +02:00
2024-04-24 03:25:14 +02:00
public class ItemEnchantments implements TooltipProvider {
- public static final ItemEnchantments EMPTY = new ItemEnchantments(new Object2IntOpenHashMap<>(), true);
2021-06-11 14:02:28 +02:00
+ // Paper start
2024-04-24 03:25:14 +02:00
+ private static final java.util.Comparator<Holder<Enchantment>> ENCHANTMENT_ORDER = java.util.Comparator.comparing(Holder::getRegisteredName);
+ public static final ItemEnchantments EMPTY = new ItemEnchantments(new Object2IntAVLTreeMap<>(ENCHANTMENT_ORDER), true);
2021-06-11 14:02:28 +02:00
+ // Paper end
2024-04-24 03:25:14 +02:00
public static final int MAX_LEVEL = 255;
private static final Codec<Integer> LEVEL_CODEC = Codec.intRange(0, 255);
- private static final Codec<Object2IntOpenHashMap<Holder<Enchantment>>> LEVELS_CODEC = Codec.unboundedMap(
+ private static final Codec<Object2IntAVLTreeMap<Holder<Enchantment>>> LEVELS_CODEC = Codec.unboundedMap( // Paper
BuiltInRegistries.ENCHANTMENT.holderByNameCodec(), LEVEL_CODEC
)
- .xmap(Object2IntOpenHashMap::new, Function.identity());
+ .xmap(Object2IntAVLTreeMap::new, Function.identity()); // Paper
private static final Codec<ItemEnchantments> FULL_CODEC = RecordCodecBuilder.create(
instance -> instance.group(
LEVELS_CODEC.fieldOf("levels").forGetter(component -> component.enchantments),
@@ -45,16 +51,16 @@ public class ItemEnchantments implements TooltipProvider {
);
public static final Codec<ItemEnchantments> CODEC = Codec.withAlternative(FULL_CODEC, LEVELS_CODEC, map -> new ItemEnchantments(map, true));
public static final StreamCodec<RegistryFriendlyByteBuf, ItemEnchantments> STREAM_CODEC = StreamCodec.composite(
- ByteBufCodecs.map(Object2IntOpenHashMap::new, ByteBufCodecs.holderRegistry(Registries.ENCHANTMENT), ByteBufCodecs.VAR_INT),
+ ByteBufCodecs.map((v) -> new Object2IntAVLTreeMap<>(ENCHANTMENT_ORDER), ByteBufCodecs.holderRegistry(Registries.ENCHANTMENT), ByteBufCodecs.VAR_INT), // Paper
component -> component.enchantments,
ByteBufCodecs.BOOL,
component -> component.showInTooltip,
ItemEnchantments::new
);
- final Object2IntOpenHashMap<Holder<Enchantment>> enchantments;
+ final Object2IntAVLTreeMap<Holder<Enchantment>> enchantments; // Paper
public final boolean showInTooltip;
2021-12-06 21:28:36 +01:00
2024-04-24 03:25:14 +02:00
- ItemEnchantments(Object2IntOpenHashMap<Holder<Enchantment>> enchantments, boolean showInTooltip) {
+ ItemEnchantments(Object2IntAVLTreeMap<Holder<Enchantment>> enchantments, boolean showInTooltip) { // Paper
this.enchantments = enchantments;
this.showInTooltip = showInTooltip;
2021-07-07 08:52:40 +02:00
2024-04-24 03:25:14 +02:00
@@ -145,7 +151,7 @@ public class ItemEnchantments implements TooltipProvider {
2021-06-11 14:02:28 +02:00
}
2024-04-24 03:25:14 +02:00
public static class Mutable {
- private final Object2IntOpenHashMap<Holder<Enchantment>> enchantments = new Object2IntOpenHashMap<>();
+ private final Object2IntAVLTreeMap<Holder<Enchantment>> enchantments = new Object2IntAVLTreeMap<>(ENCHANTMENT_ORDER); // Paper
public boolean showInTooltip;
public Mutable(ItemEnchantments enchantmentsComponent) {
2021-06-11 14:02:28 +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-04-25 23:21:18 +02:00
index b6521462d193bff83ace1dc694c6d957a7173969..d302767e8f01fdfcba9c22e2e35677afc18c0641 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java
2024-04-24 03:25:14 +02:00
@@ -191,16 +191,11 @@ public final class CraftItemStack extends ItemStack {
2021-06-11 14:02:28 +02:00
public void addUnsafeEnchantment(Enchantment ench, int level) {
2023-06-13 01:51:45 +02:00
Preconditions.checkArgument(ench != null, "Enchantment cannot be null");
2021-06-11 14:02:28 +02:00
2021-06-12 02:57:04 +02:00
- if (!CraftItemStack.makeTag(this.handle)) {
2021-06-11 14:02:28 +02:00
- return;
- }
2024-04-24 03:25:14 +02:00
- ItemEnchantments list = CraftItemStack.getEnchantmentList(this.handle);
2021-06-11 14:02:28 +02:00
- if (list == null) {
2024-04-24 03:25:14 +02:00
- list = ItemEnchantments.EMPTY;
2021-06-11 14:02:28 +02:00
- }
2024-04-24 03:25:14 +02:00
- ItemEnchantments.Mutable listCopy = new ItemEnchantments.Mutable(list);
- listCopy.set(CraftEnchantment.bukkitToMinecraft(ench), level);
- this.handle.set(DataComponents.ENCHANTMENTS, listCopy.toImmutable());
2021-06-11 14:02:28 +02:00
+ // Paper start - Replace whole method
2021-06-12 02:57:04 +02:00
+ final ItemMeta itemMeta = this.getItemMeta();
2021-06-11 14:02:28 +02:00
+ itemMeta.addEnchant(ench, level, true);
2021-06-12 02:57:04 +02:00
+ this.setItemMeta(itemMeta);
2021-06-11 14:02:28 +02:00
+ // Paper end
}
static boolean makeTag(net.minecraft.world.item.ItemStack item) {
2024-04-24 03:25:14 +02:00
@@ -229,24 +224,15 @@ public final class CraftItemStack extends ItemStack {
2021-06-11 14:02:28 +02:00
public int removeEnchantment(Enchantment ench) {
2023-06-13 01:51:45 +02:00
Preconditions.checkArgument(ench != null, "Enchantment cannot be null");
2021-06-11 14:02:28 +02:00
2024-04-24 03:25:14 +02:00
- ItemEnchantments list = CraftItemStack.getEnchantmentList(this.handle);
2021-06-11 14:02:28 +02:00
- if (list == null) {
- return 0;
2023-03-23 22:57:03 +01:00
- }
2024-04-24 03:25:14 +02:00
- int level = this.getEnchantmentLevel(ench);
- if (level <= 0) {
2021-06-11 14:02:28 +02:00
- return 0;
- }
2024-04-24 03:25:14 +02:00
- int size = list.size();
-
2021-06-11 14:02:28 +02:00
- if (size == 1) {
2024-04-24 03:25:14 +02:00
- this.handle.remove(DataComponents.ENCHANTMENTS);
2021-06-11 14:02:28 +02:00
- return level;
2023-03-23 22:57:03 +01:00
+ // Paper start - replace entire method
+ int level = getEnchantmentLevel(ench);
+ if (level > 0) {
+ final ItemMeta itemMeta = this.getItemMeta();
+ if (itemMeta == null) return 0;
+ itemMeta.removeEnchant(ench);
+ this.setItemMeta(itemMeta);
}
2024-04-24 03:25:14 +02:00
-
- ItemEnchantments.Mutable listCopy = new ItemEnchantments.Mutable(list);
- listCopy.set(CraftEnchantment.bukkitToMinecraft(ench), -1); // Negative to remove
- this.handle.set(DataComponents.ENCHANTMENTS, listCopy.toImmutable());
2021-06-11 14:02:28 +02:00
+ // Paper end
return level;
}
2024-04-24 03:25:14 +02:00
@@ -258,7 +244,7 @@ public final class CraftItemStack extends ItemStack {
2021-06-11 14:02:28 +02:00
@Override
public Map<Enchantment, Integer> getEnchantments() {
2021-06-12 02:57:04 +02:00
- return CraftItemStack.getEnchantments(this.handle);
+ return this.hasItemMeta() ? this.getItemMeta().getEnchants() : ImmutableMap.<Enchantment, Integer>of(); // Paper - use Item Meta
2021-06-11 14:02:28 +02:00
}
static Map<Enchantment, Integer> getEnchantments(net.minecraft.world.item.ItemStack item) {
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java
2024-04-25 23:21:18 +02:00
index d8804be1f36872d12484d9b4327d669d19e7c573..122142125dcba97572b1e0b9195e534a0e2257d9 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java
@@ -6,6 +6,7 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.LinkedHashMultimap;
+import com.google.common.collect.ImmutableSortedMap; // Paper
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.google.common.collect.SetMultimap;
2024-04-24 03:25:14 +02:00
@@ -22,6 +23,7 @@ import java.util.Arrays;
2021-11-23 13:15:10 +01:00
import java.util.Base64;
2021-06-11 14:02:28 +02:00
import java.util.Collection;
2024-04-24 03:25:14 +02:00
import java.util.Collections;
2021-06-11 14:02:28 +02:00
+import java.util.Comparator; // Paper
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Iterator;
2024-04-24 03:25:14 +02:00
@@ -32,6 +34,7 @@ import java.util.Map;
2021-06-11 14:02:28 +02:00
import java.util.Objects;
2024-04-24 03:25:14 +02:00
import java.util.Optional;
2021-06-11 14:02:28 +02:00
import java.util.Set;
+import java.util.TreeMap; // Paper
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nonnull;
2024-04-24 03:25:14 +02:00
@@ -221,7 +224,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
private List<Component> lore; // null and empty are two different states internally
2021-06-11 14:02:28 +02:00
private Integer customModelData;
2024-04-24 03:25:14 +02:00
private Map<String, String> blockData;
2021-06-11 14:02:28 +02:00
- private Map<Enchantment, Integer> enchantments;
+ private EnchantmentMap enchantments; // Paper
private Multimap<Attribute, AttributeModifier> attributeModifiers;
private int repairCost;
private int hideFlag;
2024-04-24 03:25:14 +02:00
@@ -260,7 +263,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2021-06-11 14:02:28 +02:00
this.blockData = meta.blockData;
2023-12-25 23:51:56 +01:00
if (meta.enchantments != null) {
2021-06-11 14:02:28 +02:00
- this.enchantments = new LinkedHashMap<Enchantment, Integer>(meta.enchantments);
+ this.enchantments = new EnchantmentMap(meta.enchantments); // Paper
}
if (meta.hasAttributeModifiers()) {
2024-04-24 03:25:14 +02:00
@@ -385,8 +388,8 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2021-06-11 14:02:28 +02:00
}
}
2024-04-24 03:25:14 +02:00
- static Map<Enchantment, Integer> buildEnchantments(ItemEnchantments tag) {
- Map<Enchantment, Integer> enchantments = new LinkedHashMap<Enchantment, Integer>(tag.size());
+ static EnchantmentMap buildEnchantments(ItemEnchantments tag) { // Paper
2021-06-11 14:02:28 +02:00
+ EnchantmentMap enchantments = new EnchantmentMap(); // Paper
2024-04-24 03:25:14 +02:00
tag.entrySet().forEach((entry) -> {
Holder<net.minecraft.world.item.enchantment.Enchantment> id = entry.getKey();
@@ -640,13 +643,13 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
return modifiers;
2021-06-11 14:02:28 +02:00
}
- static Map<Enchantment, Integer> buildEnchantments(Map<String, Object> map, ItemMetaKey key) {
+ static EnchantmentMap buildEnchantments(Map<String, Object> map, ItemMetaKey key) { // Paper
Map<?, ?> ench = SerializableMeta.getObject(Map.class, map, key.BUKKIT, true);
if (ench == null) {
return null;
}
- Map<Enchantment, Integer> enchantments = new LinkedHashMap<Enchantment, Integer>(ench.size());
+ EnchantmentMap enchantments = new EnchantmentMap(); // Paper
for (Map.Entry<?, ?> entry : ench.entrySet()) {
// Doctor older enchants
String enchantKey = entry.getKey().toString();
2024-04-24 03:25:14 +02:00
@@ -951,14 +954,14 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2021-06-11 14:02:28 +02:00
@Override
public Map<Enchantment, Integer> getEnchants() {
2023-10-27 01:34:58 +02:00
- return this.hasEnchants() ? ImmutableMap.copyOf(this.enchantments) : ImmutableMap.<Enchantment, Integer>of();
2021-06-12 02:57:04 +02:00
+ return this.hasEnchants() ? ImmutableSortedMap.copyOfSorted(this.enchantments) : ImmutableMap.<Enchantment, Integer>of(); // Paper
2021-06-11 14:02:28 +02:00
}
@Override
public boolean addEnchant(Enchantment ench, int level, boolean ignoreRestrictions) {
2023-06-13 01:51:45 +02:00
Preconditions.checkArgument(ench != null, "Enchantment cannot be null");
2021-06-12 02:57:04 +02:00
if (this.enchantments == null) {
- this.enchantments = new LinkedHashMap<Enchantment, Integer>(4);
+ this.enchantments = new EnchantmentMap(); // Paper
2021-06-11 14:02:28 +02:00
}
if (ignoreRestrictions || level >= ench.getStartLevel() && level <= ench.getMaxLevel()) {
2024-04-24 03:25:14 +02:00
@@ -1492,7 +1495,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2021-06-11 14:02:28 +02:00
clone.customModelData = this.customModelData;
clone.blockData = this.blockData;
if (this.enchantments != null) {
- clone.enchantments = new LinkedHashMap<Enchantment, Integer>(this.enchantments);
+ clone.enchantments = new EnchantmentMap(this.enchantments); // Paper
}
if (this.hasAttributeModifiers()) {
clone.attributeModifiers = LinkedHashMultimap.create(this.attributeModifiers);
2024-04-25 23:21:18 +02:00
@@ -1812,4 +1815,22 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-04-24 03:25:14 +02:00
return (result != null) ? result : Optional.empty();
2021-06-11 14:02:28 +02:00
}
+
+ // Paper start
+ private static class EnchantmentMap extends TreeMap<Enchantment, Integer> {
+ private EnchantmentMap(Map<Enchantment, Integer> enchantments) {
+ this();
+ putAll(enchantments);
+ }
+
+ private EnchantmentMap() {
+ super(Comparator.comparing(o -> o.getKey().toString()));
+ }
+
+ public EnchantmentMap clone() {
+ return (EnchantmentMap) super.clone();
+ }
+ }
+ // Paper end
+
}