How Do Make A ItemStack That Gives An IronGolem Spawner?

Discussion in 'Spigot Plugin Development' started by IronChun, Jun 3, 2017.

  1. Basically it is simple. i am making a plugin where if you kill a certain number of mobs, you get a little something which can be configured in the config. I am very new to java development and am doing this to test the skills and knowledge that i have learned so far to see where i need to improve. This is my code so far.
    Code (Text):
    public class Events implements Listener{
        @EventHandler
        public void onDeath(EntityDeathEvent devent) {
            Player player = (Player) devent.getEntity();
            LivingEntity killed = devent.getEntity();
            if (killed.getKiller().equals(player)) {
                if (killed instanceof IronGolem) {
                    ItemStack reward = new ItemStack(Material.MOB_SPAWNER);
    I need help on making the spawner an IronGolem spawner, How Would I Do This? I am very sorry if this is a simple question but it is not for me.
     
  2. I'm not entirely sure if you can spawn an IronGolem via spawnegg. Not without basically manually spawning one in, when you right click holding the appropriate spawnegg.

    For example, if you gave the player a spawnegg called "Spawn IronGolem". Then you register an event that listens for a right click while holding an item (PlayerInteractEvent). And check if its the correct egg, if so, spawn an irongolem in that location and then remove the egg from the player's inventory.
     
  3. He's talking about a mob spawner, not a spawn egg...
     
    • Funny Funny x 1
    • Agree Agree x 1
  4. Ohhh, my bad. I misread the codesnippet. xD
     
  5. Well this was nice to see
     
  6. Use something like this

    Code (Text):
    new ItemStack(Material, Int amount, (byte)dataValue)
     
    • Agree Agree x 3
  7. I looked into it, and I don't think you can store an entity type in a spawner while it's in your inventory. What a lot of servers do is use a plugin called SilkSpawners that stores the type when you break it and sets it when you place it. I think there is an API for SilkSpawners and I would recommend using that.
     
  8. lol silkSpawners does it somehow, so that means it's definitely possible.
     
  9. WiseHollows Answer is correct. Give the Player a spawn egg an set a custom Display name with #spawnegg.getItemMeta().setDisplayName("IronGolemSpawner").
    Then Check at the PlayerInteractEvent wether the item in hand is this IronGolemSpawner egg.
    #event.getIteminHand().getItemMeta().getDisplayname().equals("IronGolemSpawner")
    then you can spawn the golem at the location of the Player
    Code (Text):
    @EventHandler
    public void onInteract(PlayerinteractEvent event){
      if(event.getIteminHand()!=null){
           if(event.getIteminHand().getItemMeta().getDisplayName()
    .equals("IRONGOLEMSpawner")){
            Location loc= event.getPlayer.getLocation();
           //spawn golem at this location
    }
    }
    }
    I wrote the code here directly into this comment- no guarranty for everything;)
     
    • Like Like x 2
    • Funny Funny x 1
  10. I think you're right.. I mean there's always more than one way. However using two events to set it makes sense; Setting the mob type upon placement and the name of the item upon break. Nice idea.
     
    • Agree Agree x 1
  11. He doesn't want to use a spawn egg. He wants a mob spawner boss
     
    • Agree Agree x 1
    • Funny Funny x 1
  12. Who's going to be 3? Lol
     
    • Funny Funny x 1
    • Like Like x 1
  13. Hey! I have run into a new problem, so i realized you can not set a mob spawner to be an irongolem spawner. So what i have decided to do is make two events, one gives the spawner as a custom item when 1000 iron golems are killed, and the other allows it to turn into an irongolem spawner once placed. Here is my code:
    Code (Text):
    public class Events implements Listener{
        @EventHandler
        public static void onDeath(EntityDeathEvent devent) {
            Player player = (Player) devent.getEntity();
            LivingEntity killed = devent.getEntity();
            ItemStack item = new ItemStack(Material.MOB_SPAWNER);
            ItemMeta reward = item.getItemMeta();
            ArrayList<String> lore = new ArrayList<String>();
            lore.add(ChatColor.YELLOW + "" + ChatColor.BOLD + "Harvested From The Death Of A 1000 Golems");
            reward.setDisplayName(ChatColor.GOLD + "" + ChatColor.BOLD + "IronGolem Spawner");
            reward.setLore(lore);
            if (killed.getKiller().equals(player)) {
                if (killed instanceof IronGolem) {
                    player.getInventory().addItem(item);
                   
                }
            }
        }
           
        }
    Now all i am wondering about is how to make it so that both classes(Seperate class for each event) Share the same item im defining.
     
    • Like Like x 2
  14. This post is a little old, but I think it you change EntityId:<mobname> to SpawnData:{id:<mobname>} you'll get a spawner that spawns <mobname>. Maybe that changed since then. Just a thought.
     
  15. Hm, I would check for you but I'm on my phone. Instead I'll give you an alternative.

    Instead of setting the actual mob spawner, why not detect a mob spawning in a mob spawner and set the type of the mob then? You can do this by storing locations of mob spawners with their desired type in a configuration file.
    https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/event/entity/CreatureSpawnEvent.html
     
  16. I think you can give it a certain MaterialData or Durability for the type of spawner?