From ce747e1973920ffc57464f38ce1ecf3e4ddac557 Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Wed, 29 May 2024 06:48:52 +1000 Subject: [PATCH] #1013, SPIGOT-4288, SPIGOT-6202: Add material rerouting in preparation for the switch to ItemType and BlockType This also moves the conversion from and to legacy material to the method calls of legacy plugins, and no longer allows them directly in the server. This has the side effect of fixing some legacy plugin issues, such as SPIGOT-4288, SPIGOT-6161. Also fixes legacy items sometimes not stacking in inventory when using addItem, a client disconnect when using legacy items in recipes and probably some more. By: DerFrZocker --- .../event/inventory/FurnaceExtractEvent.java | 5 +++++ .../bukkit/event/player/PlayerBucketEvent.java | 8 +++++++- .../player/PlayerStatisticIncrementEvent.java | 12 ++++++++++++ .../main/java/org/bukkit/inventory/ItemStack.java | 9 ++++++++- .../java/org/bukkit/inventory/RecipeChoice.java | 15 ++++++++++++--- 5 files changed, 44 insertions(+), 5 deletions(-) diff --git a/paper-api/src/main/java/org/bukkit/event/inventory/FurnaceExtractEvent.java b/paper-api/src/main/java/org/bukkit/event/inventory/FurnaceExtractEvent.java index 020739697..65db4991b 100644 --- a/paper-api/src/main/java/org/bukkit/event/inventory/FurnaceExtractEvent.java +++ b/paper-api/src/main/java/org/bukkit/event/inventory/FurnaceExtractEvent.java @@ -1,9 +1,11 @@ package org.bukkit.event.inventory; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.entity.Player; import org.bukkit.event.block.BlockExpEvent; +import org.bukkit.material.MaterialData; import org.jetbrains.annotations.NotNull; /** @@ -17,6 +19,9 @@ public class FurnaceExtractEvent extends BlockExpEvent { public FurnaceExtractEvent(@NotNull Player player, @NotNull Block block, @NotNull Material itemType, int itemAmount, int exp) { super(block, exp); this.player = player; + if (itemType != null && itemType.isLegacy()) { + itemType = Bukkit.getUnsafe().fromLegacy(new MaterialData(itemType), true); + } this.itemType = itemType; this.itemAmount = itemAmount; } diff --git a/paper-api/src/main/java/org/bukkit/event/player/PlayerBucketEvent.java b/paper-api/src/main/java/org/bukkit/event/player/PlayerBucketEvent.java index d108067c3..ab045b73e 100644 --- a/paper-api/src/main/java/org/bukkit/event/player/PlayerBucketEvent.java +++ b/paper-api/src/main/java/org/bukkit/event/player/PlayerBucketEvent.java @@ -1,5 +1,6 @@ package org.bukkit.event.player; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; @@ -7,6 +8,7 @@ import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; +import org.bukkit.material.MaterialData; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -38,7 +40,11 @@ public abstract class PlayerBucketEvent extends PlayerEvent implements Cancellab this.blockClicked = blockClicked; this.blockFace = blockFace; this.itemStack = itemInHand; - this.bucket = bucket; + if (bucket != null && bucket.isLegacy()) { + this.bucket = Bukkit.getUnsafe().fromLegacy(new MaterialData(bucket), true); + } else { + this.bucket = bucket; + } this.hand = hand; } diff --git a/paper-api/src/main/java/org/bukkit/event/player/PlayerStatisticIncrementEvent.java b/paper-api/src/main/java/org/bukkit/event/player/PlayerStatisticIncrementEvent.java index f971844bf..3fbcb0ecd 100644 --- a/paper-api/src/main/java/org/bukkit/event/player/PlayerStatisticIncrementEvent.java +++ b/paper-api/src/main/java/org/bukkit/event/player/PlayerStatisticIncrementEvent.java @@ -1,11 +1,13 @@ package org.bukkit.event.player; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.Statistic; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import org.bukkit.event.HandlerList; +import org.bukkit.material.MaterialData; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -49,6 +51,16 @@ public class PlayerStatisticIncrementEvent extends PlayerEvent implements Cancel this.initialValue = initialValue; this.newValue = newValue; this.entityType = null; + if (material != null && material.isLegacy()) { + if (statistic.getType() == Statistic.Type.BLOCK) { + material = Bukkit.getUnsafe().fromLegacy(new MaterialData(material), false); + } else if (statistic.getType() == Statistic.Type.ITEM) { + material = Bukkit.getUnsafe().fromLegacy(new MaterialData(material), true); + } else { + // Theoretically, this should not happen, can probably print a warning, but for now it should be fine. + material = Bukkit.getUnsafe().fromLegacy(new MaterialData(material), false); + } + } this.material = material; } diff --git a/paper-api/src/main/java/org/bukkit/inventory/ItemStack.java b/paper-api/src/main/java/org/bukkit/inventory/ItemStack.java index 692f67c4e..62f5e08e8 100644 --- a/paper-api/src/main/java/org/bukkit/inventory/ItemStack.java +++ b/paper-api/src/main/java/org/bukkit/inventory/ItemStack.java @@ -83,8 +83,15 @@ public class ItemStack implements Cloneable, ConfigurationSerializable, Translat * @deprecated this method uses an ambiguous data byte object */ @Deprecated - public ItemStack(@NotNull final Material type, final int amount, final short damage, @Nullable final Byte data) { + public ItemStack(@NotNull Material type, final int amount, final short damage, @Nullable final Byte data) { Preconditions.checkArgument(type != null, "Material cannot be null"); + if (type.isLegacy()) { + if (type.getMaxDurability() > 0) { + type = Bukkit.getUnsafe().fromLegacy(new MaterialData(type, data == null ? 0 : data), true); + } else { + type = Bukkit.getUnsafe().fromLegacy(new MaterialData(type, data == null ? (byte) damage : data), true); + } + } this.type = type; this.amount = amount; if (damage != 0) { diff --git a/paper-api/src/main/java/org/bukkit/inventory/RecipeChoice.java b/paper-api/src/main/java/org/bukkit/inventory/RecipeChoice.java index 5b7ddf5f3..a98fc2ffd 100644 --- a/paper-api/src/main/java/org/bukkit/inventory/RecipeChoice.java +++ b/paper-api/src/main/java/org/bukkit/inventory/RecipeChoice.java @@ -7,8 +7,10 @@ import java.util.Collections; import java.util.List; import java.util.Objects; import java.util.function.Predicate; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.Tag; +import org.bukkit.material.MaterialData; import org.jetbrains.annotations.NotNull; /** @@ -65,12 +67,19 @@ public interface RecipeChoice extends Predicate, Cloneable { public MaterialChoice(@NotNull List choices) { Preconditions.checkArgument(choices != null, "choices"); Preconditions.checkArgument(!choices.isEmpty(), "Must have at least one choice"); + + this.choices = new ArrayList<>(choices.size()); + for (Material choice : choices) { Preconditions.checkArgument(choice != null, "Cannot have null choice"); - Preconditions.checkArgument(!choice.isAir(), "Cannot have empty/air choice"); - } - this.choices = new ArrayList<>(choices); + if (choice.isLegacy()) { + choice = Bukkit.getUnsafe().fromLegacy(new MaterialData(choice, (byte) 0), true); + } + + Preconditions.checkArgument(!choice.isAir(), "Cannot have empty/air choice"); + this.choices.add(choice); + } } @Override