Solved Monster Spawn Eggs

Discussion in 'Plugin Development' started by elian1203, Mar 22, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    elian1203

    Hello, it is a while since I've been on the bukkit forums, as I have not encountered many problems since 1.9. However, I have noticed that spawn eggs seem to be different. They all fall under one item id, 383, and I have yet to find a proper way to create them with code. So here I will be posting my "getItem" method, feel free to add constructive criticism as the method is greatly deprecated and I could not find another way to replicate it. Most importantly, I am looking for an answer for how to create 1.9 spawn eggs. Note: right now it just creates an empty egg.
    Code:
        @SuppressWarnings("deprecation")
        private ItemStack getItem(String item) throws Exception {
            ItemStack itemStack = null;
            Material material = Material.getMaterial(item);
    
            if (material == null) {
                String[] itemArray = item.split(":");
    
                if (itemArray.length == 1) {
                    material = Material.getMaterial(Integer.parseInt(itemArray[0]));
                    itemStack = new ItemStack(material);
                } else {
                    byte type = Byte.parseByte(itemArray[1]);
    
                    if (isInt(itemArray[0])) {
                        int id = Integer.parseInt(itemArray[0]);
                        material = Material.getMaterial(id);
                        itemStack = new ItemStack(material, 1, (short) 0, type);
                    } else {
                        material = Material.getMaterial(itemArray[0].toUpperCase());
                        itemStack = new ItemStack(material, 1, (short) 0, type);
                    }
                }
            } else {
                itemStack = new ItemStack(material);
            }
    
            if (material.equals(Material.MONSTER_EGG)) {
                SpawnEgg egg = (SpawnEgg) itemStack.getData();
    
                String type = pricesData.getString("items." + item + ".type");
                EntityType entityType = EntityType.fromName(type.toUpperCase());
    
                System.out.println(entityType);
    
                egg.setSpawnedType(entityType);
    
                itemStack = egg.toItemStack();
                // Note: I have tried itemStack.setData(egg.getData);
            }
    
            return itemStack;
        }
     
  2. Offline

    mcdorli

    They probably have different durabilitys attached to them
     
  3. Offline

    elian1203

    @mcdorli Thank you for replying, but it does not seem to work. Code:
    Code:
    if (material.equals(Material.MONSTER_EGG)) {
                // SpawnEgg egg = (SpawnEgg) itemStack.getData();
    
                String type = pricesData.getString("items." + item + ".type");
                EntityType entityType = EntityType.fromName(type.toUpperCase());
                /*
                System.out.println(entityType);
    
                egg.setSpawnedType(entityType);
    
                itemStack = egg.toItemStack();
                */
                // Note: I have tried itemStack.setData(egg.getData);
               
                System.out.println(entityType.getTypeId());
    
                itemStack.setDurability(entityType.getTypeId()); // Note: I've also tried just putting 4, 5, 50
            }
     
  4. Offline

    NathanWolf

    Spawn eggs use NBT tags now, not durability. This means (since there is not yet an API update) you have to use raw NBT access to configure a spawn egg. They need an "EntityTag" tag with at least an "id" tag underneath that with the entity type.

    See here for a /give example: http://minecraft.gamepedia.com/Spawn_Egg#Data_values

    The good/cool news is that you can put more than the id in there. Any tags you put in will be passed on to the spawned entity, meaning they can have custom names or just about anything else you can imagine.
     
  5. Offline

    elian1203

Thread Status:
Not open for further replies.

Share This Page