Plugin InventoryClickEvent Issue.

Discussion in 'Plugin Development' started by Ciscada, May 5, 2020.

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

    Ciscada

    Hello! So I'm totally new to coding plugins, however, I'm not new to coding in general, I rarely ask for help on forums (In fact never did) but I must admit for once, I got no fixes out of this sh*t! :p

    Here's my problem, I'm coding an addon that allows you to create Gangs / Teams and Turfs (Zones) which customizable settings such as Names, Colors, Logos, Leaders, etc ...
    I'm currently coding this "Editmode" of this plugin, which allows its user to create Turfs to be captured by any Gangs / Teams. In this Editmode, a wand is given to the user, the wand allows you to set the dimension of the turf, etc. My problem is, I want to STOP the user from moving the wand from their inventory, I've already coded the PlayerDropItemEvent which works like a charm, however. I made an InventoryClickEvent to check whenever the user attempts to click the wand to move it to another slot, by default the wand is given on the first slot of the Hotbar (Slot 0) which is easy right, I just gotta check if the items originally comes from slot 0 which works, however, it also stops other items from moving, even tho I add 3-5 if statements to check if its the right item, it always stops it...

    Here's my code:
    Code:
        @EventHandler
        public void onIventoryClick(InventoryClickEvent Event)
        {
            Player Player = (Player) Event.getWhoClicked();
            Material Item = Event.getCurrentItem().getType();
            if(ArrayClass.Editmode_Players.contains(Player.getName())){
                if (Item != null || Item != Material.AIR || Event.getSlot() == 0){
                    Event.setCancelled(true);
                }
            } else {
                Event.setCancelled(false);
            }
        }
    
    Go ahead and ask any information you may need/like to help me out with this.
    Thanks for everyone who'll try to help me out! :D
     
  2. Offline

    KarimAKL

    @Ciscada
    1. Follow the Java naming conventions.
    2. The clicked item as well as the clicked inventory can be null, check for that before accessing it.
    3. You can check if the item is your wand instead of checking the slot.
    4. The item's material will never be null, and it can't be AIR, in this case; the item would be null if there's no item in the clicked slot.
    5. There's no need to set the cancelled state to false in the else statement.

    Let me know about your progress after you've changed some of your code.
     
  3. Offline

    Ciscada

    @KarimAKL
    Hello sir! Thanks you for your help!
    However, I've already checked / tried everything you've said! I didn't say it but I've tried a lot of different codes and modified them, it never works. But here, lemme show you the code I've tried:

    Code:
        @EventHandler
        public void onIventoryClick(InventoryClickEvent Event)
        {
            Player Player = (Player) Event.getWhoClicked();  //WHO CLICK THE ITEM
            Material Item = Event.getCurrentItem().getType();  //GETTING WHAT WAS CLICKED
            if (Item != null || Item != Material.AIR){    //NO NEED TO STOP THEM FROM REMOVEING AIR LOLz
                if(ArrayClass.Editmode_Players.contains(Player.getName())) {
                    ItemStack Wand = new ItemStack(Material.getMaterial(JSONClass.getStringData("wand", Config.getConfigFile())));
                    if(Item == Wand.getType()){
                        if(Event.getCurrentItem().getItemMeta().getDisplayName().equals("GAT's Magical Wand")) {
                            Event.setCancelled(true);
                        }
                    }
                }
            }
        }
    
     
  4. Offline

    KarimAKL

    @Ciscada It seems you've ignored 3/5 of my pointers; please read them again.

    Try debugging, and then tell me what the result is (with your debug code as well).
     
  5. Offline

    Ciscada

    @KarimAKL
    Um, i did not ignore the pointers. Look.
    3. if(Item == Wand.getType()).
    4. I letter changed to LEGACY_AIR cause yes I did "debug" my code with printing statements and it detects the Item type as "LEGACY_AIR".
    5. I did remove the else statement.

    However, my code doesn't return any errors. Will a Print Statement be enough for a "debug"?
     
  6. Offline

    KarimAKL

    That's the 3 i meant.

    Anyway, which version are you developing for? If it's 1.13 or higher, you'll have to include 'api-version: 1.13' in your plugin.yml file.
     
  7. Offline

    Ciscada

    @KarimAKL

    Oh yes my bad, i just wanted to get my code going before like completely finishing it!
    But I'm running the lastest Bukkit version for 1.15.2.

    So do I need to do

    'api-version: 1.15'?
     
  8. Offline

    timtower Administrator Administrator Moderator

    Yes
     
Thread Status:
Not open for further replies.

Share This Page