InventoryClickEvent Does Not Work

Discussion in 'Plugin Development' started by inksack, May 29, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    inksack

    Hello, first, sorry for my poor english.
    I compilled a plugin, and InventoryClickEvent in this plugin is not working correctly.
    Code:java
    1. import org.bukkit.Bukkit;
    2. import org.bukkit.Material;
    3. import org.bukkit.command.Command;
    4. import org.bukkit.command.CommandSender;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.inventory.InventoryClickEvent;
    8. import org.bukkit.inventory.Inventory;
    9. import org.bukkit.inventory.ItemStack;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class PBGun extends JavaPlugin {
    13. public static Inventory myInventory = Bukkit.createInventory(null, 9, "My custom Inventory!");
    14. // The first parameter, is the inventory owner. I make it null to let everyone use it.
    15. //The second parameter, is the slots in a inventory. Must be a multiple of 9. Can be up to 54.
    16. //The third parameter, is the inventory name. This will accept chat colors
    17. static {
    18. myInventory.setItem(0, new ItemStack(Material.DIRT, 1));
    19. myInventory.setItem(8, new ItemStack(Material.GOLD_BLOCK, 1));
    20. }
    21.  
    22.  
    23.  
    24. public void onInventoryClick(InventoryClickEvent event) {
    25. Player player = (Player) event.getWhoClicked(); // The player that clicked the item
    26. ItemStack clicked = event.getCurrentItem(); // The item that was clicked
    27. Inventory inventory = event.getInventory(); // The inventory that was clicked in
    28. if (inventory.getName().equals(myInventory.getName())) { // The inventory is our custom Inventory
    29. if (clicked.getType() == Material.DIRT) { // The item that the player clicked it dirt
    30. event.setCancelled(true); // Make it so the dirt is back in its original spot
    31. player.closeInventory(); // Closes there inventory
    32. player.getInventory().addItem(new ItemStack(Material.DIRT, 1)); // Adds dirt
    33. player.sendMessage("LOL");
    34. }
    35. }
    36. }
    37.  
    38.  
    39. public boolean onCommand(CommandSender sender, Command cmd, String label,
    40. String[] a) {
    41. final Player player = (Player) sender;
    42. if (cmd.getName().equalsIgnoreCase("mj")) {
    43. player.openInventory(myInventory);
    44. }
    45. return false;
    46. }}
    47.  


    And when I click the dirt in the inventory, nothing happenes. Please, help. Thank you
     
  2. Offline

    TrollTaylor

    Put @EventHandler above your onInventoryClick
     
  3. Offline

    fireblast709

    And register your listener
     
  4. Offline

    glen3b

    First of all, you should really say what "doesn't work."

    Second of all, never call Bukkit API before onEnable is called.

    The Inventory cannot be created in a static field or initializer, as the CraftBukkit server variable in the static Bukkit class will not have been assigned.

    Therefore, you must override onEnable and create your inventory and set the elements in there. You also must register your event listener (the main plugin class, which should implement Listener) in onEnable and you must add the @EventHandler annotation above your onInventoryClick method.
     
  5. Offline

    Plo124

    inksack
    I fixed your code for you:
    Code:
    import org.bukkit.Bukkit;
            import org.bukkit.Material;
            import org.bukkit.command.Command;
            import org.bukkit.command.CommandSender;
            import org.bukkit.entity.Player;
            import org.bukkit.event.EventHandler;
            import org.bukkit.event.inventory.InventoryClickEvent;
            import org.bukkit.inventory.Inventory;
            import org.bukkit.inventory.ItemStack;
            import org.bukkit.plugin.java.JavaPlugin;
            public class PBGun extends JavaPlugin implements Listener {
                public static Inventory myInventory;
                // The first parameter, is the inventory owner. I make it null to
                // let everyone use it.
                // The second parameter, is the slots in a inventory. Must be a
                // multiple of 9. Can be up to 54.
                // The third parameter, is the inventory name. This will accept chat
                // colors
                @Override
                            public void onEnable() {
                                    myInventory = Bukkit.createInventory(null,
                        9, "My custom Inventory!");
                    myInventory.setItem(0, new ItemStack(Material.DIRT, 1));
                    myInventory.setItem(8, new ItemStack(Material.GOLD_BLOCK, 1));
                                    Bukkit.getPluginManager().registerEvents(this,this);
                }
                            @EventHandler
                public void onInventoryClick(InventoryClickEvent event) {
                    Player player = (Player) event.getWhoClicked(); // The player
                                                                    // that clicked
                                                                    // the item
                    ItemStack clicked = event.getCurrentItem(); // The item that was
                                                                // clicked
                    Inventory inventory = event.getInventory(); // The inventory
                                                                // that was clicked
                                                                // in
                    if (inventory.getName().equals(myInventory.getName())) { // The
                                                                                // inventory
                                                                                // is
                                                                                // our
                                                                                // custom
                                                                                // Inventory
                        if (clicked.getType() == Material.DIRT) { // The item that
                                                                    // the player
                                                                    // clicked it
                                                                    // dirt
                            event.setCancelled(true); // Make it so the dirt is back
                                                        // in its original spot
                            player.closeInventory(); // Closes there inventory
                            player.getInventory().addItem(
                                    new ItemStack(Material.DIRT, 1)); // Adds dirt
                            player.sendMessage("LOL");
                        }
                    }
                }
     
                public boolean onCommand(CommandSender sender, Command cmd,
                        String label, String[] a) {
                    final Player player = (Player) sender;
                    if (cmd.getName().equalsIgnoreCase("mj")) {
                        player.openInventory(myInventory);
                    }
                    return false;
                }
            }
     
  6. Offline

    inksack

    Plo124
    Thanks A LOT! Now it works!
     
Thread Status:
Not open for further replies.

Share This Page