SPIGOT-7731: Spawn eggs cannot have damage

By: Doc <nachito94@msn.com>
This commit is contained in:
CraftBukkit/Spigot 2024-06-10 14:06:34 +10:00
parent 95f120b7ff
commit 9db084c279
6 changed files with 4 additions and 84 deletions

View file

@ -459,11 +459,6 @@ public final class CraftItemFactory implements ItemFactory {
} }
} }
@Override
public Material updateMaterial(ItemMeta meta, Material material) throws IllegalArgumentException {
return ((CraftMetaItem) meta).updateMaterial(material);
}
@Override @Override
public Material getSpawnEgg(EntityType type) { public Material getSpawnEgg(EntityType type) {
if (type == EntityType.UNKNOWN) { if (type == EntityType.UNKNOWN) {

View file

@ -626,12 +626,6 @@ public final class CraftItemStack extends ItemStack {
itemMeta = CraftItemFactory.instance().asMetaFor(itemMeta, getType(item)); itemMeta = CraftItemFactory.instance().asMetaFor(itemMeta, getType(item));
if (itemMeta == null) return true; if (itemMeta == null) return true;
Item oldItem = item.getItem();
Item newItem = CraftItemType.bukkitToMinecraft(CraftItemFactory.instance().updateMaterial(itemMeta, CraftItemType.minecraftToBukkit(oldItem)));
if (oldItem != newItem) {
item.setItem(newItem);
}
if (!((CraftMetaItem) itemMeta).isEmpty()) { if (!((CraftMetaItem) itemMeta).isEmpty()) {
CraftMetaItem.Applicator tag = new CraftMetaItem.Applicator(); CraftMetaItem.Applicator tag = new CraftMetaItem.Applicator();

View file

@ -1691,10 +1691,6 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable, BlockDataMeta {
void serializeInternal(final Map<String, NBTBase> unhandledTags) { void serializeInternal(final Map<String, NBTBase> unhandledTags) {
} }
Material updateMaterial(Material material) {
return material;
}
static void serializeEnchantments(Map<Enchantment, Integer> enchantments, ImmutableMap.Builder<String, Object> builder, ItemMetaKey key) { static void serializeEnchantments(Map<Enchantment, Integer> enchantments, ImmutableMap.Builder<String, Object> builder, ItemMetaKey key) {
if (enchantments == null || enchantments.isEmpty()) { if (enchantments == null || enchantments.isEmpty()) {
return; return;

View file

@ -13,12 +13,9 @@ import net.minecraft.world.item.component.CustomData;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization; import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.entity.CraftEntitySnapshot; import org.bukkit.craftbukkit.entity.CraftEntitySnapshot;
import org.bukkit.craftbukkit.entity.CraftEntityType;
import org.bukkit.craftbukkit.util.CraftLegacy;
import org.bukkit.entity.EntitySnapshot; import org.bukkit.entity.EntitySnapshot;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.inventory.meta.SpawnEggMeta; import org.bukkit.inventory.meta.SpawnEggMeta;
import org.bukkit.material.MaterialData;
@DelegateDeserialization(SerializableMeta.class) @DelegateDeserialization(SerializableMeta.class)
public class CraftMetaSpawnEgg extends CraftMetaItem implements SpawnEggMeta { public class CraftMetaSpawnEgg extends CraftMetaItem implements SpawnEggMeta {
@ -110,19 +107,10 @@ public class CraftMetaSpawnEgg extends CraftMetaItem implements SpawnEggMeta {
@ItemMetaKey.Specific(ItemMetaKey.Specific.To.NBT) @ItemMetaKey.Specific(ItemMetaKey.Specific.To.NBT)
static final ItemMetaKey ENTITY_ID = new ItemMetaKey("id"); static final ItemMetaKey ENTITY_ID = new ItemMetaKey("id");
private EntityType spawnedType;
private NBTTagCompound entityTag; private NBTTagCompound entityTag;
CraftMetaSpawnEgg(CraftMetaItem meta) { CraftMetaSpawnEgg(CraftMetaItem meta) {
super(meta); super(meta);
if (!(meta instanceof CraftMetaSpawnEgg egg)) {
return;
}
this.spawnedType = egg.spawnedType;
updateMaterial(null); // Trigger type population
} }
CraftMetaSpawnEgg(DataComponentPatch tag) { CraftMetaSpawnEgg(DataComponentPatch tag) {
@ -135,11 +123,6 @@ public class CraftMetaSpawnEgg extends CraftMetaItem implements SpawnEggMeta {
CraftMetaSpawnEgg(Map<String, Object> map) { CraftMetaSpawnEgg(Map<String, Object> map) {
super(map); super(map);
String entityType = SerializableMeta.getString(map, ENTITY_ID.BUKKIT, true);
if (entityType != null) {
this.spawnedType = CraftEntityType.stringToBukkit(entityType);
}
} }
@Override @Override
@ -149,31 +132,11 @@ public class CraftMetaSpawnEgg extends CraftMetaItem implements SpawnEggMeta {
if (tag.contains(ENTITY_TAG.NBT)) { if (tag.contains(ENTITY_TAG.NBT)) {
entityTag = tag.getCompound(ENTITY_TAG.NBT); entityTag = tag.getCompound(ENTITY_TAG.NBT);
if (context instanceof Map) {
Map<String, Object> map = (Map<String, Object>) context;
// Duplicated from constructor
String entityType = SerializableMeta.getString(map, ENTITY_ID.BUKKIT, true);
if (entityType != null) {
this.spawnedType = CraftEntityType.stringToBukkit(entityType);
}
}
if (this.spawnedType != null) {
// We have a valid spawn type, just remove the ID now
entityTag.remove(ENTITY_ID.NBT);
}
// Tag still has some other data, lets try our luck with a conversion // Tag still has some other data, lets try our luck with a conversion
if (!entityTag.isEmpty()) { if (!entityTag.isEmpty()) {
// SPIGOT-4128: This is hopeless until we start versioning stacks. RIP data. // SPIGOT-4128: This is hopeless until we start versioning stacks. RIP data.
// entityTag = (NBTTagCompound) MinecraftServer.getServer().dataConverterManager.update(DataConverterTypes.ENTITY, new Dynamic(DynamicOpsNBT.a, entityTag), -1, CraftMagicNumbers.DATA_VERSION).getValue(); // entityTag = (NBTTagCompound) MinecraftServer.getServer().dataConverterManager.update(DataConverterTypes.ENTITY, new Dynamic(DynamicOpsNBT.a, entityTag), -1, CraftMagicNumbers.DATA_VERSION).getValue();
} }
// See if we can read a converted ID tag
if (entityTag.contains(ENTITY_ID.NBT)) {
this.spawnedType = CraftEntityType.stringToBukkit(entityTag.getString(ENTITY_ID.NBT));
}
} }
} }
@ -208,11 +171,7 @@ public class CraftMetaSpawnEgg extends CraftMetaItem implements SpawnEggMeta {
} }
boolean isSpawnEggEmpty() { boolean isSpawnEggEmpty() {
return !(hasSpawnedType() || entityTag != null); return entityTag != null;
}
boolean hasSpawnedType() {
return spawnedType != null;
} }
@Override @Override
@ -244,8 +203,7 @@ public class CraftMetaSpawnEgg extends CraftMetaItem implements SpawnEggMeta {
if (meta instanceof CraftMetaSpawnEgg) { if (meta instanceof CraftMetaSpawnEgg) {
CraftMetaSpawnEgg that = (CraftMetaSpawnEgg) meta; CraftMetaSpawnEgg that = (CraftMetaSpawnEgg) meta;
return hasSpawnedType() ? that.hasSpawnedType() && this.spawnedType.equals(that.spawnedType) : !that.hasSpawnedType() return entityTag != null ? that.entityTag != null && this.entityTag.equals(that.entityTag) : entityTag == null;
&& entityTag != null ? that.entityTag != null && this.entityTag.equals(that.entityTag) : entityTag == null;
} }
return true; return true;
} }
@ -260,9 +218,6 @@ public class CraftMetaSpawnEgg extends CraftMetaItem implements SpawnEggMeta {
final int original; final int original;
int hash = original = super.applyHash(); int hash = original = super.applyHash();
if (hasSpawnedType()) {
hash = 73 * hash + spawnedType.hashCode();
}
if (entityTag != null) { if (entityTag != null) {
hash = 73 * hash + entityTag.hashCode(); hash = 73 * hash + entityTag.hashCode();
} }
@ -281,30 +236,10 @@ public class CraftMetaSpawnEgg extends CraftMetaItem implements SpawnEggMeta {
public CraftMetaSpawnEgg clone() { public CraftMetaSpawnEgg clone() {
CraftMetaSpawnEgg clone = (CraftMetaSpawnEgg) super.clone(); CraftMetaSpawnEgg clone = (CraftMetaSpawnEgg) super.clone();
clone.spawnedType = spawnedType;
if (entityTag != null) { if (entityTag != null) {
clone.entityTag = entityTag.copy(); clone.entityTag = entityTag.copy();
} }
return clone; return clone;
} }
@Override
final Material updateMaterial(Material material) {
if (spawnedType == null) {
spawnedType = EntityType.fromId(getDamage());
setDamage(0);
}
if (spawnedType != null) {
if (entityTag != null) {
// Remove ID tag as it is now in the material
entityTag.remove("id");
}
return CraftLegacy.fromLegacy(new MaterialData(Material.LEGACY_MONSTER_EGG, (byte) spawnedType.getTypeId()));
}
return super.updateMaterial(material);
}
} }

View file

@ -269,7 +269,7 @@ public final class CraftLegacy {
new Exception().printStackTrace(); new Exception().printStackTrace();
} }
SPAWN_EGGS.put((byte) 0, Material.PIG_SPAWN_EGG); // Will be fixed by updateMaterial if possible SPAWN_EGGS.put((byte) 0, Material.PIG_SPAWN_EGG);
SPAWN_EGGS.put((byte) EntityType.BAT.getTypeId(), Material.BAT_SPAWN_EGG); SPAWN_EGGS.put((byte) EntityType.BAT.getTypeId(), Material.BAT_SPAWN_EGG);
SPAWN_EGGS.put((byte) EntityType.BLAZE.getTypeId(), Material.BLAZE_SPAWN_EGG); SPAWN_EGGS.put((byte) EntityType.BLAZE.getTypeId(), Material.BLAZE_SPAWN_EGG);

View file

@ -88,7 +88,7 @@ public class MaterialReroutingTest extends AbstractTestingBase {
if (methodNode.desc.contains("Lorg/bukkit/Material;") || signature.contains("Lorg/bukkit/Material;")) { if (methodNode.desc.contains("Lorg/bukkit/Material;") || signature.contains("Lorg/bukkit/Material;")) {
// Add method exceptions here // Add method exceptions here
switch (methodNode.name) { switch (methodNode.name) {
case "<init>", "updateMaterial", "setItemMeta0" -> { case "<init>", "setItemMeta0" -> {
continue; continue;
} }
} }