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/MWTeam.java

180 Zeilen
5.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-05-27 17:52:55 +02:00
package de.steamwar.misslewars;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
2020-05-27 17:52:55 +02:00
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
2020-09-26 20:43:50 +02:00
import org.bukkit.inventory.Inventory;
2020-05-27 17:52:55 +02:00
import org.bukkit.inventory.ItemStack;
2020-06-24 15:26:10 +02:00
import org.bukkit.inventory.meta.ItemMeta;
2020-09-26 21:11:34 +02:00
import org.bukkit.scoreboard.Objective;
2020-05-27 17:52:55 +02:00
import org.bukkit.scoreboard.Team;
import java.util.HashSet;
2020-09-26 20:47:12 +02:00
import java.util.LinkedList;
import java.util.Objects;
import java.util.Set;
2020-05-27 17:52:55 +02:00
public class MWTeam {
private static final ItemStack bow = new ItemStack(Material.BOW);
static {
2020-06-24 15:26:10 +02:00
ItemMeta bowMeta = Objects.requireNonNull(bow.getItemMeta());
2020-06-27 20:38:50 +02:00
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);
2020-06-24 15:26:10 +02:00
bowMeta.setUnbreakable(true);
bow.setItemMeta(bowMeta);
2020-05-27 17:52:55 +02:00
}
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<Player> players = new LinkedList<>();
private final Set<Player> openInvitations = new HashSet<>();
2020-05-27 17:52:55 +02:00
2020-06-24 15:26:10 +02:00
MWTeam(ChatColor color, Location spawn, String teamName, int portalZ) {
2020-05-27 17:52:55 +02:00
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);
}
2020-09-26 20:45:37 +02:00
public void givePlayerItem(ItemStack item) {
2020-09-26 20:43:50 +02:00
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;
}
}
}
2020-09-26 21:11:34 +02:00
public void teamScoreboard(Objective objective) {
players.forEach(p -> objective.getScore(getColorCode() + p.getName()).setScore(1));
2020-09-26 21:11:34 +02:00
}
2020-09-26 20:43:50 +02:00
public int size() {
return players.size();
}
2020-05-27 17:52:55 +02:00
public int getPortalZ() {
return portalZ;
}
public Location getSpawn(){
return spawn;
}
2020-09-26 12:20:16 +02:00
public void join(Player p) {
2020-05-27 17:52:55 +02:00
players.add(p);
p.teleport(spawn);
2020-06-27 20:38:50 +02:00
p.setGameMode(GameMode.SURVIVAL);
2020-06-24 15:26:10 +02:00
p.getInventory().setItem(0, bow);
2020-05-27 17:52:55 +02:00
sbteam.addPlayer(p);
2020-06-24 20:36:43 +02:00
p.setDisplayName(color + p.getName());
2020-09-26 20:43:50 +02:00
if (MissileWars.getFightState() == FightState.WAITING && !enemy().players.isEmpty())
2020-05-27 17:52:55 +02:00
MissileWars.startRound();
}
2020-09-26 12:20:16 +02:00
public void leave(Player p) {
if (!players.contains(p)) return;
2020-05-27 17:52:55 +02:00
players.remove(p);
2021-01-26 11:14:46 +01:00
p.setDisplayName("§7" + p.getName());
2020-05-27 17:52:55 +02:00
sbteam.removePlayer(p);
2020-06-24 15:26:10 +02:00
if (players.isEmpty() && MissileWars.getFightState() == FightState.FIGHTING)
2020-05-27 17:52:55 +02:00
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);
}
2020-05-27 17:52:55 +02:00
2020-06-24 15:26:10 +02:00
private MWTeam enemy() {
2020-05-27 17:52:55 +02:00
if (this == MissileWars.getRedTeam())
return MissileWars.getBlueTeam();
return MissileWars.getRedTeam();
}
public String getColorCode(){
2020-05-27 17:52:55 +02:00
return "§" + color.getChar();
}
public boolean hasPlayer(Player p) {
2020-05-27 17:52:55 +02:00
return players.contains(p);
}
public boolean hasInvite(Player p) {
return openInvitations.contains(p);
}
2020-05-27 17:52:55 +02:00
public String getColoredName() {
return color.toString() + teamName;
}
2020-08-30 21:37:10 +02:00
2020-12-20 14:19:42 +01:00
public LinkedList<Player> getPlayers() {
return players;
}
public static void removeInvitations(Player p) {
MissileWars.getRedTeam().openInvitations.remove(p);
MissileWars.getBlueTeam().openInvitations.remove(p);
}
2020-05-27 17:52:55 +02:00
}