From 4e3ec8c530c47541ac4eaded8ae6a6eb3bacc429 Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 26 Sep 2020 11:39:25 +0200 Subject: [PATCH 1/6] Balance ItemCountdown for uneven Teams --- src/de/steamwar/misslewars/MWTeam.java | 5 ++- .../misslewars/countdowns/ItemCountdown.java | 34 ++++++++++--------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/de/steamwar/misslewars/MWTeam.java b/src/de/steamwar/misslewars/MWTeam.java index 4cd09ee..7237ee6 100644 --- a/src/de/steamwar/misslewars/MWTeam.java +++ b/src/de/steamwar/misslewars/MWTeam.java @@ -19,7 +19,10 @@ package de.steamwar.misslewars; -import org.bukkit.*; +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.ItemStack; diff --git a/src/de/steamwar/misslewars/countdowns/ItemCountdown.java b/src/de/steamwar/misslewars/countdowns/ItemCountdown.java index 35ca32f..909aea8 100644 --- a/src/de/steamwar/misslewars/countdowns/ItemCountdown.java +++ b/src/de/steamwar/misslewars/countdowns/ItemCountdown.java @@ -19,8 +19,10 @@ package de.steamwar.misslewars.countdowns; -import de.steamwar.misslewars.*; -import de.steamwar.misslewars.items.Missile; +import de.steamwar.misslewars.Config; +import de.steamwar.misslewars.FightState; +import de.steamwar.misslewars.MissileWars; +import de.steamwar.misslewars.StateDependent; import de.steamwar.misslewars.items.SpecialItem; import org.bukkit.Bukkit; import org.bukkit.Material; @@ -29,7 +31,10 @@ import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.scheduler.BukkitTask; -import java.util.*; +import java.util.ArrayList; +import java.util.EnumSet; +import java.util.List; +import java.util.Random; public class ItemCountdown extends StateDependent { @@ -41,21 +46,18 @@ public class ItemCountdown extends StateDependent { } private void run(){ - int items = Math.max(MissileWars.getBlueTeam().getPlayers().size(), MissileWars.getRedTeam().getPlayers().size()); - List itemList = new ArrayList<>(); + List blue = new ArrayList<>(MissileWars.getBlueTeam().getPlayers()); + List red = new ArrayList<>(MissileWars.getBlueTeam().getPlayers()); + + int items = Math.max(blue.size(), red.size()); + int randomOffsetBlue = random.nextInt(blue.size()); + int randomOffsetRed = random.nextInt(red.size()); + for (int i = 0; i < items; i++) { - itemList.add(SpecialItem.getRandomItem()); - } + ItemStack itemStack = SpecialItem.getRandomItem(); - giveTeamItem(MissileWars.getBlueTeam(), itemList); - giveTeamItem(MissileWars.getRedTeam(), itemList); - } - - private void giveTeamItem(MWTeam team, List itemList) { - List itemStackList = new ArrayList<>(itemList); - for (Player p : team.getPlayers()) { - ItemStack itemStack = itemStackList.remove(random.nextInt(itemStackList.size())); - givePlayerItem(p, itemStack); + givePlayerItem(blue.get((i + randomOffsetBlue) % blue.size()), itemStack); + givePlayerItem(red.get((i + randomOffsetRed) % red.size()), itemStack); } } -- 2.39.5 From c10465bbc1d28bcedb5a71efee6f5d832692706b Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 26 Sep 2020 12:20:16 +0200 Subject: [PATCH 2/6] Add initial TapeSet --- src/de/steamwar/misslewars/MWTeam.java | 7 +- src/de/steamwar/misslewars/data/TapeSet.java | 89 ++++++++++++++++++++ 2 files changed, 92 insertions(+), 4 deletions(-) create mode 100644 src/de/steamwar/misslewars/data/TapeSet.java diff --git a/src/de/steamwar/misslewars/MWTeam.java b/src/de/steamwar/misslewars/MWTeam.java index 7237ee6..cd447dd 100644 --- a/src/de/steamwar/misslewars/MWTeam.java +++ b/src/de/steamwar/misslewars/MWTeam.java @@ -80,7 +80,7 @@ public class MWTeam { return spawn; } - public void join (Player p) { + public void join(Player p) { players.add(p); p.teleport(spawn); p.setGameMode(GameMode.SURVIVAL); @@ -91,9 +91,8 @@ public class MWTeam { MissileWars.startRound(); } - public void leave (Player p) { - if(!players.contains(p)) - return; + public void leave(Player p) { + if (!players.contains(p)) return; players.remove(p); sbteam.removePlayer(p); diff --git a/src/de/steamwar/misslewars/data/TapeSet.java b/src/de/steamwar/misslewars/data/TapeSet.java new file mode 100644 index 0000000..d2cde7b --- /dev/null +++ b/src/de/steamwar/misslewars/data/TapeSet.java @@ -0,0 +1,89 @@ +package de.steamwar.misslewars.data; + +import java.util.HashMap; +import java.util.Map; + +public class TapeSet { + + private Map> nodeMap = new HashMap<>(); + private Node last = null; + private Node currentIterator = null; + + private class Node { + + private Node next = null; + private Node prev = null; + private T value; + + public Node(T value) { + this.value = value; + } + + private void unlink() { + if (!itself()) { + next.prev = prev; + prev.next = next; + } + next = null; + prev = null; + } + + private void link(Node prev) { + this.prev = prev; + next = prev.next; + next.prev = this; + prev.next = this; + } + + private boolean itself() { + return next == prev && next == this; + } + + } + + public int size() { + return nodeMap.size(); + } + + public boolean isEmpty() { + return nodeMap.isEmpty(); + } + + public boolean contains(T t) { + return nodeMap.containsKey(t); + } + + public void add(T t) { + if (nodeMap.containsKey(t)) return; + Node node = new Node<>(t); + nodeMap.put(t, node); + if (last == null) { + last = node; + currentIterator = node; + } else { + node.link(last); + } + } + + public void remove(T t) { + if (!nodeMap.containsKey(t)) return; + Node node = nodeMap.get(t); + if (node.itself()) { + last = null; + currentIterator = null; + } + if (node == currentIterator) { + currentIterator = node.next; + } + node.unlink(); + nodeMap.remove(t); + } + + public T next() { + if (currentIterator == null) return null; + T value = currentIterator.value; + currentIterator = currentIterator.next; + return value; + } + +} -- 2.39.5 From efb54a9018bac91421e54a87daa5bc5597a3fb22 Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 26 Sep 2020 12:35:11 +0200 Subject: [PATCH 3/6] Add TapeSet --- src/de/steamwar/misslewars/data/TapeSet.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/de/steamwar/misslewars/data/TapeSet.java b/src/de/steamwar/misslewars/data/TapeSet.java index d2cde7b..eb7e7eb 100644 --- a/src/de/steamwar/misslewars/data/TapeSet.java +++ b/src/de/steamwar/misslewars/data/TapeSet.java @@ -1,3 +1,22 @@ +/* + 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.data; import java.util.HashMap; -- 2.39.5 From 4c80c725d2d009b5f7da8936199d7ececeebfb48 Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 26 Sep 2020 12:40:03 +0200 Subject: [PATCH 4/6] Add LinkedList implementation for MWTeam --- src/de/steamwar/misslewars/MWTeam.java | 8 +- .../misslewars/countdowns/ItemCountdown.java | 17 +-- src/de/steamwar/misslewars/data/TapeSet.java | 108 ------------------ 3 files changed, 12 insertions(+), 121 deletions(-) delete mode 100644 src/de/steamwar/misslewars/data/TapeSet.java diff --git a/src/de/steamwar/misslewars/MWTeam.java b/src/de/steamwar/misslewars/MWTeam.java index cd447dd..e850435 100644 --- a/src/de/steamwar/misslewars/MWTeam.java +++ b/src/de/steamwar/misslewars/MWTeam.java @@ -29,9 +29,7 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.scoreboard.Team; -import java.util.HashSet; -import java.util.Objects; -import java.util.Set; +import java.util.*; public class MWTeam { private static final ItemStack bow = new ItemStack(Material.BOW); @@ -52,9 +50,9 @@ public class MWTeam { private final Location spawn; private final int portalZ; - private Set players = new HashSet<>(); + private LinkedList players = new LinkedList<>(); - public Set getPlayers(){ + public LinkedList getPlayers(){ return players; } diff --git a/src/de/steamwar/misslewars/countdowns/ItemCountdown.java b/src/de/steamwar/misslewars/countdowns/ItemCountdown.java index 909aea8..1abb4a8 100644 --- a/src/de/steamwar/misslewars/countdowns/ItemCountdown.java +++ b/src/de/steamwar/misslewars/countdowns/ItemCountdown.java @@ -23,6 +23,7 @@ import de.steamwar.misslewars.Config; import de.steamwar.misslewars.FightState; import de.steamwar.misslewars.MissileWars; import de.steamwar.misslewars.StateDependent; +import de.steamwar.misslewars.items.Missile; import de.steamwar.misslewars.items.SpecialItem; import org.bukkit.Bukkit; import org.bukkit.Material; @@ -46,18 +47,18 @@ public class ItemCountdown extends StateDependent { } private void run(){ - List blue = new ArrayList<>(MissileWars.getBlueTeam().getPlayers()); - List red = new ArrayList<>(MissileWars.getBlueTeam().getPlayers()); - - int items = Math.max(blue.size(), red.size()); - int randomOffsetBlue = random.nextInt(blue.size()); - int randomOffsetRed = random.nextInt(red.size()); + int items = Math.max(MissileWars.getBlueTeam().getPlayers().size(), MissileWars.getRedTeam().getPlayers().size()); for (int i = 0; i < items; i++) { ItemStack itemStack = SpecialItem.getRandomItem(); - givePlayerItem(blue.get((i + randomOffsetBlue) % blue.size()), itemStack); - givePlayerItem(red.get((i + randomOffsetRed) % red.size()), itemStack); + 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); } } diff --git a/src/de/steamwar/misslewars/data/TapeSet.java b/src/de/steamwar/misslewars/data/TapeSet.java deleted file mode 100644 index eb7e7eb..0000000 --- a/src/de/steamwar/misslewars/data/TapeSet.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - 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.data; - -import java.util.HashMap; -import java.util.Map; - -public class TapeSet { - - private Map> nodeMap = new HashMap<>(); - private Node last = null; - private Node currentIterator = null; - - private class Node { - - private Node next = null; - private Node prev = null; - private T value; - - public Node(T value) { - this.value = value; - } - - private void unlink() { - if (!itself()) { - next.prev = prev; - prev.next = next; - } - next = null; - prev = null; - } - - private void link(Node prev) { - this.prev = prev; - next = prev.next; - next.prev = this; - prev.next = this; - } - - private boolean itself() { - return next == prev && next == this; - } - - } - - public int size() { - return nodeMap.size(); - } - - public boolean isEmpty() { - return nodeMap.isEmpty(); - } - - public boolean contains(T t) { - return nodeMap.containsKey(t); - } - - public void add(T t) { - if (nodeMap.containsKey(t)) return; - Node node = new Node<>(t); - nodeMap.put(t, node); - if (last == null) { - last = node; - currentIterator = node; - } else { - node.link(last); - } - } - - public void remove(T t) { - if (!nodeMap.containsKey(t)) return; - Node node = nodeMap.get(t); - if (node.itself()) { - last = null; - currentIterator = null; - } - if (node == currentIterator) { - currentIterator = node.next; - } - node.unlink(); - nodeMap.remove(t); - } - - public T next() { - if (currentIterator == null) return null; - T value = currentIterator.value; - currentIterator = currentIterator.next; - return value; - } - -} -- 2.39.5 From d1461b5e8cd7a6c078ec908829c726166304a54d Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 26 Sep 2020 20:43:50 +0200 Subject: [PATCH 5/6] 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); } } -- 2.39.5 From e3fce8fbf2862537c0c23d299984992cd31f193a Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 26 Sep 2020 20:45:37 +0200 Subject: [PATCH 6/6] Simplify ItemCountdown --- src/de/steamwar/misslewars/MWTeam.java | 2 +- src/de/steamwar/misslewars/countdowns/ItemCountdown.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/de/steamwar/misslewars/MWTeam.java b/src/de/steamwar/misslewars/MWTeam.java index 2dc8f54..a609f3d 100644 --- a/src/de/steamwar/misslewars/MWTeam.java +++ b/src/de/steamwar/misslewars/MWTeam.java @@ -67,7 +67,7 @@ public class MWTeam { sbteam.setColor(color); } - public void givePlayerItem(ItemStack item){ + public void givePlayerItem(ItemStack item) { Player p = players.removeFirst(); players.addLast(p); diff --git a/src/de/steamwar/misslewars/countdowns/ItemCountdown.java b/src/de/steamwar/misslewars/countdowns/ItemCountdown.java index f4537bd..8831365 100644 --- a/src/de/steamwar/misslewars/countdowns/ItemCountdown.java +++ b/src/de/steamwar/misslewars/countdowns/ItemCountdown.java @@ -47,9 +47,9 @@ public class ItemCountdown extends StateDependent { } private void run(){ - int items = Math.max(MissileWars.getBlueTeam().size(), MissileWars.getRedTeam().size()); + int itemCount = Math.max(MissileWars.getBlueTeam().size(), MissileWars.getRedTeam().size()); - for (int i = 0; i < items; i++) { + for (int i = 0; i < itemCount; i++) { ItemStack itemStack = SpecialItem.getRandomItem(); MissileWars.getBlueTeam().givePlayerItem(itemStack); -- 2.39.5