Solved Setting spawned type for spawn eggs throws UnsupportedOperationException

Discussion in 'Spigot Plugin Development' started by Gadse, Feb 2, 2020.

Thread Status:
Not open for further replies.
  1. Hello everyone,

    this is a bit of a weird one. What I'm trying to do, is to create a spawn egg for an entity type, that does not have a spawn egg. In my specific case, it's an iron golem.

    If anyone needs the code on how I'm trying to achieve it:
    Code (Java):
    ItemStack egg = new ItemStack(Material.GHAST_SPAWN_EGG);
    SpawnEggMeta meta = (SpawnEggMeta) egg.getItemMeta();
    meta.setSpawnedType(EntityType.IRON_GOLEM); // This line throws the error
    egg.setItemMeta(meta);

    Now, I understand that you're supposed to use the proper material for each mob type and why SpawnEggMeta#setSpawnedType is deprecated. But as I already said, this does not work for mobs who do not have a spawn egg.

    I know for a fact, that this should work just fine anyway since you can just give yourself a spawn egg which does the same with vanilla commands.
    /minecraft:give @s minecraft:ghast_spawn_egg{display:{Name:'{\"text\":\"Iron Golem Spawn Egg\"}'},EntityTag:{id:\"minecraft:iron_golem\"}} 1

    So I am genuinely confused, as to why this would not work in spigot. If anyone has an idea, any input to solve this issue would be greatly appreciated. I kind of want to avoid running commands to achieve something. If NMS could be avoided, that'd be nice too, but it's not a requirement. I'd just like to know if there is an API way to achieve what I want.

    ~ Gadse
     
    • Useful Useful x 1
  2. Hi!

    I couldn't find another solution but to use NMS. I agree with you, it's very unfortunate but it seems to be the only way.
    The following would do the trick.

    Code (Java):
    ItemStack egg = new ItemStack(Material.GHAST_SPAWN_EGG);
    net.minecraft.server.v1_15_R1.ItemStack NMSEgg = CraftItemStack.asNMSCopy(egg);

    NBTTagCompound entityTagCompound = new NBTTagCompound();
    entityTagCompound.setString("id", "minecraft:iron_golem");

    NBTTagCompound eggCompound  = (NMSEgg.hasTag()) ? NMSEgg.getTag() : new NBTTagCompound();
    eggCompound.set("EntityTag", entityTagCompound);
       
    NMSEgg.setTag(eggCompound);
    egg = CraftItemStack.asBukkitCopy(NMSEgg);

    I don't really know why Spigot would completely eliminate the possibility of changing the spawned type. I found this line in the documentation.
    That would explain why I guess...

    Anyway, good luck with your project!
     
    #2 SpeedWolf13, Feb 2, 2020
    Last edited: Feb 2, 2020
    • Friendly Friendly x 1
  3. Yeah, I thought as much, that's unfortunate. I'll leave this thread open for a while longer, in case anyone has a smart idea, but it seems like I'll have to go with that.
     
  4. SteelPhoenix

    Moderator

    Javadoc:
    As stated, each spawn egg now has their own material enum value. You can only change the spawned type by modifying the underlying (unsupported) nms handle.
     
Thread Status:
Not open for further replies.