Skip to content

Commit

Permalink
SPIGOT-7731: Spawn eggs cannot have damage
Browse files Browse the repository at this point in the history
  • Loading branch information
Doc94 authored and md-5 committed Jun 10, 2024
1 parent 340ccd5 commit c6b4d5a
Show file tree
Hide file tree
Showing 6 changed files with 4 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -459,11 +459,6 @@ public ItemStack createItemStack(String input) throws IllegalArgumentException {
}
}

@Override
public Material updateMaterial(ItemMeta meta, Material material) throws IllegalArgumentException {
return ((CraftMetaItem) meta).updateMaterial(material);
}

@Override
public Material getSpawnEgg(EntityType type) {
if (type == EntityType.UNKNOWN) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -626,12 +626,6 @@ public static boolean setItemMeta(net.minecraft.world.item.ItemStack item, ItemM
itemMeta = CraftItemFactory.instance().asMetaFor(itemMeta, getType(item));
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()) {
CraftMetaItem.Applicator tag = new CraftMetaItem.Applicator();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1691,10 +1691,6 @@ ImmutableMap.Builder<String, Object> serialize(ImmutableMap.Builder<String, Obje
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) {
if (enchantments == null || enchantments.isEmpty()) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;
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.EntityType;
import org.bukkit.inventory.meta.SpawnEggMeta;
import org.bukkit.material.MaterialData;

@DelegateDeserialization(SerializableMeta.class)
public class CraftMetaSpawnEgg extends CraftMetaItem implements SpawnEggMeta {
Expand Down Expand Up @@ -110,19 +107,10 @@ public class CraftMetaSpawnEgg extends CraftMetaItem implements SpawnEggMeta {
@ItemMetaKey.Specific(ItemMetaKey.Specific.To.NBT)
static final ItemMetaKey ENTITY_ID = new ItemMetaKey("id");

private EntityType spawnedType;
private NBTTagCompound entityTag;

CraftMetaSpawnEgg(CraftMetaItem meta) {
super(meta);

if (!(meta instanceof CraftMetaSpawnEgg egg)) {
return;
}

this.spawnedType = egg.spawnedType;

updateMaterial(null); // Trigger type population
}

CraftMetaSpawnEgg(DataComponentPatch tag) {
Expand All @@ -135,11 +123,6 @@ public class CraftMetaSpawnEgg extends CraftMetaItem implements SpawnEggMeta {

CraftMetaSpawnEgg(Map<String, Object> map) {
super(map);

String entityType = SerializableMeta.getString(map, ENTITY_ID.BUKKIT, true);
if (entityType != null) {
this.spawnedType = CraftEntityType.stringToBukkit(entityType);
}
}

@Override
Expand All @@ -149,31 +132,11 @@ void deserializeInternal(NBTTagCompound tag, Object context) {
if (tag.contains(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
if (!entityTag.isEmpty()) {
// 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();
}

// See if we can read a converted ID tag
if (entityTag.contains(ENTITY_ID.NBT)) {
this.spawnedType = CraftEntityType.stringToBukkit(entityTag.getString(ENTITY_ID.NBT));
}
}
}

Expand Down Expand Up @@ -208,11 +171,7 @@ boolean isEmpty() {
}

boolean isSpawnEggEmpty() {
return !(hasSpawnedType() || entityTag != null);
}

boolean hasSpawnedType() {
return spawnedType != null;
return entityTag != null;
}

@Override
Expand Down Expand Up @@ -244,8 +203,7 @@ boolean equalsCommon(CraftMetaItem meta) {
if (meta instanceof CraftMetaSpawnEgg) {
CraftMetaSpawnEgg that = (CraftMetaSpawnEgg) meta;

return hasSpawnedType() ? that.hasSpawnedType() && this.spawnedType.equals(that.spawnedType) : !that.hasSpawnedType()
&& entityTag != null ? that.entityTag != null && this.entityTag.equals(that.entityTag) : entityTag == null;
return entityTag != null ? that.entityTag != null && this.entityTag.equals(that.entityTag) : entityTag == null;
}
return true;
}
Expand All @@ -260,9 +218,6 @@ int applyHash() {
final int original;
int hash = original = super.applyHash();

if (hasSpawnedType()) {
hash = 73 * hash + spawnedType.hashCode();
}
if (entityTag != null) {
hash = 73 * hash + entityTag.hashCode();
}
Expand All @@ -281,30 +236,10 @@ Builder<String, Object> serialize(Builder<String, Object> builder) {
public CraftMetaSpawnEgg clone() {
CraftMetaSpawnEgg clone = (CraftMetaSpawnEgg) super.clone();

clone.spawnedType = spawnedType;
if (entityTag != null) {
clone.entityTag = entityTag.copy();
}

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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public static void init() {
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.BLAZE.getTypeId(), Material.BLAZE_SPAWN_EGG);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void testBukkitClasses(InputStream inputStream) throws IOException {
if (methodNode.desc.contains("Lorg/bukkit/Material;") || signature.contains("Lorg/bukkit/Material;")) {
// Add method exceptions here
switch (methodNode.name) {
case "<init>", "updateMaterial", "setItemMeta0" -> {
case "<init>", "setItemMeta0" -> {
continue;
}
}
Expand Down

0 comments on commit c6b4d5a

Please sign in to comment.