SteamWar/MissileWars
Archiviert
13
0

Merge pull request 'Balance ItemCountdown for uneven Teams' (#12) from BalanceItems into master

Reviewed-by: Lixfel <lixfel@steamwar.de>
Dieser Commit ist enthalten in:
Lixfel 2020-09-26 20:46:29 +02:00
Commit 12efa8c808
2 geänderte Dateien mit 53 neuen und 51 gelöschten Zeilen

Datei anzeigen

@ -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);

Datei anzeigen

@ -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);
MissileWars.getRedTeam().givePlayerItem(itemStack);
}
}