0

I am working on a plugin that allows players to fly at the cost of a item.

I have this code that works but only if the item is in the first slot of the players inventory (Hotbar) and no where else.

To enable fly I have a command class that checks the players inventory for redstone and if they have it, They are added to an arrayList based on their UUID. Inside of my onEnable I have a TaskTimer that runs continually and checks for players in the arrayList.

Bukkit.getScheduler().runTaskTimerAsynchronously(this, () -> {
            for (Player player : Bukkit.getOnlinePlayers()) {
                if (Fly.flyingPlayers.contains(player.getUniqueId())) {
                    for (int i = 0; i < player.getInventory().getSize(); i++) {
                        ItemStack item = player.getInventory().getItem(i);
                        if (item !=null && item.getType().equals(Material.REDSTONE)) {
                            int amount = item.getAmount() -1;
                            item.setAmount(amount);
                            player.getInventory().setItem(i, amount > 0 ? item : null);
                            player.updateInventory();
                            break;
                        } else {
                            Fly.flyingPlayers.remove(player.getUniqueId());
                            player.setAllowFlight(false);
                            player.setFlying(false);
                            player.sendMessage(chatColor("" + this.getConfig().getString("Prefix") + "&4You are out of fuel"));
                            break;
                        }
                    }
                }
            }
        }, 20L, 20L);
Mike
  • 1
  • 1

1 Answers1

0

The issue comes from the if. If it's redstone, it will remove the item, else it will instantly think that it's finished instead of going to next slots.

You should do something like that:

Bukkit.getScheduler().runTaskTimerAsynchronously(this, () -> {
    for (Player player : Bukkit.getOnlinePlayers()) {
        if (Fly.flyingPlayers.contains(player.getUniqueId())) {
             boolean hasEdited = false;
             for (int i = 0; i < player.getInventory().getSize(); i++) {
                  ItemStack item = player.getInventory().getItem(i);
                  if (item != null && item.getType().equals(Material.REDSTONE)) {
                       int amount = item.getAmount() -1;
                       item.setAmount(amount);
                       player.getInventory().setItem(i, amount > 0 ? item : null);
                       player.updateInventory();
                       hasEdited = true; // save that it have been edited
                       break; // break the for loop
                  }
              }
              if(hasEdited) // edited in for loop
                  continue; // go to next player
              Fly.flyingPlayers.remove(player.getUniqueId());
              player.setAllowFlight(false);
              player.setFlying(false);
              player.sendMessage(chatColor(this.getConfig().getString("Prefix") + "&4You are out of fuel"));
         }
    }
}, 20L, 20L);
Elikill58
  • 4,050
  • 24
  • 23
  • 45