package de.steamwar.misslewars.countdowns; import de.steamwar.misslewars.*; import de.steamwar.misslewars.items.SpecialItem; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.scheduler.BukkitTask; import java.util.EnumSet; public class ItemCountdown extends StateDependent { private BukkitTask task; public ItemCountdown() { super(EnumSet.of(FightState.FIGHTING)); } private void run(){ ItemStack item = SpecialItem.getRandomItem(); giveTeamItem(MissileWars.getBlueTeam(), item); giveTeamItem(MissileWars.getRedTeam(), item); } private void giveTeamItem(MWTeam team, ItemStack item){ for(Player p : team.getPlayers()){ givePlayerItem(p, item); } } private void givePlayerItem(Player p, ItemStack item){ Inventory inventory = p.getInventory(); for (int i = 0; i <= 35; i++) { //35 is the last normal inventory slot ItemStack itemStack = inventory.getItem(i); if (itemStack != null && itemStack.isSimilar(item) && itemStack.getAmount() != itemStack.getMaxStackSize()) { itemStack.setAmount(itemStack.getAmount() + item.getAmount()); inventory.setItem(i, itemStack); p.updateInventory(); return; } } for (int i = 0; i <= 35; i++) { //35 is the last normal inventory slot ItemStack itemStack = inventory.getItem(i); if (itemStack == null || itemStack.getType().equals(Material.AIR)) { inventory.setItem(i, item); p.updateInventory(); return; } } } @Override public void enable() { task = Bukkit.getScheduler().runTaskTimer(MissileWars.getPlugin(), this::run, Config.ItemTime, 0); } @Override public void disable() { if(!task.isCancelled()) task.cancel(); } }