Balance ItemCountdown for uneven Teams #12
@ -19,16 +19,18 @@
|
||||
|
||||
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.Inventory;
|
||||
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);
|
||||
@ -49,11 +51,7 @@ public class MWTeam {
|
||||
private final Location spawn;
|
||||
private final int portalZ;
|
||||
|
||||
private Set<Player> players = new HashSet<>();
|
||||
|
||||
public Set<Player> getPlayers(){
|
||||
return players;
|
||||
}
|
||||
private LinkedList<Player> players = new LinkedList<>();
|
||||
|
||||
MWTeam(ChatColor color, Location spawn, String teamName, int portalZ) {
|
||||
this.teamName = teamName;
|
||||
@ -69,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;
|
||||
}
|
||||
@ -77,20 +104,19 @@ 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);
|
||||
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();
|
||||
}
|
||||
|
||||
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);
|
||||
|
@ -19,7 +19,10 @@
|
||||
|
||||
package de.steamwar.misslewars.countdowns;
|
||||
|
||||
import de.steamwar.misslewars.*;
|
||||
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;
|
||||
@ -29,7 +32,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,43 +47,13 @@ public class ItemCountdown extends StateDependent {
|
||||
}
|
||||
|
||||
private void run(){
|
||||
int items = Math.max(MissileWars.getBlueTeam().getPlayers().size(), MissileWars.getRedTeam().getPlayers().size());
|
||||
List<ItemStack> itemList = new ArrayList<>();
|
||||
for (int i = 0; i < items; i++) {
|
||||
itemList.add(SpecialItem.getRandomItem());
|
||||
}
|
||||
int itemCount = Math.max(MissileWars.getBlueTeam().size(), MissileWars.getRedTeam().size());
|
||||
|
||||
|
||||
giveTeamItem(MissileWars.getBlueTeam(), itemList);
|
||||
giveTeamItem(MissileWars.getRedTeam(), itemList);
|
||||
}
|
||||
for (int i = 0; i < itemCount; i++) {
|
||||
ItemStack itemStack = SpecialItem.getRandomItem();
|
||||
|
||||
private void giveTeamItem(MWTeam team, List<ItemStack> itemList) {
|
||||
List<ItemStack> itemStackList = new ArrayList<>(itemList);
|
||||
for (Player p : team.getPlayers()) {
|
||||
ItemStack itemStack = itemStackList.remove(random.nextInt(itemStackList.size()));
|
||||
givePlayerItem(p, itemStack);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
Lixfel
hat
Bitte das Ganze zu einer Funktion im Team machen um nicht das Ganze doppelt zu coden! Dann muss auch nicht getPlayers() public sein. Bitte das Ganze zu einer Funktion im Team machen um nicht das Ganze doppelt zu coden! Dann muss auch nicht getPlayers() public sein.
|
||||
MissileWars.getRedTeam().givePlayerItem(itemStack);
|
||||
}
|
||||
}
|
||||
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren
Das nochmal in eine ArrayList einzufügen, ist nicht wirklich elegant, evtl. kann man die Funktion weiterhin pro Team verwenden?
Ich wollte es dadurch eleganter machen ich überlege nochmal wie man das vllt ändern kann