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);