How to spawn an entity from a GUI

Discussion in 'Spigot Plugin Development' started by Stellrow, Jul 23, 2018.

  1. So im trying to spawn an enitty whenever i click the item in the gui,but i dont know how to make another event happening inside another event
    Code (Text):
    package main;

    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntitySpawnEvent;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    import net.md_5.bungee.api.ChatColor;

    public class SpawnShop implements Listener {
        private final Main plugin;
        public SpawnShop(Main plugin) {
            this.plugin = plugin;
        }
       
        @EventHandler
        public void onClickEvent(InventoryClickEvent e) {
            Player player = (Player) e.getWhoClicked();

            Inventory open = e.getClickedInventory();
            ItemStack item = e.getCurrentItem();

            if (item == null || e.isCancelled() || !open.getName().equals(ChatColor.GREEN + "Shop"))
                return;
           
            ItemMeta meta = item.getItemMeta();
            String name = meta.getDisplayName();
            if(name.equals(ChatColor.DARK_RED + "Villager")) {
                ///Spawn here
                }
            }
           
        }
       

     
    Anyone knows a good and efficient way to do this?
     
  2. Simple :
    player.getWorld().spawnEntity(player.getLocation(), EntityType.VILLAGER)
     
    • Winner Winner x 1