Giving items in Gui

Discussion in 'Spigot Plugin Development' started by Dragono, May 22, 2016.

  1. I want to give items when somebody clicks on an item in the gui like a shop


    @EventHandler
    public void onArmorClick(InventoryClickEvent event) {
    if (!ChatColor.stripColor(event.getInventory().getName())
    .equalsIgnoreCase("Armor"))

    return;

    Player player = (Player) event.getWhoClicked();
    event.setCancelled(true);

    if (event.getCurrentItem() == null
    || event.getCurrentItem().getType() == Material.AIR
    || !event.getCurrentItem().hasItemMeta()) {
    player.closeInventory();
    return;
    }

    switch (event.getCurrentItem().getType())

    {

    case IRON_HELMET:
    event.getWhoClicked().getInventory().addItem(new ItemStack(Material.IRON_HELMET));
    player.closeInventory();
    break;

    default:

    player.closeInventory();
    break;

    }

    }
     
  2. Get the event entity's inventory, then add an ItemStack to the event entity's inventory, using #addItem
     
  3. Example. Just create an itemstack and apply it to their inv

    Code (Text):
    @EventHandler
        public void onClick(InventoryClickEvent e) {
            if (e.getInventory().getName().equals("Inv name")) {
                if (e.getSlot() == 1) {
                    e.setCancelled(true);
                    ItemStack stack = new ItemStack(Material.SNOW_BLOCK);
                    e.getWhoClicked().getInventory().addItem(stack);
                    e.getWhoClicked().closeInventory();
                }
            }
        }