/* This file is a part of the SteamWar software. Copyright (C) 2020 SteamWar.de-Serverteam This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ package de.steamwar.misslewars.countdowns; import de.steamwar.misslewars.*; import de.steamwar.misslewars.items.Missile; 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.*; public class ItemCountdown extends StateDependent { private BukkitTask task; private Random random = new Random(); public ItemCountdown() { super(EnumSet.of(FightState.FIGHTING)); } private void run(){ int items = Math.max(MissileWars.getBlueTeam().getPlayers().size(), MissileWars.getRedTeam().getPlayers().size()); List itemList = new ArrayList<>(); for (int i = 0; i < items; i++) { itemList.add(SpecialItem.getRandomItem()); } giveTeamItem(MissileWars.getBlueTeam(), itemList); giveTeamItem(MissileWars.getRedTeam(), itemList); } private void giveTeamItem(MWTeam team, List itemList) { List itemStackList = new ArrayList<>(itemList); for (Player p : team.getPlayers()) { ItemStack itemStack = itemStackList.remove(random.nextInt(itemStackList.size())); givePlayerItem(p, itemStack); } } 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, 0, Config.ItemTime); } @Override public void disable() { if(task != null && !task.isCancelled()) task.cancel(); } }