Minecraft Plugin Programming in Self-programmed Inventory Click Item and Run Command?
Here is the source code:
@Override
public boolean onCommand (CommandSender sender, Command cmd, String label, String [] args) {
Player p = (player) transmitter;
if (p.hasPermission ("rankup.open")) {
if ((sender instanceof Player)) {
if (args.length == 0) {
Inventory inventory = Bukkit.createInventory (null, 93, "§6§lRang Shop"); p.sendMessage ("§7You have the rank menu open!"); p.openInventory (inventory);
ItemStack cost1 = new ItemStack (Material.GOLD_NUGGET);
ItemMeta ITEMMETA = kost1.getItemMeta ();
ITEMMETA.setDisplayName ( "$ §4§l10.000");
kosten1.setItemMeta (ITEMMETA);
ItemStack cost2 = new ItemStack (Material.GOLD_NUGGET);
ItemMeta item = kost2.getItemMeta ();
item.setDisplayName ( "$ §4§l100.000");
kosten2.setItemMeta (item);
ItemStack cost3 = new ItemStack (Material.GOLD_NUGGET);
ItemMeta halt = kosten3.getItemMeta ();
halt.setDisplayName ( "$ §4§l1.000.000");
kosten3.setItemMeta (halt);
ItemStack diamond = new ItemStack (Material.DIAMOND_BLOCK);
ItemMeta itemMeta = diamond.getItemMeta ();
itemMeta.setDisplayName ("§b§lDiamond Rank");
diamond.setItemMeta (itemMeta);
inventory.setItem (11, diamond);
ItemStack ultra = new ItemStack (Material.EMERALD_BLOCK);
ItemMeta ItemMeta = ultra.getItemMeta ();
ItemMeta.setDisplayName ("§a§lUltra rank");
ultra.setItemMeta (ItemMeta);
inventory.setItem (13, ultra);
ItemStack supreme = new ItemStack (Material.PURPUR_BLOCK);
ItemMeta iTemMeta = supreme.getItemMeta ();
iTemMeta.setDisplayName ("§d§lSupreme Rank");
supreme.setItemMeta (iTemMeta);
inventory.setItem (15, supreme);
ItemStack glass = new ItemStack (Material.STAINED_GLASS_PANE);
ItemMeta itemmeta = glass.getItemMeta ();
itemmeta.setDisplayName ( "§6Ränge");
glass.setItemMeta (itemmeta);
inventory.setItem (0, glass);
inventory.setItem (1, glass);
inventory.setItem (2, glass);
inventory.setItem (3, glass);
inventory.setItem (4, glass);
inventory.setItem (5, glass);
inventory.setItem (6, glass);
inventory.setItem (7, glass);
inventory.setItem (8, glass);
inventory.setItem (9, glass);
inventory.setItem (10, glass);
inventory.setItem (12, glass);
inventory.setItem (14, glass);
inventory.setItem (16, glass);
inventory.setItem (17, glass);
inventory.setItem (18, glass);
inventory.setItem (19, glass);
inventory.setItem (20, cost1);
inventory.setItem (21, glass);
inventory.setItem (22, cost2);
inventory.setItem (23, glass);
inventory.setItem (24, cost3);
inventory.setItem (25, glass);
inventory.setItem (26, glass);
inventory.setItem (27, glass);
inventory.setItem (28, glass);
}
}
}
{
}
return true;
}
@EventHandler
public void onClick (InventoryClickEvent e) {
if (e.getInventory (). GetName (). Equals ("§6§lRang Shop")) {
if (e.getCurrentItem ()! = null && e.getCurrentItem (). GetType ()! = Material.AIR) {
e.setCancelled (true);
}
}
}
P.chat ( "/ COMMAND");
Sure, but I have an inventory, and I have three immovable items inside. And I want to know how it works, if you click on one of the items, the inventory closes and a command is executed.
In the InventoryClickEvent, just ask:
if (e.getCurrentItem (). GetType () == Material.TOTHY) {
p.performCommand ("COMMAND HERE HIN");
}
Of course you should exchange "Material.YESWHERE" for the right item.
After that you should not forget either
p.closeInventory ();
otherwise the command will be executed, but the inventory will remain open.
Since I see that "p" is not yet defined in the InventoryClickEvent, this can best be done at the top of the event.
Player p = (Player) e.getWhoClicked ();
If (e.getCurrentItem (). GetType () == Material.ITEM) {
player.closeInventory ();
player.chat ( "/ COMMAND");