From 821c66c556b8ffbf7d40ae7ef020bfb1311aab1b Mon Sep 17 00:00:00 2001 From: Connor Monahan Date: Sat, 29 Jul 2017 02:14:18 -0500 Subject: [PATCH] Fix #790, add playercount and switching scoreboards * Switching scoreboard jumps between all scoreboard types every minute * Playercount scoreboard shows number of players per team --- war/src/main/java/com/tommytony/war/Team.java | 108 +++----- war/src/main/java/com/tommytony/war/War.java | 248 ++++++++---------- .../main/java/com/tommytony/war/Warzone.java | 81 ++++-- .../tommytony/war/config/ScoreboardType.java | 16 +- .../war/job/ScoreboardSwitchTimer.java | 44 ++++ 5 files changed, 266 insertions(+), 231 deletions(-) create mode 100644 war/src/main/java/com/tommytony/war/job/ScoreboardSwitchTimer.java diff --git a/war/src/main/java/com/tommytony/war/Team.java b/war/src/main/java/com/tommytony/war/Team.java index 6e37d97..546146e 100644 --- a/war/src/main/java/com/tommytony/war/Team.java +++ b/war/src/main/java/com/tommytony/war/Team.java @@ -1,17 +1,10 @@ package com.tommytony.war; -import java.io.File; -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Random; -import java.util.logging.Level; - +import com.tommytony.war.config.*; +import com.tommytony.war.spout.SpoutDisplayer; +import com.tommytony.war.utility.Direction; +import com.tommytony.war.volume.Volume; import org.apache.commons.lang.Validate; -import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; @@ -22,21 +15,15 @@ import org.bukkit.block.BlockState; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.material.Sign; -import org.bukkit.scoreboard.DisplaySlot; -import org.bukkit.scoreboard.Objective; import org.getspout.spoutapi.SpoutManager; import org.getspout.spoutapi.player.SpoutPlayer; import org.kitteh.tag.TagAPI; -import com.tommytony.war.config.InventoryBag; -import com.tommytony.war.config.ScoreboardType; -import com.tommytony.war.config.TeamConfig; -import com.tommytony.war.config.TeamConfigBag; -import com.tommytony.war.config.TeamKind; -import com.tommytony.war.config.TeamSpawnStyle; -import com.tommytony.war.spout.SpoutDisplayer; -import com.tommytony.war.utility.Direction; -import com.tommytony.war.volume.Volume; +import java.io.File; +import java.text.MessageFormat; +import java.util.*; +import java.util.Map.Entry; +import java.util.logging.Level; /** * @@ -44,6 +31,8 @@ import com.tommytony.war.volume.Volume; * */ public class Team { + private final Warzone warzone; + Random teamSpawnRandomizer = new Random(); private List players = new ArrayList(); private List teamChatPlayers = new ArrayList(); private List teamSpawns; @@ -53,9 +42,7 @@ public class Team { private int points = 0; private Map spawnVolumes; private Volume flagVolume; - private final Warzone warzone; private TeamKind kind; - private TeamConfigBag teamConfig; private InventoryBag inventories; @@ -82,7 +69,7 @@ public class Team { } return null; } - + public Warzone getZone() { return this.warzone; } @@ -119,12 +106,13 @@ public class Team { initializeTeamSpawn(teamSpawn); } } + public void initializeTeamSpawn(Location teamSpawn) { // Set the spawn int x = teamSpawn.getBlockX(); int y = teamSpawn.getBlockY(); int z = teamSpawn.getBlockZ(); - + ItemStack light = this.warzone.getWarzoneMaterials().getLightBlock(); TeamSpawnStyle style = this.getTeamConfig().resolveSpawnStyle(); @@ -353,7 +341,7 @@ public class Team { } block.update(true); } - + if (War.war.isSpoutServer()) { War.war.getSpoutDisplayer().updateStats(this.warzone); } @@ -382,7 +370,6 @@ public class Team { return this.teamSpawns; } - Random teamSpawnRandomizer = new Random(); public Location getRandomSpawn() { return this.teamSpawns.get(teamSpawnRandomizer.nextInt(this.teamSpawns.size())); } @@ -395,6 +382,7 @@ public class Team { if (this.warzone.getScoreboard() != null && this.warzone.getScoreboardType() != ScoreboardType.NONE) { player.setScoreboard(this.warzone.getScoreboard()); } + warzone.updateScoreboard(); } public List getPlayers() { @@ -464,14 +452,14 @@ public class Team { } } - public void setName(String name) { - this.name = name; - } - public String getName() { return this.name; } + public void setName(String name) { + this.name = name; + } + public void removePlayer(Player thePlayer) { this.players.remove(thePlayer); synchronized (teamChatPlayers) { @@ -490,21 +478,18 @@ public class Team { this.warzone.restorePlayerState(thePlayer); } this.warzone.getLoadoutSelections().remove(thePlayer); - } - - public void setRemainingLives(int remainingLives) { - this.remainingLives = remainingLives; - if (this.warzone.getScoreboard() != null && this.warzone.getScoreboardType() == ScoreboardType.LIFEPOOL) { - String teamName = kind.getColor() + name + ChatColor.RESET; - Objective obj = this.warzone.getScoreboard().getObjective("Lifepool"); - obj.getScore(teamName).setScore(remainingLives); - } + warzone.updateScoreboard(); } public int getRemainingLives() { return this.remainingLives; } + public void setRemainingLives(int remainingLives) { + this.remainingLives = remainingLives; + warzone.updateScoreboard(); + } + public void addPoint() { boolean atLeastOnePlayerOnTeam = this.players.size() != 0; boolean atLeastOnePlayerOnOtherTeam = false; @@ -518,11 +503,7 @@ public class Team { } else if (!atLeastOnePlayerOnOtherTeam) { this.teamcast("zone.score.empty"); } - if (this.warzone.getScoreboardType() == ScoreboardType.POINTS) { - String teamName = kind.getColor() + name + ChatColor.RESET; - this.warzone.getScoreboard().getObjective(DisplaySlot.SIDEBAR) - .getScore(teamName).setScore(points); - } + this.warzone.updateScoreboard(); } public int getPoints() { @@ -553,22 +534,17 @@ public class Team { public void resetPoints() { this.points = 0; - if (this.warzone.getScoreboardType() == ScoreboardType.POINTS - && this.warzone.getScoreboard() != null) { - String teamName = kind.getColor() + name + ChatColor.RESET; - this.warzone.getScoreboard().getObjective(DisplaySlot.SIDEBAR) - .getScore(teamName).setScore(points); - } - } - - public void setFlagVolume(Volume flagVolume) { - this.flagVolume = flagVolume; + warzone.updateScoreboard(); } public Volume getFlagVolume() { return this.flagVolume; } + public void setFlagVolume(Volume flagVolume) { + this.flagVolume = flagVolume; + } + private void setFlagVolume() { if (this.flagVolume == null) { this.flagVolume = new Volume(this.getName() + "flag", this.warzone.getWorld()); @@ -689,16 +665,6 @@ public class Team { } } - public void setTeamFlag(Location teamFlag) { - this.teamFlag = teamFlag; - - // this resets the block to old state - this.setFlagVolume(); - this.getFlagVolume().saveBlocks(); - - this.initializeTeamFlag(); - } - public boolean isTeamFlagBlock(Block block) { if (this.teamFlag != null) { int flagX = this.teamFlag.getBlockX(); @@ -714,6 +680,16 @@ public class Team { public Location getTeamFlag() { return this.teamFlag; } + + public void setTeamFlag(Location teamFlag) { + this.teamFlag = teamFlag; + + // this resets the block to old state + this.setFlagVolume(); + this.getFlagVolume().saveBlocks(); + + this.initializeTeamFlag(); + } public void deleteTeamFlag() { this.getFlagVolume().resetBlocks(); diff --git a/war/src/main/java/com/tommytony/war/War.java b/war/src/main/java/com/tommytony/war/War.java index 2318597..e2fd943 100644 --- a/war/src/main/java/com/tommytony/war/War.java +++ b/war/src/main/java/com/tommytony/war/War.java @@ -1,17 +1,23 @@ package com.tommytony.war; -import java.io.File; -import java.io.IOException; -import java.text.MessageFormat; -import java.util.*; -import java.util.logging.FileHandler; -import java.util.logging.Formatter; -import java.util.logging.Level; - +import com.tommytony.war.command.WarCommandHandler; +import com.tommytony.war.config.*; +import com.tommytony.war.event.*; import com.tommytony.war.job.CapturePointTimer; +import com.tommytony.war.job.HelmetProtectionTask; +import com.tommytony.war.job.ScoreboardSwitchTimer; +import com.tommytony.war.job.SpoutFadeOutMessageJob; +import com.tommytony.war.mapper.WarYmlMapper; +import com.tommytony.war.mapper.WarzoneYmlMapper; +import com.tommytony.war.spout.SpoutDisplayer; +import com.tommytony.war.structure.*; import com.tommytony.war.ui.UIManager; +import com.tommytony.war.utility.Loadout; +import com.tommytony.war.utility.PlayerState; +import com.tommytony.war.utility.SizeCounter; +import com.tommytony.war.utility.WarLogFormatter; +import com.tommytony.war.volume.Volume; import net.milkbowl.vault.economy.Economy; - import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; @@ -29,41 +35,13 @@ import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; -import com.tommytony.war.command.WarCommandHandler; -import com.tommytony.war.config.FlagReturn; -import com.tommytony.war.config.InventoryBag; -import com.tommytony.war.config.ScoreboardType; -import com.tommytony.war.config.KillstreakReward; -import com.tommytony.war.config.MySQLConfig; -import com.tommytony.war.config.TeamConfig; -import com.tommytony.war.config.TeamConfigBag; -import com.tommytony.war.config.TeamKind; -import com.tommytony.war.config.TeamSpawnStyle; -import com.tommytony.war.config.WarConfig; -import com.tommytony.war.config.WarConfigBag; -import com.tommytony.war.config.WarzoneConfig; -import com.tommytony.war.config.WarzoneConfigBag; -import com.tommytony.war.event.WarBlockListener; -import com.tommytony.war.event.WarEntityListener; -import com.tommytony.war.event.WarPlayerListener; -import com.tommytony.war.event.WarServerListener; -import com.tommytony.war.event.WarTagListener; -import com.tommytony.war.job.HelmetProtectionTask; -import com.tommytony.war.job.SpoutFadeOutMessageJob; -import com.tommytony.war.mapper.WarYmlMapper; -import com.tommytony.war.mapper.WarzoneYmlMapper; -import com.tommytony.war.spout.SpoutDisplayer; -import com.tommytony.war.structure.Bomb; -import com.tommytony.war.structure.Cake; -import com.tommytony.war.structure.HubLobbyMaterials; -import com.tommytony.war.structure.Monument; -import com.tommytony.war.structure.WarHub; -import com.tommytony.war.structure.ZoneLobby; -import com.tommytony.war.utility.Loadout; -import com.tommytony.war.utility.PlayerState; -import com.tommytony.war.utility.SizeCounter; -import com.tommytony.war.utility.WarLogFormatter; -import com.tommytony.war.volume.Volume; +import java.io.File; +import java.io.IOException; +import java.text.MessageFormat; +import java.util.*; +import java.util.logging.FileHandler; +import java.util.logging.Formatter; +import java.util.logging.Level; /** * Main class of War @@ -72,47 +50,38 @@ import com.tommytony.war.volume.Volume; * @package bukkit.tommytony.war */ public class War extends JavaPlugin { + static final boolean HIDE_BLANK_MESSAGES = true; public static War war; - + private static ResourceBundle messages = ResourceBundle.getBundle("messages"); + private final List zoneMakerNames = new ArrayList(); + private final List commandWhitelist = new ArrayList(); + private final List incompleteZones = new ArrayList(); + private final List zoneMakersImpersonatingPlayers = new ArrayList(); + private final HashMap wandBearers = new HashMap(); // playername to zonename + private final List deadlyAdjectives = new ArrayList(); + private final List killerVerbs = new ArrayList(); + private final InventoryBag defaultInventories = new InventoryBag(); + private final WarConfigBag warConfig = new WarConfigBag(); + private final WarzoneConfigBag warzoneDefaultConfig = new WarzoneConfigBag(); + private final TeamConfigBag teamDefaultConfig = new TeamConfigBag(); // general private WarPlayerListener playerListener = new WarPlayerListener(); private WarEntityListener entityListener = new WarEntityListener(); private WarBlockListener blockListener = new WarBlockListener(); private WarServerListener serverListener = new WarServerListener(); - private WarCommandHandler commandHandler = new WarCommandHandler(); private PluginDescriptionFile desc = null; private boolean loaded = false; private boolean isSpoutServer = false; private boolean tagServer = false; - // Zones and hub private List warzones = new ArrayList(); private WarHub warHub; - - private final List zoneMakerNames = new ArrayList(); - private final List commandWhitelist = new ArrayList(); - - private final List incompleteZones = new ArrayList(); - private final List zoneMakersImpersonatingPlayers = new ArrayList(); private HashMap disconnected = new HashMap(); - private final HashMap wandBearers = new HashMap(); // playername to zonename - - private final List deadlyAdjectives = new ArrayList(); - private final List killerVerbs = new ArrayList(); - - private final InventoryBag defaultInventories = new InventoryBag(); private KillstreakReward killstreakReward; private MySQLConfig mysqlConfig; private Economy econ = null; - - private final WarConfigBag warConfig = new WarConfigBag(); - private final WarzoneConfigBag warzoneDefaultConfig = new WarzoneConfigBag(); - private final TeamConfigBag teamDefaultConfig = new TeamConfigBag(); private SpoutDisplayer spoutMessenger = null; - - private static ResourceBundle messages = ResourceBundle.getBundle("messages"); - private HubLobbyMaterials warhubMaterials = new HubLobbyMaterials( new ItemStack(Material.GLASS), new ItemStack(Material.WOOD), new ItemStack(Material.OBSIDIAN), new ItemStack(Material.GLOWSTONE)); @@ -123,6 +92,15 @@ public class War extends JavaPlugin { War.war = this; } + public static void reloadLanguage() { + String[] parts = War.war.getWarConfig().getString(WarConfig.LANGUAGE).replace("-", "_").split("_"); + Locale lang = new Locale(parts[0]); + if (parts.length >= 2) { + lang = new Locale(parts[0], parts[1]); + } + War.messages = ResourceBundle.getBundle("messages", lang); + } + /** * @see JavaPlugin#onEnable() * @see War#loadWar() @@ -145,7 +123,7 @@ public class War extends JavaPlugin { public void loadWar() { this.setLoaded(true); this.desc = this.getDescription(); - + // Spout server detection try { Class.forName("org.getspout.spoutapi.player.SpoutPlayer"); @@ -245,37 +223,37 @@ public class War extends JavaPlugin { this.getDefaultInventories().clearLoadouts(); HashMap defaultLoadout = new HashMap(); - + ItemStack stoneSword = new ItemStack(Material.STONE_SWORD, 1, (byte) 8); stoneSword.setDurability((short) 8); defaultLoadout.put(0, stoneSword); - + ItemStack bow = new ItemStack(Material.BOW, 1, (byte) 8); bow.setDurability((short) 8); defaultLoadout.put(1, bow); - + ItemStack arrows = new ItemStack(Material.ARROW, 7); defaultLoadout.put(2, arrows); - + ItemStack stonePick = new ItemStack(Material.IRON_PICKAXE, 1, (byte) 8); stonePick.setDurability((short) 8); defaultLoadout.put(3, stonePick); - + ItemStack stoneSpade = new ItemStack(Material.STONE_SPADE, 1, (byte) 8); stoneSword.setDurability((short) 8); defaultLoadout.put(4, stoneSpade); - + this.getDefaultInventories().addLoadout("default", defaultLoadout); - + HashMap reward = new HashMap(); reward.put(0, new ItemStack(Material.CAKE, 1)); this.getDefaultInventories().setReward(reward); - + this.getCommandWhitelist().add("who"); this.getZoneMakerNames().add("tommytony"); this.setKillstreakReward(new KillstreakReward()); this.setMysqlConfig(new MySQLConfig()); - + // Add constants this.getDeadlyAdjectives().clear(); for (String adjective : this.getString("pvp.kill.adjectives").split(";")) { @@ -285,17 +263,19 @@ public class War extends JavaPlugin { for (String verb : this.getString("pvp.kill.verbs").split(";")) { this.getKillerVerbs().add(verb); } - + // Load files WarYmlMapper.load(); - + // Start tasks HelmetProtectionTask helmetProtectionTask = new HelmetProtectionTask(); this.getServer().getScheduler().scheduleSyncRepeatingTask(this, helmetProtectionTask, 250, 100); CapturePointTimer cpt = new CapturePointTimer(); cpt.runTaskTimer(this, 100, 20); - + ScoreboardSwitchTimer sst = new ScoreboardSwitchTimer(); + sst.runTaskTimer(this, 500, 20 * 60); + if (this.isSpoutServer) { SpoutFadeOutMessageJob fadeOutMessagesTask = new SpoutFadeOutMessageJob(); this.getServer().getScheduler().scheduleSyncRepeatingTask(this, fadeOutMessagesTask, 100, 100); @@ -331,25 +311,16 @@ public class War extends JavaPlugin { } catch (IOException e) { this.getLogger().log(Level.WARNING, "Failed to create War log file"); } - + // Size check long datSize = SizeCounter.getFileOrDirectorySize(new File(this.getDataFolder() + "/dat/")) / 1024 / 1024; long tempSize = SizeCounter.getFileOrDirectorySize(new File(this.getDataFolder() + "/temp/")) / 1024 / 1024; - + if (datSize + tempSize > 100) { this.log("War data files are taking " + datSize + "MB and its temp files " + tempSize + "MB. Consider permanently deleting old warzone versions and backups in /plugins/War/temp/.", Level.WARNING); } - - this.log("War v" + this.desc.getVersion() + " is on.", Level.INFO); - } - public static void reloadLanguage() { - String[] parts = War.war.getWarConfig().getString(WarConfig.LANGUAGE).replace("-", "_").split("_"); - Locale lang = new Locale(parts[0]); - if (parts.length >= 2) { - lang = new Locale(parts[0], parts[1]); - } - War.messages = ResourceBundle.getBundle("messages", lang); + this.log("War v" + this.desc.getVersion() + " is on.", Level.INFO); } /** @@ -430,7 +401,7 @@ public class War extends JavaPlugin { private void inventoryToLoadout(Player player, HashMap loadout) { this.inventoryToLoadout(player.getInventory(), loadout); } - + public String updateTeamFromNamedParams(Team team, CommandSender commandSender, String[] arguments) { try { Map namedParams = new HashMap(); @@ -444,10 +415,10 @@ public class War extends JavaPlugin { thirdParameter.put(pairSplit[0].toLowerCase(), pairSplit[2]); } } - + StringBuilder returnMessage = new StringBuilder(); returnMessage.append(team.getTeamConfig().updateFromNamedParams(namedParams)); - + if (commandSender instanceof Player) { Player player = (Player) commandSender; if (namedParams.containsKey("loadout")) { @@ -466,7 +437,7 @@ public class War extends JavaPlugin { } } } - + loadout = new HashMap(); team.getInventories().setLoadout(loadoutName, loadout); returnMessage.append(loadoutName + " respawn loadout added."); @@ -483,7 +454,7 @@ public class War extends JavaPlugin { ldt.setPermission(null); returnMessage.append(' ').append(loadoutName).append(" respawn loadout permission deleted."); } - } + } if (namedParams.containsKey("deleteloadout")) { String loadoutName = namedParams.get("deleteloadout"); if (team.getInventories().containsLoadout(loadoutName)) { @@ -520,7 +491,7 @@ public class War extends JavaPlugin { thirdParameter.put(pairSplit[0].toLowerCase(), pairSplit[2]); } } - + StringBuilder returnMessage = new StringBuilder(); if (namedParams.containsKey("author")) { for(String author : namedParams.get("author").split(",")) { @@ -538,7 +509,7 @@ public class War extends JavaPlugin { } } } - + returnMessage.append(warzone.getWarzoneConfig().updateFromNamedParams(namedParams)); returnMessage.append(warzone.getTeamDefaultConfig().updateFromNamedParams(namedParams)); @@ -561,7 +532,7 @@ public class War extends JavaPlugin { } } } - + warzone.getDefaultInventories().setLoadout(loadoutName, loadout); returnMessage.append(loadoutName).append(" respawn loadout added."); } else { @@ -577,7 +548,7 @@ public class War extends JavaPlugin { ldt.setPermission(null); returnMessage.append(' ').append(loadoutName).append(" respawn loadout permission deleted."); } - } + } if (namedParams.containsKey("deleteloadout")) { String loadoutName = namedParams.get("deleteloadout"); if (warzone.getDefaultInventories().containsLoadout(loadoutName)) { @@ -597,7 +568,7 @@ public class War extends JavaPlugin { String whichBlocks = namedParams.get("lobbymaterial"); ItemStack blockInHand = player.getInventory().getItemInMainHand(); boolean updatedLobbyMaterials = false; - + if (!blockInHand.getType().isBlock() && !blockInHand.getType().equals(Material.AIR)) { this.badMsg(player, "Can only use blocks or air as lobby material."); } else { @@ -618,7 +589,7 @@ public class War extends JavaPlugin { returnMessage.append(" lobby light material set to ").append(blockInHand.getType()); updatedLobbyMaterials = true; } - + if (updatedLobbyMaterials && warzone.getLobby() != null) { warzone.getLobby().getVolume().resetBlocks(); warzone.getLobby().initialize(); @@ -629,7 +600,7 @@ public class War extends JavaPlugin { String whichBlocks = namedParams.get("material"); ItemStack blockInHand = player.getInventory().getItemInMainHand(); boolean updatedMaterials = false; - + if (!blockInHand.getType().isBlock()) { this.badMsg(player, "Can only use blocks as material."); } else { @@ -646,7 +617,7 @@ public class War extends JavaPlugin { returnMessage.append(" light material set to ").append(blockInHand.getType()); updatedMaterials = true; } - + if (updatedMaterials) { // reset all structures for (Monument monument : warzone.getMonuments()) { @@ -695,13 +666,13 @@ public class War extends JavaPlugin { thirdParameter.put(pairSplit[0].toLowerCase(), pairSplit[2]); } } - + StringBuilder returnMessage = new StringBuilder(); - + returnMessage.append(this.getWarConfig().updateFromNamedParams(namedParams)); returnMessage.append(this.getWarzoneDefaultConfig().updateFromNamedParams(namedParams)); returnMessage.append(this.getTeamDefaultConfig().updateFromNamedParams(namedParams)); - + if (commandSender instanceof Player) { Player player = (Player) commandSender; if (namedParams.containsKey("loadout")) { @@ -724,7 +695,7 @@ public class War extends JavaPlugin { ldt.setPermission(null); returnMessage.append(' ').append(loadoutName).append(" respawn loadout permission deleted."); } - } + } if (namedParams.containsKey("deleteloadout")) { String loadoutName = namedParams.get("deleteloadout"); if (this.getDefaultInventories().containsLoadout(loadoutName)) { @@ -753,7 +724,7 @@ public class War extends JavaPlugin { String whichBlocks = namedParams.get("warhubmaterial"); ItemStack blockInHand = player.getInventory().getItemInMainHand(); boolean updatedWarhubMaterials = false; - + if (!blockInHand.getType().isBlock() && !blockInHand.getType().equals(Material.AIR)) { this.badMsg(player, "Can only use blocks or air as warhub material."); } else { @@ -774,7 +745,7 @@ public class War extends JavaPlugin { returnMessage.append(" warhub light material set to " + blockInHand.getType()); updatedWarhubMaterials = true; } - + if (updatedWarhubMaterials && War.war.getWarHub() != null) { War.war.getWarHub().getVolume().resetBlocks(); War.war.getWarHub().initialize(); @@ -788,24 +759,24 @@ public class War extends JavaPlugin { return "PARSE-ERROR"; } } - + public String printConfig(Team team) { ChatColor teamColor = ChatColor.AQUA; - + ChatColor normalColor = ChatColor.WHITE; - + String teamConfigStr = ""; InventoryBag invs = team.getInventories(); teamConfigStr += getLoadoutsString(invs); - + for (TeamConfig teamConfig : TeamConfig.values()) { Object value = team.getTeamConfig().getValue(teamConfig); if (value != null) { teamConfigStr += " " + teamConfig.toStringWithValue(value).replace(":", ":" + teamColor) + normalColor; } } - - return " ::" + teamColor + "Team " + team.getName() + teamColor + " config" + normalColor + "::" + + return " ::" + teamColor + "Team " + team.getName() + teamColor + " config" + normalColor + "::" + ifEmptyInheritedForTeam(teamConfigStr); } @@ -813,7 +784,7 @@ public class War extends JavaPlugin { StringBuilder loadoutsString = new StringBuilder(); ChatColor loadoutColor = ChatColor.GREEN; ChatColor normalColor = ChatColor.WHITE; - + if (invs.hasLoadouts()) { StringBuilder loadouts = new StringBuilder(); for (Loadout ldt : invs.getNewLoadouts()) { @@ -825,11 +796,11 @@ public class War extends JavaPlugin { } loadoutsString.append(" loadout:").append(loadoutColor).append(loadouts.toString()).append(normalColor); } - + if (invs.hasReward()) { loadoutsString.append(" reward:").append(loadoutColor).append("default").append(normalColor); } - + return loadoutsString.toString(); } @@ -838,7 +809,7 @@ public class War extends JavaPlugin { ChatColor zoneColor = ChatColor.DARK_AQUA; ChatColor authorColor = ChatColor.GREEN; ChatColor normalColor = ChatColor.WHITE; - + String warzoneConfigStr = ""; for (WarzoneConfig warzoneConfig : WarzoneConfig.values()) { Object value = zone.getWarzoneConfig().getValue(warzoneConfig); @@ -846,7 +817,7 @@ public class War extends JavaPlugin { warzoneConfigStr += " " + warzoneConfig.toStringWithValue(value).replace(":", ":" + zoneColor) + normalColor; } } - + String teamDefaultsStr = ""; teamDefaultsStr += getLoadoutsString( zone.getDefaultInventories()); for (TeamConfig teamConfig : TeamConfig.values()) { @@ -855,8 +826,8 @@ public class War extends JavaPlugin { teamDefaultsStr += " " + teamConfig.toStringWithValue(value).replace(":", ":" + teamColor) + normalColor; } } - - return "::" + zoneColor + "Warzone " + authorColor + zone.getName() + zoneColor + " config" + normalColor + "::" + + return "::" + zoneColor + "Warzone " + authorColor + zone.getName() + zoneColor + " config" + normalColor + "::" + " author:" + authorColor + ifEmptyEveryone(zone.getAuthorsString()) + normalColor + ifEmptyInheritedForWarzone(warzoneConfigStr) + " ::" + teamColor + "Team defaults" + normalColor + "::" @@ -869,10 +840,10 @@ public class War extends JavaPlugin { } return maybeEmpty; } - + private String ifEmptyInheritedForTeam(String maybeEmpty) { if (maybeEmpty.equals("")) { - maybeEmpty = " all values inherited (see " + ChatColor.GREEN + "/warcfg -p" + ChatColor.WHITE + maybeEmpty = " all values inherited (see " + ChatColor.GREEN + "/warcfg -p" + ChatColor.WHITE + " and " + ChatColor.GREEN + "/zonecfg -p" + ChatColor.WHITE + ")"; } return maybeEmpty; @@ -890,26 +861,26 @@ public class War extends JavaPlugin { ChatColor zoneColor = ChatColor.DARK_AQUA; ChatColor globalColor = ChatColor.DARK_GREEN; ChatColor normalColor = ChatColor.WHITE; - + String warConfigStr = ""; for (WarConfig warConfig : WarConfig.values()) { warConfigStr += " " + warConfig.toStringWithValue(this.getWarConfig().getValue(warConfig)).replace(":", ":" + globalColor) + normalColor; } - + String warzoneDefaultsStr = ""; for (WarzoneConfig warzoneConfig : WarzoneConfig.values()) { warzoneDefaultsStr += " " + warzoneConfig.toStringWithValue(this.getWarzoneDefaultConfig().getValue(warzoneConfig)).replace(":", ":" + zoneColor) + normalColor; } - + String teamDefaultsStr = ""; teamDefaultsStr += getLoadoutsString(this.getDefaultInventories()); for (TeamConfig teamConfig : TeamConfig.values()) { teamDefaultsStr += " " + teamConfig.toStringWithValue(this.getTeamDefaultConfig().getValue(teamConfig)).replace(":", ":" + teamColor) + normalColor; } - - return normalColor + "::" + globalColor + "War config" + normalColor + "::" + warConfigStr + + return normalColor + "::" + globalColor + "War config" + normalColor + "::" + warConfigStr + normalColor + " ::" + zoneColor + "Warzone defaults" + normalColor + "::" + warzoneDefaultsStr - + normalColor + " ::" + teamColor + "Team defaults" + normalColor + "::" + teamDefaultsStr; + + normalColor + " ::" + teamColor + "Team defaults" + normalColor + "::" + teamDefaultsStr; } private void setZoneRallyPoint(String warzoneName, Player player) { @@ -959,8 +930,6 @@ public class War extends JavaPlugin { return activeZones; } - static final boolean HIDE_BLANK_MESSAGES = true; - public void msg(CommandSender sender, String str) { if (messages.containsKey(str)) str = this.getString(str); if (HIDE_BLANK_MESSAGES && (str == null || str.isEmpty())) return; @@ -1227,10 +1196,7 @@ public class War extends JavaPlugin { } public boolean inAnyWarzoneLobby(Location location) { - if (ZoneLobby.getLobbyByLocation(location) == null) { - return false; - } - return true; + return ZoneLobby.getLobbyByLocation(location) != null; } public List getZoneMakersImpersonatingPlayers() { @@ -1293,14 +1259,14 @@ public class War extends JavaPlugin { return this.spoutMessenger ; } - public void setWarhubMaterials(HubLobbyMaterials warhubMaterials) { - this.warhubMaterials = warhubMaterials; - } - public HubLobbyMaterials getWarhubMaterials() { return this.warhubMaterials; } + public void setWarhubMaterials(HubLobbyMaterials warhubMaterials) { + this.warhubMaterials = warhubMaterials; + } + public boolean isTagServer() { return tagServer; } diff --git a/war/src/main/java/com/tommytony/war/Warzone.java b/war/src/main/java/com/tommytony/war/Warzone.java index 1043522..38c69bb 100644 --- a/war/src/main/java/com/tommytony/war/Warzone.java +++ b/war/src/main/java/com/tommytony/war/Warzone.java @@ -114,6 +114,7 @@ public class Warzone { private Random killSeed = new Random(); // prevent tryCallDelayedPlayers from being recursively called by Warzone#assign private boolean activeDelayedCall = false; + private ScoreboardType scoreboardType; public Warzone(World world, String name) { this.world = world; @@ -123,6 +124,9 @@ public class Warzone { this.volume = new ZoneVolume(name, this.getWorld(), this); this.lobbyMaterials = War.war.getWarhubMaterials().clone(); this.pvpReady = true; + this.scoreboardType = this.getWarzoneConfig().getScoreboardType(WarzoneConfig.SCOREBOARD); + if (scoreboardType == ScoreboardType.SWITCHING) + scoreboardType = ScoreboardType.LIFEPOOL; } public static Warzone getZoneByName(String name) { @@ -386,18 +390,7 @@ public class Warzone { this.cakeThieves.clear(); if (this.getScoreboardType() != ScoreboardType.NONE) { this.scoreboard = Bukkit.getScoreboardManager().getNewScoreboard(); - scoreboard.registerNewObjective(this.getScoreboardType().getDisplayName(), "dummy"); - Objective obj = scoreboard.getObjective(this.getScoreboardType().getDisplayName()); - Validate.isTrue(obj.isModifiable(), "Cannot modify players' scores on the " + this.name + " scoreboard."); - for (Team team : this.getTeams()) { - String teamName = team.getKind().getColor() + team.getName() + ChatColor.RESET; - if (this.getScoreboardType() == ScoreboardType.POINTS) { - obj.getScore(teamName).setScore(team.getPoints()); - } else if (this.getScoreboardType() == ScoreboardType.LIFEPOOL) { - obj.getScore(teamName).setScore(team.getRemainingLives()); - } - } - obj.setDisplaySlot(DisplaySlot.SIDEBAR); + this.updateScoreboard(); for (Team team : this.getTeams()) { for (Player player : team.getPlayers()) { player.setScoreboard(scoreboard); @@ -1117,16 +1110,60 @@ public class Warzone { if (attackerTeam.getTeamConfig().resolveBoolean(TeamConfig.KILLSTREAK)) { War.war.getKillstreakReward().rewardPlayer(attacker, this.getKillCount(attacker.getName())); } - if (this.getScoreboard() != null && this.getScoreboardType() == ScoreboardType.TOPKILLS) { - Objective obj = this.getScoreboard().getObjective("Top kills"); - obj.getScore(attacker.getName()).setScore(this.getKillCount(attacker.getName())); - } + this.updateScoreboard(); if (defenderTeam.getTeamConfig().resolveBoolean(TeamConfig.INVENTORYDROP)) { dropItems(defender.getLocation(), defender.getInventory().getContents()); dropItems(defender.getLocation(), defender.getInventory().getArmorContents()); } this.handleDeath(defender); } + + public void updateScoreboard() { + if (this.getScoreboardType() == ScoreboardType.NONE) + return; + if (this.getScoreboard() == null) + return; + if (this.scoreboard.getObjective(this.getScoreboardType().getDisplayName()) == null) { + for (String entry : this.scoreboard.getEntries()) { + this.scoreboard.resetScores(entry); + } + this.scoreboard.clearSlot(DisplaySlot.SIDEBAR); + for (Objective obj : this.scoreboard.getObjectives()) { + obj.unregister(); + } + scoreboard.registerNewObjective(this.getScoreboardType().getDisplayName(), "dummy"); + Objective obj = scoreboard.getObjective(this.getScoreboardType().getDisplayName()); + Validate.isTrue(obj.isModifiable(), "Cannot modify players' scores on the " + this.name + " scoreboard."); + obj.setDisplaySlot(DisplaySlot.SIDEBAR); + } + switch (this.getScoreboardType()) { + case POINTS: + for (Team team : this.getTeams()) { + String teamName = team.getKind().getColor() + team.getName() + ChatColor.RESET; + this.getScoreboard().getObjective(DisplaySlot.SIDEBAR).getScore(teamName).setScore(team.getPoints()); + } + break; + case LIFEPOOL: + for (Team team : this.getTeams()) { + String teamName = team.getKind().getColor() + team.getName() + ChatColor.RESET; + this.getScoreboard().getObjective(DisplaySlot.SIDEBAR).getScore(teamName).setScore(team.getRemainingLives()); + } + break; + case TOPKILLS: + for (Player player : this.getPlayers()) { + this.getScoreboard().getObjective(DisplaySlot.SIDEBAR).getScore(player.getName()).setScore(this.getKillCount(player.getName())); + } + break; + case PLAYERCOUNT: + for (Team team : this.getTeams()) { + String teamName = team.getKind().getColor() + team.getName() + ChatColor.RESET; + this.getScoreboard().getObjective(DisplaySlot.SIDEBAR).getScore(teamName).setScore(team.getPlayers().size()); + } + break; + default: + break; + } + } /** * Handle death messages before passing to {@link #handleDeath(Player)} @@ -1831,7 +1868,17 @@ public class Warzone { } public ScoreboardType getScoreboardType() { - return this.getWarzoneConfig().getScoreboardType(WarzoneConfig.SCOREBOARD); + return scoreboardType; + } + + /** + * Sets the TEMPORARY scoreboard type for use in this warzone. + * This type will NOT be persisted in the Warzone config. + * + * @param scoreboardType temporary scoreboard type + */ + public void setScoreboardType(ScoreboardType scoreboardType) { + this.scoreboardType = scoreboardType; } public boolean hasKillCount(String player) { diff --git a/war/src/main/java/com/tommytony/war/config/ScoreboardType.java b/war/src/main/java/com/tommytony/war/config/ScoreboardType.java index 40de160..bc7d32f 100644 --- a/war/src/main/java/com/tommytony/war/config/ScoreboardType.java +++ b/war/src/main/java/com/tommytony/war/config/ScoreboardType.java @@ -5,18 +5,15 @@ public enum ScoreboardType { NONE(null), POINTS("Points"), LIFEPOOL("Lifepool"), - TOPKILLS("Top kills"); + TOPKILLS("Top kills"), + PLAYERCOUNT("Player count"), + SWITCHING("Switching"); private final String displayName; - private ScoreboardType(String displayName) { + ScoreboardType(String displayName) { this.displayName = displayName; } - @Override - public String toString() { - return super.toString().toLowerCase(); - } - public static ScoreboardType getFromString(String string) { for (ScoreboardType boardMode : ScoreboardType.values()) { if (string.toLowerCase().equals(boardMode.toString())) { @@ -27,6 +24,11 @@ public enum ScoreboardType { return ScoreboardType.NONE; } + @Override + public String toString() { + return super.toString().toLowerCase(); + } + public String getDisplayName() { return displayName; } diff --git a/war/src/main/java/com/tommytony/war/job/ScoreboardSwitchTimer.java b/war/src/main/java/com/tommytony/war/job/ScoreboardSwitchTimer.java new file mode 100644 index 0000000..b53e2ee --- /dev/null +++ b/war/src/main/java/com/tommytony/war/job/ScoreboardSwitchTimer.java @@ -0,0 +1,44 @@ +package com.tommytony.war.job; + +import com.tommytony.war.War; +import com.tommytony.war.Warzone; +import com.tommytony.war.config.ScoreboardType; +import com.tommytony.war.config.WarzoneConfig; +import org.bukkit.scheduler.BukkitRunnable; + +/** + * Switches scoreboards periodically + * Github #790 + */ +public class ScoreboardSwitchTimer extends BukkitRunnable { + @Override + public void run() { + if (!War.war.isLoaded()) { + return; + } + for (Warzone zone : War.war.getEnabledWarzones()) { + if (zone.getWarzoneConfig().getScoreboardType(WarzoneConfig.SCOREBOARD) == ScoreboardType.SWITCHING) { + switch (zone.getScoreboardType()) { + case SWITCHING: + zone.setScoreboardType(ScoreboardType.POINTS); + break; + case POINTS: + zone.setScoreboardType(ScoreboardType.LIFEPOOL); + break; + case LIFEPOOL: + zone.setScoreboardType(ScoreboardType.TOPKILLS); + break; + case TOPKILLS: + zone.setScoreboardType(ScoreboardType.PLAYERCOUNT); + break; + case PLAYERCOUNT: + zone.setScoreboardType(ScoreboardType.POINTS); + break; + default: + break; + } + zone.updateScoreboard(); + } + } + } +}