Cancel InventoryClickEvent

Discussion in 'Plugin Development' started by TheFl4me, Sep 11, 2016.

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

    TheFl4me

    I have a problem,

    The player will have a glass block on his head and he must not be allowed to remove that block in anyway.
    here is my issue:

    If the player is in GM Survival everything works fine but if he is in GM Creative it only partially works, by that I mean the first time he clicks the glass block it is cancelled, the second time he clicks it he can move it around freely .----.

    Code:
    @EventHandler
        public void dontmoveGlass(InventoryClickEvent e) {
            ePlayer p = ePlayer.get((Player) e.getWhoClicked());
            if(e.getSlotType() == InventoryType.SlotType.ARMOR && p.isInvis()) {
                e.setResult(Event.Result.DENY);
            }
        }
    Any idea as to what is wrong?
     
  2. Offline

    Zombie_Striker

  3. Offline

    TheFl4me

    Did not work.
     
  4. Offline

    ChipDev

    Errors? Console? Code? :l
     
  5. Offline

    TheFl4me

    I do not mean to be rude or anything but did you even read my initial post? If so you would understand why my last reply was "enough".
     
  6. Offline

    Zombie_Striker

    @TheFl4me
    Although the initial post has the code, it does not have what @ChipDev asked for. You do not say if there are any errors. If so, please post them.

    Can you try always canceling the event? If that fixes the problem, you issue was the placement of the cancel line. If not, then you issue is that the method itself is not firing when a player clicks the helmet twice.
     
  7. Offline

    TheFl4me

    Ok I understand the confusion.

    No, there is no error.

    Yes, I also tried cancelling the event, the outcome was the exact same.

    Also I do not think you fully understand the situation, the problem is not whenI double click the helmet, but when I cliked it the first time (the event is cancelled) now if I wait a few seconds and click it again the event is not cancelled and I can move the item whereever I want.
     
  8. Offline

    TookPepine

    This is always fun playing with this event...

    Okay so survival mode: OKAY
    Creative Mode: Problem.

    try to add this @EventHandler(priority = EventPriority.HIGHEST)

    + please can you add a debug message when you are cancelling the event. Message to the player or to the console. So we can see if the system read the second click in GM Creative.
     
  9. @TookPepine nope that won't do anything.

    @TheFl4me What you need to do is

    1: Write a click event. If gamemode of player equals gamemode you want, continue, do a for loop for each item, if that item's material equals the wanted material (NEVER COMPARE ITEMSTACKS IN THESE SITUATIONS), remove the item and place it back on the head. Now, when someone clicks the inventory, it is updated.
    2: Do the same, but with a close event.
    3: Now, write a drop event. Do the same gamemode check, but also check if the dropped item's itemstack has the same material and other checks. If so, remove the item (not itemstack) and place it back.
    4: Write a inventory drag event which will do the same as the click event; check gm, check If the item is there and then remove and replace.

    Along with GM, you might want to check the world. A bit lengthy, but trust me, looks a lot neater than canceling the event.
     
  10. Offline

    TookPepine

    @TheEnderCrafter9 What he's doing is pretty much what we call: Dead Code. + Using a loop for each click will cause a Server Performance if you have a lot of players.


    @TheFl4me Here a solution of your problem, I'm closing the player Inventory if he's in creative mode, I'll keep checking on it but sounds like the Creative Inventory doesn't update like the Survival ones.

    Code:
        @EventHandler
        public void dontmoveGlass(InventoryClickEvent e) {
            Player player = (Player) e.getWhoClicked();
            ItemStack item = e.getCurrentItem();
    
            if(item != null && item.getType() == Material.GLASS && e.getSlotType() == InventoryType.SlotType.ARMOR) {
                e.setResult(Result.DENY);
                if(player.getGameMode() == GameMode.CREATIVE)
                    player.closeInventory();
            }
        }
    
    + You had this :
    Code:
    e.getSlotType() == InventoryType.SlotType.ARMOR && p.isInvis()
    That will also block if the player will try to remove any armor on him. I changed this line so that it'll only cancel if the player try to remove the glass. And you can add your p.isInvis() at the end.

    You can use e.setResult(Result.DENY) or e.setCancelled(true)

    PS: Make sure to use a method to block the DROP EVENT + if he dies (PlayerDeathEvent) if you want them to keep the glass.
     
  11. Offline

    TheFl4me

    Will do for the playerdeathevent (I forgot about that, thanks).

    But I need a methode that does not close the inventory and that doesnt require as much performance as what @TheEnderCrafter9 recommended.

    (Sorry if I am being picky here xd)
     
  12. Offline

    TookPepine

    Yeah no worry, the thing is Creative Inventory for X reason doesn't react like the survival one. When I'll have the time, I'll try to find something out. (PS: right now only the creative Inventory will close)
     
    Last edited: Sep 13, 2016
  13. Offline

    TheFl4me

  14. Offline

    RenditionsRule

Thread Status:
Not open for further replies.

Share This Page