InvClickE

Discussion in 'Spigot Plugin Development' started by Deger, Jun 4, 2016.

  1. Hello i need help so when i click on my item i will get like a diamond sword, pickaxe, and spade., now i just made it so it send me a message but i dont know how to make it add me dia sword, pickaxe and spade!!


    Interact (inventoryclickevent)
    Code (Text):
    package me.deger.kit.event;

    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;

    public class Interact implements Listener {

        @EventHandler
        public void onInvClick(InventoryClickEvent e) {
            Inventory inv = e.getInventory();

            if (!(inv.getTitle().equals(ChatColor.RED + "Custom Inventory")))
                return;
            Player p = (Player) e.getWhoClicked();
            ItemStack item = e.getCurrentItem();

            if (item.getType() == Material.DIAMOND) {
                p.sendMessage("It Works");
                e.setCancelled(true);
                p.closeInventory();
            } else {
                e.setCancelled(true);
                p.closeInventory();
            }

        }

    }
     


    Command:
    Code (Text):
    package me.deger.kit.command;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    public class KitCommand implements CommandExecutor {

        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

            Player p = (Player) sender;

            Inventory inv = Bukkit.createInventory(null, 9, ChatColor.RED + "Custom Inventory");

            ItemStack spawnItem = nameItem(Material.DIAMOND, ChatColor.GOLD + "Kit!");
            inv.setItem(4, spawnItem);

            p.openInventory(inv);
            return true;
        }

        private ItemStack nameItem(ItemStack item, String name) {
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(name);

            item.setItemMeta(meta);
            return item;
        }

        private ItemStack nameItem(Material item, String name) {
            return nameItem(new ItemStack(item), name);

        }

    }
     
  2. Example for give Items:
    Code (Text):
    p.getInventory().addItem(new ItemStack(Material.DIAMOND_SPADE));
    p.getInventory().addItem(new ItemStack(Material.DIAMOND_PICKAXE));
     
  3. Worked like a charm, ty bro <3
     
  4. i just made a variable name of diamons spade and pickaxen and added just the names, much more handy :D
     
  5. Please mark the thread as solved. :)
     
  6. well its been a while to mark the thread as solved.