From d1461b5e8cd7a6c078ec908829c726166304a54d Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 26 Sep 2020 20:43:50 +0200 Subject: [PATCH] Simplify MWTeam Simplify ItemCountdown --- src/de/steamwar/misslewars/MWTeam.java | 36 ++++++++++++++++--- .../misslewars/countdowns/ItemCountdown.java | 33 ++--------------- 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/src/de/steamwar/misslewars/MWTeam.java b/src/de/steamwar/misslewars/MWTeam.java index e850435..2dc8f54 100644 --- a/src/de/steamwar/misslewars/MWTeam.java +++ b/src/de/steamwar/misslewars/MWTeam.java @@ -25,6 +25,7 @@ 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.Team; @@ -52,10 +53,6 @@ public class MWTeam { private LinkedList players = new LinkedList<>(); - public LinkedList getPlayers(){ - return players; - } - MWTeam(ChatColor color, Location spawn, String teamName, int portalZ) { this.teamName = teamName; this.color = color; @@ -70,6 +67,35 @@ public class MWTeam { 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 int size() { + return players.size(); + } + public int getPortalZ() { return portalZ; } @@ -85,7 +111,7 @@ public class MWTeam { p.getInventory().setItem(0, bow); sbteam.addPlayer(p); p.setDisplayName(color + p.getName()); - if (MissileWars.getFightState() == FightState.WAITING && !enemy().getPlayers().isEmpty()) + if (MissileWars.getFightState() == FightState.WAITING && !enemy().players.isEmpty()) MissileWars.startRound(); } diff --git a/src/de/steamwar/misslewars/countdowns/ItemCountdown.java b/src/de/steamwar/misslewars/countdowns/ItemCountdown.java index 1abb4a8..f4537bd 100644 --- a/src/de/steamwar/misslewars/countdowns/ItemCountdown.java +++ b/src/de/steamwar/misslewars/countdowns/ItemCountdown.java @@ -47,40 +47,13 @@ public class ItemCountdown extends StateDependent { } private void run(){ - int items = Math.max(MissileWars.getBlueTeam().getPlayers().size(), MissileWars.getRedTeam().getPlayers().size()); + int items = Math.max(MissileWars.getBlueTeam().size(), MissileWars.getRedTeam().size()); for (int i = 0; i < items; i++) { ItemStack itemStack = SpecialItem.getRandomItem(); - Player blue = MissileWars.getBlueTeam().getPlayers().removeFirst(); - givePlayerItem(blue, itemStack); - MissileWars.getBlueTeam().getPlayers().addLast(blue); - - Player red = MissileWars.getRedTeam().getPlayers().removeFirst(); - givePlayerItem(red, itemStack); - MissileWars.getBlueTeam().getPlayers().addLast(red); - } - } - - 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; - } + MissileWars.getBlueTeam().givePlayerItem(itemStack); + MissileWars.getRedTeam().givePlayerItem(itemStack); } }