Spawn Egg troubles

Discussion in 'Plugin Development' started by ChazSchmidt, Mar 28, 2016.

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

    ChazSchmidt

    What is the simplest way to spawn the entity that a spawn egg would spawn?

    For example, spawning a zombie entity by examining a zombie egg.

    I've been playing around with it but the only way I have found that is working is clunky, deprecated, and seems more like a work around than a solution.

    Code:
        @SuppressWarnings("deprecation")
        @EventHandler
        public void chestClick(PlayerInteractEvent e) {
            if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                Block b = e.getClickedBlock();
                Player player = e.getPlayer();
                if (b.getType() == Material.CHEST) {
                    Chest c = (Chest) b.getState();
                    if(c.getInventory().contains(Material.MONSTER_EGG)) {
                        HashMap<Integer, ? extends ItemStack> eggMap = c.getInventory().all(Material.MONSTER_EGG);
                        Set<Integer> set = new HashSet<Integer>();
                        set.addAll(eggMap.keySet());
                        for (Integer i : set) {
                            ItemStack it = eggMap.remove(i);
                            log.info("The ItemTypeId : " + it.getData().getItemTypeId());
                            log.info("The ItemTypeId : " + it.getData().toString());
                            String s = it.getData().toString();
                            s = s.substring(10);
                            s = s.substring(0, (s.length()-1));
                            log.info("String: " + s);
                            b.getWorld().spawnEntity(b.getLocation(), EntityType.fromName(s));
                            c.getInventory().removeItem(it);
                        }
                    }
                }
            }
        }
     
  2. Offline

    mine-care

    Take a look at the docs:
    http://docs.codelanx.com/Bukkit/1.7.10/org/bukkit/material/SpawnEgg.html#getSpawnedType--
    Using this method you can get the type that will be spawn instead of String maniplulations etc which arent all that secure.
    Also, what are you trying to do here:
     
  3. Offline

    ChazSchmidt

    Thank you so much. I knew there had to be an easier way that wasn't soooo terrible. The entire time I was doing the string manipulation I knew it was a horrible idea I just hadn't found an easier way and just wanted to work out further details like where to spawn them, etc.

    I had googled a million different things but I guess I overlooked the old spawn eggs because it refers to them as monster eggs. Thanks again.
     
    mine-care likes this.
Thread Status:
Not open for further replies.

Share This Page