SteamWar/MissileWars
Archiviert
13
0
Dieses Repository wurde am 2024-08-05 archiviert. Du kannst Dateien ansehen und es klonen, aber nicht pushen oder Issues/Pull-Requests öffnen.
MissileWars/src/de/steamwar/misslewars/countdowns/ItemCountdown.java

95 Zeilen
3.0 KiB
Java

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;
import de.steamwar.misslewars.*;
2020-09-05 11:44:38 +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-05 11:44:38 +02:00
import java.util.*;
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-05 11:44:38 +02:00
int items = Math.max(MissileWars.getBlueTeam().getPlayers().size(), MissileWars.getRedTeam().getPlayers().size());
List<ItemStack> itemList = new ArrayList<>();
for (int i = 0; i < items; i++) {
itemList.add(SpecialItem.getRandomItem());
}
giveTeamItem(MissileWars.getBlueTeam(), itemList);
giveTeamItem(MissileWars.getRedTeam(), itemList);
2020-06-24 15:26:10 +02:00
}
2020-09-05 11:44:38 +02:00
private void giveTeamItem(MWTeam team, List<ItemStack> itemList) {
List<ItemStack> itemStackList = new ArrayList<>(itemList);
for (Player p : team.getPlayers()) {
ItemStack itemStack = itemStackList.remove(random.nextInt(itemStackList.size()));
givePlayerItem(p, itemStack);
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();
}
}