2020-09-04 16:12:14 +02:00
|
|
|
/*
|
|
|
|
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 <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-06-24 15:26:10 +02:00
|
|
|
package de.steamwar.misslewars.countdowns;
|
|
|
|
|
2020-09-26 11:39:25 +02:00
|
|
|
import de.steamwar.misslewars.Config;
|
|
|
|
import de.steamwar.misslewars.FightState;
|
|
|
|
import de.steamwar.misslewars.MissileWars;
|
|
|
|
import de.steamwar.misslewars.StateDependent;
|
2020-09-26 12:40:03 +02:00
|
|
|
import de.steamwar.misslewars.items.Missile;
|
2020-06-24 15:26:10 +02:00
|
|
|
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;
|
|
|
|
|
2020-09-26 11:39:25 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.EnumSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Random;
|
2020-06-24 15:26:10 +02:00
|
|
|
|
|
|
|
public class ItemCountdown extends StateDependent {
|
|
|
|
|
|
|
|
private BukkitTask task;
|
2020-09-05 11:44:38 +02:00
|
|
|
private Random random = new Random();
|
2020-06-24 15:26:10 +02:00
|
|
|
|
|
|
|
public ItemCountdown() {
|
|
|
|
super(EnumSet.of(FightState.FIGHTING));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void run(){
|
2020-09-26 12:40:03 +02:00
|
|
|
int items = Math.max(MissileWars.getBlueTeam().getPlayers().size(), MissileWars.getRedTeam().getPlayers().size());
|
2020-09-26 11:39:25 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < items; i++) {
|
|
|
|
ItemStack itemStack = SpecialItem.getRandomItem();
|
2020-06-24 15:26:10 +02:00
|
|
|
|
2020-09-26 12:40:03 +02:00
|
|
|
Player blue = MissileWars.getBlueTeam().getPlayers().removeFirst();
|
|
|
|
givePlayerItem(blue, itemStack);
|
|
|
|
MissileWars.getBlueTeam().getPlayers().addLast(blue);
|
|
|
|
|
|
|
|
Player red = MissileWars.getRedTeam().getPlayers().removeFirst();
|
|
|
|
givePlayerItem(red, itemStack);
|
|
|
|
MissileWars.getBlueTeam().getPlayers().addLast(red);
|
2020-06-24 15:26:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2020-06-24 18:24:00 +02:00
|
|
|
task = Bukkit.getScheduler().runTaskTimer(MissileWars.getPlugin(), this::run, 0, Config.ItemTime);
|
2020-06-24 15:26:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void disable() {
|
2020-06-24 18:24:00 +02:00
|
|
|
if(task != null && !task.isCancelled())
|
2020-06-24 15:26:10 +02:00
|
|
|
task.cancel();
|
|
|
|
}
|
|
|
|
}
|