Solved Scheduler Bug

Discussion in 'Plugin Development' started by ByLogix, May 15, 2018.

  1. Offline

    ByLogix

    Hello,
    I want to open an Inventory if the Player sneaks and hits in the air, but at the beginning, the inventory will be
    empty and I will it with a scheduler slot for slot ("animated") with glass panes and play for every item a sound
    now my problem: every time when I close this inventory the scheduler keeps playing the sound till the inventory was full if it isn't closed.

    Code:
    @EventHandler
        public void openInventory(final PlayerInteractEvent e) {
            final Player p = (Player) e.getPlayer();
    
            if (getConfigEditor(p).getString("Mode").equalsIgnoreCase("TEAM")) {
                if (p.isSneaking()) {
                    if(e.getAction() == Action.LEFT_CLICK_AIR) {
                        new BukkitRunnable() {
                            @Override
                            public void run() {
                                e.getPlayer().closeInventory();
    
                                final Inventory inv = new InventoryBuilder(null, 9 * 5, "§aVerwaltung").toInventory();
                                p.openInventory(inv);
    
                                new BukkitRunnable() {
    
                                    int current = 0;
    
                                    @Override
                                    public void run() {
                                        try {
                                            if (p.getOpenInventory().getTopInventory() != null) {
                                                p.playSound(p.getLocation(), Sound.CHICKEN_EGG_POP, 1, 2);
                                                inv.setItem(current, new ItemBuilder(Material.STAINED_GLASS_PANE, 1, GlassID.LIME).setName("§6").toItemStack());
                                                current++;
                                            }
                                        } catch (Exception e1) {
                                            inv.setItem(ChestSlot.getSlot(2, 4), new ItemBuilder(Material.WATCH).setName("§aUhrzeit").setLore("§7Ändere die Uhrzeit.").toItemStack());
                                            inv.setItem(ChestSlot.getSlot(2, 6), new ItemBuilder(Material.DIAMOND_PICKAXE).setName("§aGamemode").setLore("§7Ändere die Spielmodi von Spielern.").toItemStack());
                                            this.cancel();
                                        }
                                        if (p.getOpenInventory().getTopInventory() != null) {
                                            p.updateInventory();
                                        }
                                    }
                                }.runTaskTimer(Main.plugin, 0, 1);
                            }
                        }.runTaskLater(Main.plugin, 2);
                    }
                }
            }
        }

    Now, where is the problem??






    (Sorry for my bad English, I'm German.)
     
  2. Online

    timtower Ninja on the waves Moderator

    @ByLogix Did you try printing the name of the top inventory? Or the contents?
     
  3. Offline

    ByLogix

    No, but now and the problem is solved, an inventory "container.crafting" is open when no inventory is open...
     
  4. Online

    timtower Ninja on the waves Moderator

    @ByLogix That indeed explains the issue. Good find.
     

Share This Page