2024-04-27 21:17:58 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Sat, 27 Apr 2024 12:16:38 -0700
2024-04-27 23:32:14 +02:00
Subject: [PATCH] Fix ItemFlags
Re-adds missing functionality for HIDE_DESTROYS and
HIDE_PLACED_ON. Also adds new flag in HIDE_STORED_ENCHANTS
which was split from HIDE_ADDITIONAL_TOOLTIP.
2024-04-27 21:17:58 +02:00
== AT ==
public net.minecraft.world.item.AdventureModePredicate predicates
2024-04-27 23:32:14 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaEnchantedBook.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaEnchantedBook.java
2024-06-15 18:28:18 +02:00
index 73fe41322e0349ad1d46a760f621b6c91112e90e..19af55ec2bf62b70bd3be44f499b32f5efe71ab1 100644
2024-04-27 23:32:14 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaEnchantedBook.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaEnchantedBook.java
2024-06-15 18:28:18 +02:00
@@ -38,7 +38,7 @@ class CraftMetaEnchantedBook extends CraftMetaItem implements EnchantmentStorage
2024-04-27 23:32:14 +02:00
getOrEmpty(tag, CraftMetaEnchantedBook.STORED_ENCHANTMENTS).ifPresent((itemEnchantments) -> {
this.enchantments = buildEnchantments(itemEnchantments);
if (!itemEnchantments.showInTooltip) {
- this.addItemFlags(ItemFlag.HIDE_ADDITIONAL_TOOLTIP);
+ this.addItemFlags(ItemFlag.HIDE_STORED_ENCHANTS); // Paper - new ItemFlag
}
});
}
2024-06-15 18:28:18 +02:00
@@ -53,7 +53,7 @@ class CraftMetaEnchantedBook extends CraftMetaItem implements EnchantmentStorage
2024-04-27 23:32:14 +02:00
void applyToItem(CraftMetaItem.Applicator itemTag) {
super.applyToItem(itemTag);
- this.applyEnchantments(this.enchantments, itemTag, CraftMetaEnchantedBook.STORED_ENCHANTMENTS, ItemFlag.HIDE_ADDITIONAL_TOOLTIP);
+ this.applyEnchantments(this.enchantments, itemTag, CraftMetaEnchantedBook.STORED_ENCHANTMENTS, ItemFlag.HIDE_STORED_ENCHANTS);
}
@Override
2024-04-27 21:17:58 +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-24 16:02:45 +02:00
index 8d13505c36d732f17293c6a6d65cac20919b8b7a..6b3eed94c26bc16177f9b9fadd140f9a89163af2 100644
2024-04-27 21:17:58 +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 16:02:45 +02:00
@@ -275,6 +275,12 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-04-27 21:17:58 +02:00
static final ItemMetaKeyType<Unit> HIDE_ADDITIONAL_TOOLTIP = new ItemMetaKeyType(DataComponents.HIDE_ADDITIONAL_TOOLTIP);
@Specific(Specific.To.NBT)
static final ItemMetaKeyType<CustomData> CUSTOM_DATA = new ItemMetaKeyType<>(DataComponents.CUSTOM_DATA);
+ // Paper start - fix ItemFlags
+ static final ItemMetaKeyType<net.minecraft.world.item.AdventureModePredicate> CAN_PLACE_ON = new ItemMetaKeyType<>(DataComponents.CAN_PLACE_ON);
+ static final ItemMetaKeyType<net.minecraft.world.item.AdventureModePredicate> CAN_BREAK = new ItemMetaKeyType<>(DataComponents.CAN_BREAK);
+ private List<net.minecraft.advancements.critereon.BlockPredicate> canPlaceOnPredicates;
+ private List<net.minecraft.advancements.critereon.BlockPredicate> canBreakPredicates;
+ // Paper end - fix ItemFlags
// We store the raw original JSON representation of all text data. See SPIGOT-5063, SPIGOT-5656, SPIGOT-5304
private Component displayName;
2024-10-24 16:02:45 +02:00
@@ -377,6 +383,10 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-04-27 21:17:58 +02:00
this.customTag = meta.customTag;
this.version = meta.version;
+ // Paper start
+ this.canPlaceOnPredicates = meta.canPlaceOnPredicates;
+ this.canBreakPredicates = meta.canBreakPredicates;
+ // Paper end
}
CraftMetaItem(DataComponentPatch tag) {
2024-10-24 16:02:45 +02:00
@@ -496,6 +506,20 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-04-30 00:16:07 +02:00
this.customTag = null;
2024-04-27 21:17:58 +02:00
}
});
+ // Paper start - fix ItemFlags
+ CraftMetaItem.getOrEmpty(tag, CraftMetaItem.CAN_PLACE_ON).ifPresent(data -> {
+ this.canPlaceOnPredicates = List.copyOf(data.predicates);
+ if (!data.showInTooltip()) {
+ this.addItemFlags(ItemFlag.HIDE_PLACED_ON);
+ }
+ });
+ CraftMetaItem.getOrEmpty(tag, CraftMetaItem.CAN_BREAK).ifPresent(data -> {
+ this.canBreakPredicates = List.copyOf(data.predicates);
+ if (!data.showInTooltip()) {
+ this.addItemFlags(ItemFlag.HIDE_DESTROYS);
+ }
+ });
+ // Paper end - fix ItemFlags
Set<Map.Entry<DataComponentType<?>, Optional<?>>> keys = tag.entrySet();
for (Map.Entry<DataComponentType<?>, Optional<?>> key : keys) {
2024-10-24 16:02:45 +02:00
@@ -735,7 +759,15 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-04-27 21:17:58 +02:00
try {
CompoundTag unhandledTag = NbtIo.readCompressed(buf, NbtAccounter.unlimitedHeap());
2024-06-15 18:28:18 +02:00
DataComponentPatch unhandledPatch = DataComponentPatch.CODEC.parse(MinecraftServer.getDefaultRegistryAccess().createSerializationContext(NbtOps.INSTANCE), unhandledTag).result().get();
- this.unhandledTags.copy(unhandledPatch);
2024-04-27 21:17:58 +02:00
+ // Paper start
2024-06-15 18:28:18 +02:00
+ CraftMetaItem.getOrEmpty(unhandledPatch, CraftMetaItem.CAN_PLACE_ON).ifPresent(data -> {
2024-04-27 21:17:58 +02:00
+ this.canPlaceOnPredicates = List.copyOf(data.predicates);
+ });
2024-06-15 18:28:18 +02:00
+ CraftMetaItem.getOrEmpty(unhandledPatch, CraftMetaItem.CAN_BREAK).ifPresent(data -> {
2024-04-27 21:17:58 +02:00
+ this.canBreakPredicates = List.copyOf(data.predicates);
+ });
2024-06-15 18:28:18 +02:00
+ this.unhandledTags.copy(unhandledPatch.forget(type -> type == CraftMetaItem.CAN_PLACE_ON.TYPE || type == CraftMetaItem.CAN_BREAK.TYPE));
2024-04-27 21:17:58 +02:00
+ // Paper end
2024-06-15 18:28:18 +02:00
for (Entry<DataComponentType<?>, Optional<?>> entry : unhandledPatch.entrySet()) {
// Move removed unhandled tags to dedicated removedTags
2024-10-24 16:02:45 +02:00
@@ -1006,6 +1038,15 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-04-27 21:17:58 +02:00
itemTag.put(CraftMetaItem.MAX_DAMAGE, this.maxDamage);
}
+ // Paper start
2024-04-28 17:22:16 +02:00
+ if (this.canPlaceOnPredicates != null && !this.canPlaceOnPredicates.isEmpty()) {
+ itemTag.put(CraftMetaItem.CAN_PLACE_ON, new net.minecraft.world.item.AdventureModePredicate(this.canPlaceOnPredicates, !this.hasItemFlag(ItemFlag.HIDE_PLACED_ON)));
2024-04-27 21:17:58 +02:00
+ }
2024-04-28 17:22:16 +02:00
+ if (this.canBreakPredicates != null && !this.canBreakPredicates.isEmpty()) {
+ itemTag.put(CraftMetaItem.CAN_BREAK, new net.minecraft.world.item.AdventureModePredicate(this.canBreakPredicates, !this.hasItemFlag(ItemFlag.HIDE_DESTROYS)));
2024-04-27 21:17:58 +02:00
+ }
+ // Paper end
+
for (Map.Entry<DataComponentType<?>, Optional<?>> e : this.unhandledTags.build().entrySet()) {
2024-07-17 01:39:57 +02:00
e.getValue().ifPresent((value) -> {
2024-04-27 21:17:58 +02:00
itemTag.builder.set((DataComponentType) e.getKey(), value);
2024-10-24 16:02:45 +02:00
@@ -1096,7 +1137,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-04-27 21:17:58 +02:00
@Overridden
boolean isEmpty() {
2024-10-24 16:02:45 +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);
+ 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
2024-04-27 21:17:58 +02:00
}
// Paper start
2024-10-24 16:02:45 +02:00
@@ -1889,6 +1930,8 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-06-14 11:59:44 +02:00
&& (this.hasJukeboxPlayable() ? that.hasJukeboxPlayable() && this.jukebox.equals(that.jukebox) : !that.hasJukeboxPlayable())
2024-04-27 21:17:58 +02:00
&& (this.hasDamage() ? that.hasDamage() && this.damage == that.damage : !that.hasDamage())
&& (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
&& (this.version == that.version);
}
2024-10-24 16:02:45 +02:00
@@ -1941,6 +1984,8 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-04-27 21:17:58 +02:00
hash = 61 * hash + (this.hasDamage() ? this.damage : 0);
hash = 61 * hash + (this.hasMaxDamage() ? 1231 : 1237);
hash = 61 * hash + (this.hasAttributeModifiers() ? this.attributeModifiers.hashCode() : 0);
+ hash = 61 * hash + (this.canPlaceOnPredicates != null ? this.canPlaceOnPredicates.hashCode() : 0); // Paper
+ hash = 61 * hash + (this.canBreakPredicates != null ? this.canBreakPredicates.hashCode() : 0); // Paper
hash = 61 * hash + this.version;
return hash;
}
2024-10-24 16:02:45 +02:00
@@ -1998,6 +2043,14 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-04-27 21:17:58 +02:00
clone.damage = this.damage;
clone.maxDamage = this.maxDamage;
clone.version = this.version;
+ // Paper start
+ if (this.canPlaceOnPredicates != null) {
+ clone.canPlaceOnPredicates = List.copyOf(this.canPlaceOnPredicates);
+ }
+ if (this.canBreakPredicates != null) {
+ clone.canBreakPredicates = List.copyOf(this.canBreakPredicates);
+ }
+ // Paper end
return clone;
} catch (CloneNotSupportedException e) {
throw new Error(e);
2024-10-24 16:02:45 +02:00
@@ -2142,6 +2195,16 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-04-27 21:17:58 +02:00
}
}
+ // Paper start
2024-04-28 17:22:16 +02:00
+ final boolean canBreakAddToUnhandled = this.canBreakPredicates != null && !this.canBreakPredicates.isEmpty();
2024-04-27 21:17:58 +02:00
+ if (canBreakAddToUnhandled) {
2024-04-28 17:22:16 +02:00
+ this.unhandledTags.set(DataComponents.CAN_BREAK, new net.minecraft.world.item.AdventureModePredicate(this.canBreakPredicates, !this.hasItemFlag(ItemFlag.HIDE_DESTROYS)));
2024-04-27 21:17:58 +02:00
+ }
2024-04-28 17:22:16 +02:00
+ final boolean canPlaceOnAddToUnhandled = this.canPlaceOnPredicates != null && !this.canPlaceOnPredicates.isEmpty();
2024-04-27 21:17:58 +02:00
+ if (canPlaceOnAddToUnhandled) {
2024-04-28 17:22:16 +02:00
+ this.unhandledTags.set(DataComponents.CAN_PLACE_ON, new net.minecraft.world.item.AdventureModePredicate(this.canPlaceOnPredicates, !this.hasItemFlag(ItemFlag.HIDE_PLACED_ON)));
2024-04-27 21:17:58 +02:00
+ }
+ // Paper end
if (!this.unhandledTags.isEmpty()) {
2024-10-24 16:02:45 +02:00
net.minecraft.nbt.Tag unhandled = DataComponentPatch.CODEC.encodeStart(MinecraftServer.getDefaultRegistryAccess().createSerializationContext(NbtOps.INSTANCE), this.unhandledTags.build()).getOrThrow(IllegalStateException::new);
2024-04-27 21:17:58 +02:00
try {
2024-10-24 16:02:45 +02:00
@@ -2152,6 +2215,14 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-04-27 21:17:58 +02:00
Logger.getLogger(CraftMetaItem.class.getName()).log(Level.SEVERE, null, ex);
}
}
+ // Paper start
+ if (canBreakAddToUnhandled) {
+ this.unhandledTags.clear(DataComponents.CAN_BREAK);
+ }
+ if (canPlaceOnAddToUnhandled) {
+ this.unhandledTags.clear(DataComponents.CAN_PLACE_ON);
+ }
+ // Paper end
2024-06-15 18:28:18 +02:00
if (!this.removedTags.isEmpty()) {
RegistryAccess registryAccess = CraftRegistry.getMinecraftRegistry();
2024-10-24 16:02:45 +02:00
@@ -2312,6 +2383,8 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
2024-04-27 21:17:58 +02:00
CraftMetaItem.MAX_DAMAGE.TYPE,
CraftMetaItem.CUSTOM_DATA.TYPE,
CraftMetaItem.ATTRIBUTES.TYPE,
+ CraftMetaItem.CAN_PLACE_ON.TYPE, // Paper
+ CraftMetaItem.CAN_BREAK.TYPE, // Paper
CraftMetaArmor.TRIM.TYPE,
CraftMetaArmorStand.ENTITY_TAG.TYPE,
CraftMetaBanner.PATTERNS.TYPE,