Add distribute items on team leave #34
@ -36,149 +36,158 @@ import java.util.Objects;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public class MWTeam {
|
public class MWTeam {
|
||||||
private static final ItemStack bow = new ItemStack(Material.BOW);
|
private static final ItemStack bow = new ItemStack(Material.BOW);
|
||||||
|
|
||||||
static {
|
static {
|
||||||
ItemMeta bowMeta = Objects.requireNonNull(bow.getItemMeta());
|
ItemMeta bowMeta = Objects.requireNonNull(bow.getItemMeta());
|
||||||
bowMeta.addEnchant(Enchantment.ARROW_FIRE, 1, true);
|
bowMeta.addEnchant(Enchantment.ARROW_FIRE, 1, true);
|
||||||
bowMeta.addEnchant(Enchantment.ARROW_KNOCKBACK, 1, true);
|
bowMeta.addEnchant(Enchantment.ARROW_KNOCKBACK, 1, true);
|
||||||
bowMeta.addEnchant(Enchantment.KNOCKBACK, 1, true);
|
bowMeta.addEnchant(Enchantment.KNOCKBACK, 1, true);
|
||||||
bowMeta.addEnchant(Enchantment.DAMAGE_ALL, 2, true);
|
bowMeta.addEnchant(Enchantment.DAMAGE_ALL, 2, true);
|
||||||
bowMeta.setUnbreakable(true);
|
bowMeta.setUnbreakable(true);
|
||||||
bow.setItemMeta(bowMeta);
|
bow.setItemMeta(bowMeta);
|
||||||
}
|
}
|
||||||
|
|
||||||
private final ChatColor color;
|
private final ChatColor color;
|
||||||
private final String teamName;
|
private final String teamName;
|
||||||
private final Team sbteam; //scoreboard-Team
|
private final Team sbteam; //scoreboard-Team
|
||||||
private final Location spawn;
|
private final Location spawn;
|
||||||
private final int portalZ;
|
private final int portalZ;
|
||||||
|
|
||||||
private final LinkedList<Player> players = new LinkedList<>();
|
private final LinkedList<Player> players = new LinkedList<>();
|
||||||
private final Set<Player> openInvitations = new HashSet<>();
|
private final Set<Player> openInvitations = new HashSet<>();
|
||||||
|
|
||||||
MWTeam(ChatColor color, Location spawn, String teamName, int portalZ) {
|
MWTeam(ChatColor color, Location spawn, String teamName, int portalZ) {
|
||||||
this.teamName = teamName;
|
this.teamName = teamName;
|
||||||
this.color = color;
|
this.color = color;
|
||||||
this.spawn = spawn;
|
this.spawn = spawn;
|
||||||
this.portalZ = portalZ;
|
this.portalZ = portalZ;
|
||||||
if(FightScoreboard.getScoreboard().getTeam(teamName) == null)
|
if (FightScoreboard.getScoreboard().getTeam(teamName) == null)
|
||||||
sbteam = FightScoreboard.getScoreboard().registerNewTeam(teamName);
|
sbteam = FightScoreboard.getScoreboard().registerNewTeam(teamName);
|
||||||
else
|
else
|
||||||
sbteam = FightScoreboard.getScoreboard().getTeam(teamName);
|
sbteam = FightScoreboard.getScoreboard().getTeam(teamName);
|
||||||
assert sbteam != null;
|
assert sbteam != null;
|
||||||
sbteam.setAllowFriendlyFire(false);
|
sbteam.setAllowFriendlyFire(false);
|
||||||
sbteam.setColor(color);
|
sbteam.setColor(color);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void givePlayerItem(ItemStack item) {
|
public void givePlayerItem(ItemStack item) {
|
||||||
Player p = players.removeFirst();
|
Player p = players.removeFirst();
|
||||||
players.addLast(p);
|
players.addLast(p);
|
||||||
|
|
||||||
Inventory inventory = p.getInventory();
|
Inventory inventory = p.getInventory();
|
||||||
for (int i = 0; i <= 35; i++) { //35 is the last normal inventory slot
|
for (int i = 0; i <= 35; i++) { //35 is the last normal inventory slot
|
||||||
ItemStack itemStack = inventory.getItem(i);
|
ItemStack itemStack = inventory.getItem(i);
|
||||||
if (itemStack != null && itemStack.isSimilar(item) && itemStack.getAmount() != itemStack.getMaxStackSize()) {
|
if (itemStack != null && itemStack.isSimilar(item) && itemStack.getAmount() != itemStack.getMaxStackSize()) {
|
||||||
itemStack.setAmount(itemStack.getAmount() + item.getAmount());
|
itemStack.setAmount(itemStack.getAmount() + item.getAmount());
|
||||||
inventory.setItem(i, itemStack);
|
inventory.setItem(i, itemStack);
|
||||||
p.updateInventory();
|
p.updateInventory();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i <= 35; i++) { //35 is the last normal inventory slot
|
for (int i = 0; i <= 35; i++) { //35 is the last normal inventory slot
|
||||||
ItemStack itemStack = inventory.getItem(i);
|
ItemStack itemStack = inventory.getItem(i);
|
||||||
if (itemStack == null || itemStack.getType().equals(Material.AIR)) {
|
if (itemStack == null || itemStack.getType().equals(Material.AIR)) {
|
||||||
inventory.setItem(i, item);
|
inventory.setItem(i, item);
|
||||||
p.updateInventory();
|
p.updateInventory();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void teamScoreboard(Objective objective) {
|
public void teamScoreboard(Objective objective) {
|
||||||
players.forEach(p -> objective.getScore(getColorCode() + p.getName()).setScore(1));
|
players.forEach(p -> objective.getScore(getColorCode() + p.getName()).setScore(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
public int size() {
|
public int size() {
|
||||||
return players.size();
|
return players.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getPortalZ() {
|
public int getPortalZ() {
|
||||||
return portalZ;
|
return portalZ;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Location getSpawn(){
|
public Location getSpawn() {
|
||||||
return spawn;
|
return spawn;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void join(Player p) {
|
public void join(Player p) {
|
||||||
players.add(p);
|
players.add(p);
|
||||||
p.teleport(spawn);
|
p.teleport(spawn);
|
||||||
p.setGameMode(GameMode.SURVIVAL);
|
p.setGameMode(GameMode.SURVIVAL);
|
||||||
p.getInventory().setItem(0, bow);
|
p.getInventory().setItem(0, bow);
|
||||||
sbteam.addPlayer(p);
|
sbteam.addPlayer(p);
|
||||||
p.setDisplayName(color + p.getName());
|
p.setDisplayName(color + p.getName());
|
||||||
if (MissileWars.getFightState() == FightState.WAITING && !enemy().players.isEmpty())
|
if (MissileWars.getFightState() == FightState.WAITING && !enemy().players.isEmpty())
|
||||||
MissileWars.startRound();
|
MissileWars.startRound();
|
||||||
|
|
||||||
if (!Config.test())
|
if (!Config.test())
|
||||||
Bukkit.getScheduler().runTaskLater(MissileWars.getPlugin(), () -> new TablistNamePacket(SteamwarUser.get(p.getUniqueId()).getId(), color + p.getName()).send(p), 5);
|
Bukkit.getScheduler().runTaskLater(MissileWars.getPlugin(), () -> new TablistNamePacket(SteamwarUser.get(p.getUniqueId()).getId(), color + p.getName()).send(p), 5);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void leave(Player p) {
|
public void leave(Player p) {
|
||||||
if (!players.contains(p)) return;
|
if (!players.contains(p)) return;
|
||||||
|
|
||||||
players.remove(p);
|
ItemStack[] savedInventory = p.getInventory().getContents();
|
||||||
p.setDisplayName("§7" + p.getName());
|
players.remove(p);
|
||||||
sbteam.removePlayer(p);
|
p.getInventory().clear();
|
||||||
if (players.isEmpty() && MissileWars.getFightState() == FightState.FIGHTING)
|
for (ItemStack stack : savedInventory) {
|
||||||
MissileWars.end(WinReasons.NO_ENEMY, enemy());
|
if (stack == null) continue;
|
||||||
|
if (stack.getType() == Material.AIR) continue;
|
||||||
|
if (stack.isSimilar(bow)) continue;
|
||||||
|
givePlayerItem(stack);
|
||||||
|
}
|
||||||
|
|
||||||
if (!Config.test())
|
p.setDisplayName("§7" + p.getName());
|
||||||
new TablistNamePacket(SteamwarUser.get(p.getUniqueId()).getId(), "§7§o" + p.getName()).send(p);
|
sbteam.removePlayer(p);
|
||||||
}
|
if (players.isEmpty() && MissileWars.getFightState() == FightState.FIGHTING)
|
||||||
|
MissileWars.end(WinReasons.NO_ENEMY, enemy());
|
||||||
|
|
||||||
public void invitePlayer(Player p) {
|
if (!Config.test())
|
||||||
if (enemy().openInvitations.contains(p)) return;
|
new TablistNamePacket(SteamwarUser.get(p.getUniqueId()).getId(), "§7§o" + p.getName()).send(p);
|
||||||
openInvitations.add(p);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void acceptInvite(Player p) {
|
public void invitePlayer(Player p) {
|
||||||
removeInvitations(p);
|
if (enemy().openInvitations.contains(p)) return;
|
||||||
join(p);
|
openInvitations.add(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
private MWTeam enemy() {
|
public void acceptInvite(Player p) {
|
||||||
if (this == MissileWars.getRedTeam())
|
removeInvitations(p);
|
||||||
return MissileWars.getBlueTeam();
|
join(p);
|
||||||
|
}
|
||||||
|
|
||||||
return MissileWars.getRedTeam();
|
private MWTeam enemy() {
|
||||||
}
|
if (this == MissileWars.getRedTeam())
|
||||||
|
return MissileWars.getBlueTeam();
|
||||||
|
|
||||||
public String getColorCode(){
|
return MissileWars.getRedTeam();
|
||||||
return "§" + color.getChar();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public boolean hasPlayer(Player p) {
|
public String getColorCode() {
|
||||||
return players.contains(p);
|
return "§" + color.getChar();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasInvite(Player p) {
|
public boolean hasPlayer(Player p) {
|
||||||
return openInvitations.contains(p);
|
return players.contains(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getColoredName() {
|
public boolean hasInvite(Player p) {
|
||||||
return color.toString() + teamName;
|
return openInvitations.contains(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
public LinkedList<Player> getPlayers() {
|
public String getColoredName() {
|
||||||
return players;
|
return color.toString() + teamName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void removeInvitations(Player p) {
|
public LinkedList<Player> getPlayers() {
|
||||||
MissileWars.getRedTeam().openInvitations.remove(p);
|
return players;
|
||||||
MissileWars.getBlueTeam().openInvitations.remove(p);
|
}
|
||||||
}
|
|
||||||
|
public static void removeInvitations(Player p) {
|
||||||
|
MissileWars.getRedTeam().openInvitations.remove(p);
|
||||||
|
MissileWars.getBlueTeam().openInvitations.remove(p);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -47,5 +47,4 @@ public class ConnectionListener extends BasicListener{
|
|||||||
MWTeam.removeInvitations(e.getPlayer());
|
MWTeam.removeInvitations(e.getPlayer());
|
||||||
MissileWars.leave(e.getPlayer());
|
MissileWars.leave(e.getPlayer());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
In neuem Issue referenzieren
Einen Benutzer sperren