SPIGOT-7636: Fix clearing ItemMeta

By: md_5 <git@md-5.net>
This commit is contained in:
CraftBukkit/Spigot 2024-04-25 16:15:01 +10:00
parent b031cef198
commit 2f65b80661
5 changed files with 28 additions and 24 deletions

View file

@ -103,7 +103,7 @@
Item item = this.getItem();
- EnumInteractionResult enuminteractionresult = item.useOn(itemactioncontext);
+ // CraftBukkit start - handle all block place event logic here
+ PatchedDataComponentMap oldData = this.getComponentsClone();
+ DataComponentPatch oldData = this.getComponentsPatch();
+ int oldCount = this.getCount();
+ WorldServer world = (WorldServer) itemactioncontext.getLevel();
+
@ -120,10 +120,10 @@
+ } finally {
+ world.captureBlockStates = false;
+ }
+ PatchedDataComponentMap newData = this.getComponentsClone();
+ DataComponentPatch newData = this.getComponentsPatch();
+ int newCount = this.getCount();
+ this.setCount(oldCount);
+ this.setComponentsClone(oldData);
+ this.restorePatch(oldData);
+ if (enuminteractionresult.consumesAction() && world.captureTreeGeneration && world.capturedBlockStates.size() > 0) {
+ world.captureTreeGeneration = false;
+ Location location = CraftLocation.toBukkit(blockposition, world.getWorld());
@ -145,7 +145,7 @@
+ if (!fertilizeEvent.isCancelled()) {
+ // Change the stack to its new contents if it hasn't been tampered with.
+ if (this.getCount() == oldCount && Objects.equals(this.components, oldData)) {
+ this.applyComponents(newData);
+ this.restorePatch(newData);
+ this.setCount(newCount);
+ }
+ for (CraftBlockState blockstate : blocks) {
@ -192,7 +192,7 @@
+ } else {
+ // Change the stack to its new contents if it hasn't been tampered with.
+ if (this.getCount() == oldCount && Objects.equals(this.components, oldData)) {
+ this.applyComponents(newData);
+ this.restorePatch(newData);
+ this.setCount(newCount);
+ }
+
@ -325,25 +325,20 @@
this.shrink(1);
if (entityliving instanceof EntityHuman) {
@@ -669,6 +909,17 @@
@@ -669,6 +909,12 @@
return this.getItem().useOnRelease(this);
}
+ // CraftBukkit start
+ @Nullable
+ private PatchedDataComponentMap getComponentsClone() {
+ return this.components.isEmpty() ? null : this.components.copy();
+ }
+
+ private void setComponentsClone(@Nullable PatchedDataComponentMap patchedDataComponentMap) {
+ this.applyComponents(patchedDataComponentMap == null ? null : patchedDataComponentMap.copy());
+ public void restorePatch(DataComponentPatch datacomponentpatch) {
+ this.components.restorePatch(datacomponentpatch);
+ }
+ // CraftBukkit end
+
@Nullable
public <T> T set(DataComponentType<? super T> datacomponenttype, @Nullable T t0) {
return this.components.set(datacomponenttype, t0);
@@ -728,7 +979,7 @@
@@ -728,7 +974,7 @@
}
private <T extends TooltipProvider> void addToTooltip(DataComponentType<T> datacomponenttype, Item.b item_b, Consumer<IChatBaseComponent> consumer, TooltipFlag tooltipflag) {
@ -352,7 +347,7 @@
if (t0 != null) {
t0.addToTooltip(item_b, consumer, tooltipflag);
@@ -955,6 +1206,13 @@
@@ -955,6 +1201,13 @@
}
@ -366,7 +361,7 @@
public IChatBaseComponent getDisplayName() {
IChatMutableComponent ichatmutablecomponent = IChatBaseComponent.empty().append(this.getHoverName());
@@ -1017,7 +1275,7 @@
@@ -1017,7 +1270,7 @@
}
public void consume(int i, @Nullable EntityLiving entityliving) {

View file

@ -4,7 +4,7 @@ import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import java.util.Map;
import net.minecraft.core.Holder;
import net.minecraft.core.component.DataComponentMap;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.component.DataComponents;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.enchantment.EnchantmentManager;
@ -616,7 +616,7 @@ public final class CraftItemStack extends ItemStack {
return false;
}
if (CraftItemFactory.instance().equals(itemMeta, null)) {
item.applyComponents(DataComponentMap.EMPTY);
item.restorePatch(DataComponentPatch.EMPTY);
return true;
}
if (!CraftItemFactory.instance().isApplicable(itemMeta, getType(item))) {
@ -636,7 +636,7 @@ public final class CraftItemStack extends ItemStack {
CraftMetaItem.Applicator tag = new CraftMetaItem.Applicator();
((CraftMetaItem) itemMeta).applyToItem(tag);
item.applyComponents(tag.build());
item.restorePatch(tag.build());
}
// SpigotCraft#463 this is required now by the Vanilla client, so mimic ItemStack constructor in ensuring it
if (item.getItem() != null && item.getMaxDamage() > 0) {

View file

@ -308,8 +308,12 @@ public class CraftMetaBookSigned extends CraftMetaItem implements BookMeta {
if (this.pages != null) {
hash = 61 * hash + 17 * this.pages.hashCode();
}
hash = 61 * hash + 17 * Boolean.hashCode(resolved);
hash = 61 * hash + 19 * Integer.hashCode(generation);
if (this.resolved) {
hash = 61 * hash + 17 * Boolean.hashCode(this.resolved);
}
if (hasGeneration()) {
hash = 61 * hash + 19 * Integer.hashCode(this.generation);
}
return original != hash ? CraftMetaBook.class.hashCode() ^ hash : hash;
}

View file

@ -18,7 +18,8 @@ public class CraftMetaColorableArmor extends CraftMetaArmor implements Colorable
Material.LEATHER_HELMET,
Material.LEATHER_CHESTPLATE,
Material.LEATHER_LEGGINGS,
Material.LEATHER_BOOTS
Material.LEATHER_BOOTS,
Material.WOLF_ARMOR
);
private Color color = DEFAULT_LEATHER_COLOR;

View file

@ -169,7 +169,9 @@ public class CraftMetaCompass extends CraftMetaItem implements CompassMeta {
hash = 73 * hash + lodestoneY;
hash = 73 * hash + lodestoneZ;
}
hash = 73 * hash + (isLodestoneTracked() ? 1231 : 1237);
if (hasLodestoneTracked()) {
hash = 73 * hash + (isLodestoneTracked() ? 1231 : 1237);
}
return original != hash ? CraftMetaCompass.class.hashCode() ^ hash : hash;
}
@ -205,7 +207,9 @@ public class CraftMetaCompass extends CraftMetaItem implements CompassMeta {
builder.put(LODESTONE_POS_Y.BUKKIT, lodestoneY);
builder.put(LODESTONE_POS_Z.BUKKIT, lodestoneZ);
}
builder.put(LODESTONE_TRACKED.BUKKIT, tracked);
if (hasLodestoneTracked()) {
builder.put(LODESTONE_TRACKED.BUKKIT, tracked);
}
return builder;
}