Canceling Bukkit Runnable on Inventory Close

Discussion in 'Plugin Development' started by baighxansgaming, Aug 26, 2017.

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

    baighxansgaming

    I'm working on a plugin that requires me to have DIFFERENT animated lores for items in an inventory for EACH PLAYER, how would I go about detecting when they close the inventory and cancel the event?

    Thanks in advance!
     
  2. Offline

    krizzdawg

    InventoryCloseEvent

    for "canceling" :
    Code:
    new BukkitRunnable() {
    public void run() {
    player.openInventory(e.getInventory());
    }
    }.runTaskLater(plugin, 5);
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  3. Offline

    baighxansgaming

    @krizzdawg i realize that part, I can't figure out how to be able to cancel the specific players runnable

    It's a repeating task and I'm not sure how to reference it on the InventoryCloseEvent
     
  4. Offline

    krizzdawg

    Are you trying to cancel the Runnable or the Event?

    Nvm, I just reread the title.

    Add the player's uuid to a HashSet<UUID>

    In the runnable check if the player is in the HashSet.

    If not use cancel(); (if its a BukkitRunnable)
     
  5. Offline

    baighxansgaming

    @krizzdawg

    I'm trying to cancel the runnable for the specific player when they close the inventory.

    I'm using this and I'm not sure how to reference it later.
    Code:
    new BukkitRunnable() {...}.runTaskTimer(this, 0, 20);
    Nvm, I figured it out using a hashmap

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 26, 2017
  6. Offline

    krizzdawg

    A full example:
    Code:
                        HashSet<UUID> inventoryPlayers = new HashSet<UUID>();
    
    
        //When the inventory is opened
    
        new BukkitRunnable() {
    
            public void run() {
                if(inventoryPlayers.contains(player.getUniqueId())) {
                    updateLores():
                }else{
                    cancel();
                }
            }
        }.runTaskTimer(this, 0, 20);
       
        //When the inventory is closed
        inventoryPlayers.remove(player.getUniqueId());
       
    Another way is just do this:
    Code:
    //When inventory is opened.
            new BukkitRunnable() {
    
                public void run() {
                    if(player.getOpenInventory().getTitle().equalsIgnoreCase(YourInventoryTitle)) {
                        updateLores();
                    }else{
                        cancel();
                    }
                }
            }.runTaskTimer(this, 0, 20);
    
     
Thread Status:
Not open for further replies.

Share This Page