Is there a smarter Way to solve this?

Discussion in 'Spigot Plugin Help' started by Parzival_, Feb 9, 2020.

  1. I m a beginner with Spigot API, i mean i started plugin coding yesterday.
    Is there a smarter way when opening an Inventory with a command and then clicking on the Item to get it in your own Inventory? i had to create the Item twice to get it working. here is my code:
    Code (Java):
    public class KitCommand implements CommandExecutor, Listener {

        Inventory inventory = Bukkit.createInventory(null, 9*3, "§6§lKit-Inventar");  
        @EventHandler
        public void handleInventoryClick(InventoryClickEvent event) {
            Player player = (Player) event.getWhoClicked();
            if(event.getClickedInventory().equals(inventory)) {
                event.setCancelled(true);
                ItemStack item = new ItemStack(Material.IRON_PICKAXE);
                ItemMeta itemMeta = item.getItemMeta();
                itemMeta.setDisplayName("§6Spitzhacke der Verdammung");
                itemMeta.addEnchant(Enchantment.KNOCKBACK, 10, true);
                item.setItemMeta(itemMeta);
                player.getInventory().addItem(item);      
            }
        }
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(sender instanceof Player) {
                Player player = (Player) sender;
                if(player.hasPermission("pluginzboi.kit")) {
                    if(args.length == 0) {              
                        ItemStack item = new ItemStack(Material.IRON_PICKAXE);
                        ItemMeta itemMeta = item.getItemMeta();
                        itemMeta.setDisplayName("§6Spitzhacke der Verdammung");
                        itemMeta.addEnchant(Enchantment.KNOCKBACK, 10, true);
                        item.setItemMeta(itemMeta);              
                        inventory.setItem(13, item);          
                        player.openInventory(inventory);
                        player.sendMessage("§aWähle ein Kit!");      
                    } else
                        player.sendMessage("§cBitte benutze §6/kit$c!");
                } else
                    player.sendMessage("§cDu hast dazu keine Rechte!");
            }  
            return false;
        }
    }