How can I cancel inventory click event?

Discussion in 'Plugin Development' started by JabberJay, May 2, 2013.

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

    JabberJay

    Ok, so I've done some research into trying to cancel an inventory click event but it just doesn't seem to work, heres the code:
    Code:
    package me.Jabber;
     
    import java.util.logging.Logger;
     
     
    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.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.inventory.InventoryType;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.InventoryView;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Invshop extends JavaPlugin implements Listener{
        public final Logger logger = Logger.getLogger("Minecraft");
        public static Inventory invShop;
       
        @Override
        public void onDisable() {
           
        }
       
        @Override
        public void onEnable() {
            invShop = Bukkit.createInventory(null, 63, "Inv Shop");
            getServer().getPluginManager().registerEvents(this, this);
           
        }
       
       
        @EventHandler
        void InventoryClickEvent(InventoryView what, InventoryType.SlotType type, int slot, boolean right, boolean shift, boolean isRightClick, InventoryClickEvent event) {
            if (isRightClick) {
                  event.setCancelled(true);
            }
        }
       
          @Override
          public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
           
            if(commandLabel.equalsIgnoreCase("shop")){
                if(sender instanceof Player){
                    ((Player) sender).openInventory(invShop);
                    invShop.clear();
                    invShop.addItem(new ItemStack(Material.APPLE));
                }
            }
           
            return false;
        }
       
     
     
    }
    
    As you can see, when someone does /shopit brings up a virtual inventory with 1 apple inside of it, I want to make it so no one can take any item from the inventory out or put anything in, thanks for your help.
     
  2. Events *must* have only 1 argument - the event object, in your case InventoryClickEvent. The rest of the stuff are gatherable from that object.
     
  3. Offline

    JabberJay

    Digi Ah ok, I have just tried this:.
    Code:
        @EventHandler
        public void InventoryClickEvent(InventoryClickEvent event) {
            if (event.getInventory() == invShop) {
                  event.setCancelled(true);
            }
        }
    But it still doesn't seem to cancel the event, have you any suggestions? Thanks for the help I really appreciate it.
     
  4. JabberJay
    The inventory you're getting is never going to be the same instance as the one you created, so your '==' condition is never true... try printing some messages before conditions, don't code blindly :)

    To check if it's your inventory you must give it an unique name then check inventory titles with equals().
     
  5. Offline

    JabberJay

    Digi Many thanks, seems to work with:
    Code:
        @EventHandler
        public void InventoryClickEvent(InventoryClickEvent event) {
            if (event.getInventory().getTitle().equals("Inv Shop")) {
                if(event.isRightClick() || event.isLeftClick()){
                    event.setCancelled(true);
                }
            }
            return;
        }
    My next mission is to figure out how to put items in different slots, lol. Thanks again.
     
  6. Offline

    Harry5573

    JabberJay people can get stuff out with things other than shift click this could make dupes. This will work better for you :
    Code:
     @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
          Player player = event.getWhoClicked();
            if (event.getInventory().getTitle().equals("Inv Shop")) {
                              event.setCancelled(true);
                              player.updateInventory();
                                 
                }
            }
            return;
        }
    For the specific item
    [​IMG]
    Code:
    Itemstack invslot1 = new ItemStack(Material.APPLE, 1); .//Will get an apple
    invShop().setItem(Slot number from the image above, new (invslot1)); .//May have to fix some brackets
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  7. Offline

    JabberJay

    Harry5573
    It seems
    Code:
    player.updateInventory();
    is outdated
     
  8. Offline

    Harry5573

    Still works everyone uses it it basically instantly updates it otherwise the item may linger in there in and reloads can dupe it
     
  9. Offline

    shawn99

    hey im trying to make my self a gui but i tryed both the code that you guys said but was still able to move the items around my inventry and take it out the gui slot.

    public class GUI extends JavaPlugin{

    public static GUI instence;
    public final static Logger log = Logger.getLogger("Minecraft");
    public FileConfiguration config;


    public void onEnable(){

    this.saveDefaultConfig();
    GUI.instence = this;


    }

    public void onDisable(){



    }

    public void log(String s, Level l){
    GUI.log.log(l, "[GUI] " +s);

    }

    @EventHandler
    public void InventoryClickEvent(InventoryClickEvent event) {
    if (event.getInventory().getTitle().equals("Inv Shop")) {
    if(event.isRightClick() || event.isLeftClick()){
    event.setCancelled(true);
    }
    }
    return;
    }





    public boolean onCommand(CommandSender theSender, Command cmd, String commandLabel, String[] args)
    {
    Player player = (Player) theSender;

    if(commandLabel.equalsIgnoreCase("gui")){
    if(player.hasPermission("gui.gui")){
    player.sendMessage(ChatColor.RED+"[DyreRank]"+ChatColor.AQUA + " Opening Rank GUI");
    ItemStack row1 = new ItemStack(Material.getMaterial(this.getConfig().getString("Menu.collum1")));
    ItemStack row2 = new ItemStack(Material.getMaterial(this.getConfig().getString("Menu.collum2")));
    ItemStack row3 = new ItemStack(Material.getMaterial(this.getConfig().getString("Menu.collum3")));
    ItemStack row4 = new ItemStack(Material.getMaterial(this.getConfig().getString("Menu.collum4")));
    Inventory inv = Bukkit.createInventory(null, 9, ChatColor.GREEN + "Gui");
    inv.setItem(0, row1);
    inv.setItem(1, row2);
    inv.setItem(2, row3);
    inv.setItem(3, row4);

    player.openInventory(inv);

    }else{
    player.sendMessage("You dont have perm");
    }
    }
    return false;
    }

    }
     
  10. Offline

    Niknea

    shawn99 Sir, you have just bumped a 8 month old thread, and please put your code in the Java syntax form so users can read your code better and help you faster. For your problem you will need to add this line of code in your plugin.
    Code:java
    1. if (!e.getInventory().getName().equalsIgnoreCase(/* ItemGUI Name */.getName())) return;
     
  11. Offline

    shawn99

    This is the code i am currently using everything works apart from the event to cancel the click event to stop the player being able to move the item from there inventory


    Code:
    package me.Hackerpro.Gui;
     
    import java.util.logging.Level;
    import java.util.logging.Logger;
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    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;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class GUI extends JavaPlugin implements Listener{
     
        public static GUI instence;
        public final static Logger log = Logger.getLogger("Minecraft");
        public FileConfiguration config;
        public static Inventory inv;
     
        public void onEnable(){
            inv = Bukkit.createInventory(null, 9, ChatColor.GREEN + "Gui");
            this.saveDefaultConfig();
            GUI.instence = this;
            getServer().getPluginManager().registerEvents(this, this);
     
        }
     
        public void onDisable(){
     
     
     
        }
     
        public void log(String s, Level l){
            GUI.log.log(l, "[GUI] " +s);
     
        }
     
     
     
     
     
     
     
     
        public boolean onCommand(CommandSender theSender, Command cmd, String commandLabel, String[] args)
        {
            Player player = (Player) theSender;
     
            if(commandLabel.equalsIgnoreCase("gui")){
                if(player.hasPermission("gui.gui")){
                    player.sendMessage(ChatColor.RED+"[DyreRank]"+ChatColor.AQUA + " Opening Rank GUI");
                    ItemStack row1 = new ItemStack(Material.getMaterial(this.getConfig().getString("Menu.collum1")));
                    ItemStack row2 = new ItemStack(Material.getMaterial(this.getConfig().getString("Menu.collum2")));
                    ItemStack row3 = new ItemStack(Material.getMaterial(this.getConfig().getString("Menu.collum3")));
                    ItemStack row4 = new ItemStack(Material.getMaterial(this.getConfig().getString("Menu.collum4")));
                    player.openInventory(inv);
                    inv.clear();
     
                    inv.setItem(0, row1);
                    inv.setItem(1, row2);
                    inv.setItem(2, row3);
                    inv.setItem(3, row4);
     
     
     
     
                }else{
                    player.sendMessage("You dont have perm");
                }
            }
            return false;
        }
     
        @EventHandler
        public void OnInventoryClick(InventoryClickEvent event){
            if(event.getInventory() == inv){
                if(event.getCurrentItem().getType() == Material.getMaterial(this.getConfig().getString("Menu.collum1"))){
                    event.setCancelled(true);
                    event.getWhoClicked().getInventory().addItem(new ItemStack(Material.getMaterial("Menu.collum1")));
     
                }
            }
     
        }
    }
     
    
    -Shawn
     
  12. Offline

    Monkeyboystein

    OK, first off, why would you bump an 8 month old post, 2nd why wouldnt you just make your own post?
    We're very nice around here; we dont byte(intended pun),
     
    AoH_Ruthless likes this.
  13. Offline

    AoH_Ruthless

    Lol
     
Thread Status:
Not open for further replies.

Share This Page