From 43a64c77b68c46f10dcab8b173f0c15029dc971e Mon Sep 17 00:00:00 2001 From: zOnlyKroks Date: Sun, 4 Apr 2021 14:05:25 +0200 Subject: [PATCH 1/4] Add distribute items on team leave --- src/de/steamwar/misslewars/MWTeam.java | 233 +++++++++--------- .../listener/ConnectionListener.java | 1 - 2 files changed, 121 insertions(+), 113 deletions(-) diff --git a/src/de/steamwar/misslewars/MWTeam.java b/src/de/steamwar/misslewars/MWTeam.java index 25a2656..5af49c9 100644 --- a/src/de/steamwar/misslewars/MWTeam.java +++ b/src/de/steamwar/misslewars/MWTeam.java @@ -36,149 +36,158 @@ import java.util.Objects; import java.util.Set; public class MWTeam { - private static final ItemStack bow = new ItemStack(Material.BOW); + 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); - } + 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 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<>(); + 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) + 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); + 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; - } - } + 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; - } - } - } + 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 void teamScoreboard(Objective objective) { + players.forEach(p -> objective.getScore(getColorCode() + p.getName()).setScore(1)); + } - public int size() { - return players.size(); - } + public int size() { + return players.size(); + } - public int getPortalZ() { - return portalZ; - } + public int getPortalZ() { + return portalZ; + } - public Location getSpawn(){ - return spawn; - } + 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 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(); - if (!Config.test()) - Bukkit.getScheduler().runTaskLater(MissileWars.getPlugin(), () -> new TablistNamePacket(SteamwarUser.get(p.getUniqueId()).getId(), color + p.getName()).send(p), 5); - } + if (!Config.test()) + Bukkit.getScheduler().runTaskLater(MissileWars.getPlugin(), () -> new TablistNamePacket(SteamwarUser.get(p.getUniqueId()).getId(), color + p.getName()).send(p), 5); + } - public void leave(Player p) { - if (!players.contains(p)) return; + public void leave(Player p) { + if (!players.contains(p)) return; - players.remove(p); - p.setDisplayName("§7" + p.getName()); - sbteam.removePlayer(p); - if (players.isEmpty() && MissileWars.getFightState() == FightState.FIGHTING) - MissileWars.end(WinReasons.NO_ENEMY, enemy()); + ItemStack[] savedInventory = p.getInventory().getContents(); + players.remove(p); + p.getInventory().clear(); + for (ItemStack stack : savedInventory) { + if (stack == null) continue; + if (stack.getType() == Material.AIR) continue; + if (stack.isSimilar(bow)) continue; + givePlayerItem(stack); + } - if (!Config.test()) - new TablistNamePacket(SteamwarUser.get(p.getUniqueId()).getId(), "§7§o" + p.getName()).send(p); - } + p.setDisplayName("§7" + p.getName()); + 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); - } + if (!Config.test()) + new TablistNamePacket(SteamwarUser.get(p.getUniqueId()).getId(), "§7§o" + p.getName()).send(p); + } - public void acceptInvite(Player p) { - removeInvitations(p); - join(p); - } + public void invitePlayer(Player p) { + if (enemy().openInvitations.contains(p)) return; + openInvitations.add(p); + } - private MWTeam enemy() { - if (this == MissileWars.getRedTeam()) - return MissileWars.getBlueTeam(); + public void acceptInvite(Player p) { + removeInvitations(p); + join(p); + } - return MissileWars.getRedTeam(); - } + private MWTeam enemy() { + if (this == MissileWars.getRedTeam()) + return MissileWars.getBlueTeam(); - public String getColorCode(){ - return "§" + color.getChar(); - } + return MissileWars.getRedTeam(); + } - public boolean hasPlayer(Player p) { - return players.contains(p); - } + public String getColorCode() { + return "§" + color.getChar(); + } - public boolean hasInvite(Player p) { - return openInvitations.contains(p); - } + public boolean hasPlayer(Player p) { + return players.contains(p); + } - public String getColoredName() { - return color.toString() + teamName; - } + public boolean hasInvite(Player p) { + return openInvitations.contains(p); + } - public LinkedList getPlayers() { - return players; - } + public String getColoredName() { + return color.toString() + teamName; + } - public static void removeInvitations(Player p) { - MissileWars.getRedTeam().openInvitations.remove(p); - MissileWars.getBlueTeam().openInvitations.remove(p); - } + public LinkedList getPlayers() { + return players; + } + + public static void removeInvitations(Player p) { + MissileWars.getRedTeam().openInvitations.remove(p); + MissileWars.getBlueTeam().openInvitations.remove(p); + } } diff --git a/src/de/steamwar/misslewars/listener/ConnectionListener.java b/src/de/steamwar/misslewars/listener/ConnectionListener.java index 7e42772..cac290d 100644 --- a/src/de/steamwar/misslewars/listener/ConnectionListener.java +++ b/src/de/steamwar/misslewars/listener/ConnectionListener.java @@ -47,5 +47,4 @@ public class ConnectionListener extends BasicListener{ MWTeam.removeInvitations(e.getPlayer()); MissileWars.leave(e.getPlayer()); } - } From e2eba79378c3ec747f6e7165383d7fb26d76029d Mon Sep 17 00:00:00 2001 From: zOnlyKroks Date: Sun, 4 Apr 2021 16:40:35 +0200 Subject: [PATCH 2/4] Add distribute items on team leave --- src/de/steamwar/misslewars/MWTeam.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/de/steamwar/misslewars/MWTeam.java b/src/de/steamwar/misslewars/MWTeam.java index 5af49c9..2bd81a0 100644 --- a/src/de/steamwar/misslewars/MWTeam.java +++ b/src/de/steamwar/misslewars/MWTeam.java @@ -54,7 +54,7 @@ public class MWTeam { private final Location spawn; private final int portalZ; - private final LinkedList players = new LinkedList<>(); + private final LinkedList players = new LinkedList<>(); private final Set openInvitations = new HashSet<>(); MWTeam(ChatColor color, Location spawn, String teamName, int portalZ) { @@ -62,7 +62,7 @@ public class MWTeam { this.color = color; this.spawn = spawn; this.portalZ = portalZ; - if (FightScoreboard.getScoreboard().getTeam(teamName) == null) + if(FightScoreboard.getScoreboard().getTeam(teamName) == null) sbteam = FightScoreboard.getScoreboard().registerNewTeam(teamName); else sbteam = FightScoreboard.getScoreboard().getTeam(teamName); @@ -108,7 +108,7 @@ public class MWTeam { return portalZ; } - public Location getSpawn() { + public Location getSpawn(){ return spawn; } @@ -165,7 +165,7 @@ public class MWTeam { return MissileWars.getRedTeam(); } - public String getColorCode() { + public String getColorCode(){ return "§" + color.getChar(); } From 1b9bea8a16b8707a7c081f926d6e41f09d6ac262 Mon Sep 17 00:00:00 2001 From: zOnlyKroks Date: Sun, 4 Apr 2021 16:42:57 +0200 Subject: [PATCH 3/4] Add distribute items on team leave --- src/de/steamwar/misslewars/listener/ConnectionListener.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/de/steamwar/misslewars/listener/ConnectionListener.java b/src/de/steamwar/misslewars/listener/ConnectionListener.java index cac290d..7e42772 100644 --- a/src/de/steamwar/misslewars/listener/ConnectionListener.java +++ b/src/de/steamwar/misslewars/listener/ConnectionListener.java @@ -47,4 +47,5 @@ public class ConnectionListener extends BasicListener{ MWTeam.removeInvitations(e.getPlayer()); MissileWars.leave(e.getPlayer()); } + } From e5fcb7e3fc1c34b655bdcc87ad75b11bf27ccf71 Mon Sep 17 00:00:00 2001 From: zOnlyKroks Date: Sun, 4 Apr 2021 16:56:47 +0200 Subject: [PATCH 4/4] Add distribute items on team leave --- src/de/steamwar/misslewars/MWTeam.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/de/steamwar/misslewars/MWTeam.java b/src/de/steamwar/misslewars/MWTeam.java index 2bd81a0..4f11d7b 100644 --- a/src/de/steamwar/misslewars/MWTeam.java +++ b/src/de/steamwar/misslewars/MWTeam.java @@ -129,16 +129,16 @@ public class MWTeam { public void leave(Player p) { if (!players.contains(p)) return; - ItemStack[] savedInventory = p.getInventory().getContents(); players.remove(p); - p.getInventory().clear(); - for (ItemStack stack : savedInventory) { + for (ItemStack stack : p.getInventory().getContents()) { if (stack == null) continue; if (stack.getType() == Material.AIR) continue; if (stack.isSimilar(bow)) continue; givePlayerItem(stack); } + p.getInventory().clear(); + p.setDisplayName("§7" + p.getName()); sbteam.removePlayer(p); if (players.isEmpty() && MissileWars.getFightState() == FightState.FIGHTING)