/*
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;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Team;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Objects;
import java.util.Set;
public class MWTeam {
private static final ItemStack bow = new ItemStack(Material.BOW);
static {
ItemMeta bowMeta = Objects.requireNonNull(bow.getItemMeta());
bowMeta.addEnchant(Enchantment.ARROW_FIRE, 1, true);
bowMeta.addEnchant(Enchantment.ARROW_KNOCKBACK, 1, true);
bowMeta.addEnchant(Enchantment.KNOCKBACK, 1, true);
bowMeta.addEnchant(Enchantment.DAMAGE_ALL, 2, true);
bowMeta.setUnbreakable(true);
bow.setItemMeta(bowMeta);
}
private final ChatColor color;
private final String teamName;
private final Team sbteam; //scoreboard-Team
private final Location spawn;
private final int portalZ;
private final LinkedList players = new LinkedList<>();
private final Set openInvitations = new HashSet<>();
MWTeam(ChatColor color, Location spawn, String teamName, int portalZ) {
this.teamName = teamName;
this.color = color;
this.spawn = spawn;
this.portalZ = portalZ;
if(FightScoreboard.getScoreboard().getTeam(teamName) == null)
sbteam = FightScoreboard.getScoreboard().registerNewTeam(teamName);
else
sbteam = FightScoreboard.getScoreboard().getTeam(teamName);
assert sbteam != null;
sbteam.setAllowFriendlyFire(false);
sbteam.setColor(color);
}
public void givePlayerItem(ItemStack item) {
Player p = players.removeFirst();
players.addLast(p);
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;
}
}
}
public void teamScoreboard(Objective objective) {
players.forEach(p -> objective.getScore(getColorCode() + p.getName()).setScore(1));
}
public int size() {
return players.size();
}
public int getPortalZ() {
return portalZ;
}
public Location getSpawn(){
return spawn;
}
public void join(Player p) {
players.add(p);
p.teleport(spawn);
p.setGameMode(GameMode.SURVIVAL);
p.getInventory().setItem(0, bow);
sbteam.addPlayer(p);
p.setDisplayName(color + p.getName());
if (MissileWars.getFightState() == FightState.WAITING && !enemy().players.isEmpty())
MissileWars.startRound();
}
public void leave(Player p) {
if (!players.contains(p)) return;
players.remove(p);
sbteam.removePlayer(p);
if (players.isEmpty() && MissileWars.getFightState() == FightState.FIGHTING)
MissileWars.end(WinReasons.NO_ENEMY, enemy());
}
public void invitePlayer(Player p) {
if (enemy().openInvitations.contains(p)) return;
openInvitations.add(p);
}
public void acceptInvite(Player p) {
removeInvitations(p);
join(p);
}
private MWTeam enemy() {
if (this == MissileWars.getRedTeam())
return MissileWars.getBlueTeam();
return MissileWars.getRedTeam();
}
public String getColorCode(){
return "ยง" + color.getChar();
}
public boolean hasPlayer(Player p) {
return players.contains(p);
}
public boolean hasInvite(Player p) {
return openInvitations.contains(p);
}
public String getColoredName() {
return color.toString() + teamName;
}
public static void removeInvitations(Player p) {
MissileWars.getRedTeam().openInvitations.remove(p);
MissileWars.getBlueTeam().openInvitations.remove(p);
}
}