Skip to content

Commit

Permalink
Merge pull request #26 from garnesen/WarRevamp
Browse files Browse the repository at this point in the history
Scoreboard 2.0
  • Loading branch information
LlmDl committed Jul 19, 2015
2 parents 5c38eb8 + ac6dd27 commit 85f5b33
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 78 deletions.
11 changes: 11 additions & 0 deletions src/com/palmergames/bukkit/towny/war/eventwar/War.java
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,17 @@ public WarSpoils getWarSpoils() {
*/
public boolean togglePlayerHud(Player p)
{
Town playerTown = null;
try {
playerTown = TownyUniverse.getDataSource().getResident(p.getName()).getTown();
} catch (NotRegisteredException e) {
p.sendMessage("You are not in war!");
return false;
}
if (!warringTowns.contains(playerTown)) {
p.sendMessage("You are not in war!");
return false;
}
if (!playersWithHUD.containsKey(p)){
playersWithHUD.put(p, new WarHUD(plugin, p));
return true;
Expand Down
179 changes: 104 additions & 75 deletions src/com/palmergames/bukkit/towny/war/eventwar/WarHUD.java
Original file line number Diff line number Diff line change
@@ -1,130 +1,159 @@
package com.palmergames.bukkit.towny.war.eventwar;

import java.util.ArrayList;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.ScoreboardManager;
import org.bukkit.scoreboard.Team;

import com.palmergames.bukkit.towny.Towny;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.object.Coord;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownyUniverse;
import com.palmergames.bukkit.towny.object.WorldCoord;
import com.palmergames.util.KeyValue;

public class WarHUD {

Line R15 = new Line (" ", null, 15);
Line HOMETOWN = new Line(ChatColor.GOLD + "", "", 14);
Line SCORE = new Line (ChatColor.WHITE + "Points: ", "", 13);
Line R12 = new Line (" ", null, 12);
Line LOCATIONTITLE = new Line (ChatColor.GOLD + "" + ChatColor.BOLD + "Location", null, 11);
Line NATION = new Line (ChatColor.WHITE + "Nation: " + ChatColor.YELLOW, "", 10);
Line TOWN = new Line (ChatColor.WHITE + "Town: " + ChatColor.YELLOW, "", 9);
Line HOME = new Line ("" + ChatColor.RED, "", 8);
String WAR_HUD_TITLE = ChatColor.GOLD + "" + ChatColor.BOLD + "War";

ArrayList<Line> table = new ArrayList<Line>();
Team space1, town_title, town_score, space2, location_title, nation, town, health, home, space3, top_title, first, second, third;

String space1_player = ChatColor.DARK_AQUA.toString();
String town_title_player = ChatColor.YELLOW + "" + ChatColor.UNDERLINE;
String town_score_player = ChatColor.WHITE + "Score: " + ChatColor.GREEN;
String space2_player = ChatColor.DARK_BLUE.toString();
String location_title_player = ChatColor.YELLOW + "" + ChatColor.UNDERLINE + "Location";
String nation_player = ChatColor.WHITE + "Nation: " + ChatColor.GREEN;
String town_player = ChatColor.WHITE + "Town: " + ChatColor.GREEN;
String health_player = ChatColor.WHITE + "Health: " + ChatColor.GREEN;
String home_player = ChatColor.RED + "";
String space3_player = ChatColor.DARK_GREEN.toString();
String top_title_player = ChatColor.YELLOW + "" + ChatColor.UNDERLINE + "Top Towns";
String first_player = ChatColor.GOLD + "";
String second_player = ChatColor.GRAY + "";
String third_player = ChatColor.DARK_GRAY + "";

Scoreboard board;
Objective obj;
Objective buffer;
Towny plugin;
Player p;

public WarHUD (Towny plugin, Player p) {
this.plugin = plugin;
this.p = p;
ScoreboardManager manager = Bukkit.getScoreboardManager();
board = manager.getNewScoreboard();
obj = board.registerNewObjective("OBJ", "dummy");
buffer = board.registerNewObjective("BUFFER", "dummy");
updateHomeTown(false);
updateScore(false);
updateLocation(false);
update();
init();
updateHomeTown();
updateScore();
updateLocation();
}

@SuppressWarnings("deprecation")
private void init()
{
//init objective
Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();
Objective obj = board.registerNewObjective("WAR_HUD_OBJ", "dummy");
obj.setDisplaySlot(DisplaySlot.SIDEBAR);
obj.setDisplayName(WAR_HUD_TITLE);
//register teams
space1 = board.registerNewTeam("space1");
town_title = board.registerNewTeam("town_title");
town_score = board.registerNewTeam("town_score");
space2 = board.registerNewTeam("space2");
location_title = board.registerNewTeam("location_title");
nation = board.registerNewTeam("nation");
town = board.registerNewTeam("town");
health = board.registerNewTeam("health");
home = board.registerNewTeam("home");
space3 = board.registerNewTeam("space3");
top_title = board.registerNewTeam("top_title");
first = board.registerNewTeam("first");
second = board.registerNewTeam("second");
third = board.registerNewTeam("third");
//register players
space1.addPlayer(Bukkit.getOfflinePlayer(space1_player));
town_title.addPlayer(Bukkit.getOfflinePlayer(town_title_player));
town_score.addPlayer(Bukkit.getOfflinePlayer(town_score_player));
space2.addPlayer(Bukkit.getOfflinePlayer(space2_player));
location_title.addPlayer(Bukkit.getOfflinePlayer(location_title_player));
nation.addPlayer(Bukkit.getOfflinePlayer(nation_player));
town.addPlayer(Bukkit.getOfflinePlayer(town_player));
health.addPlayer(Bukkit.getOfflinePlayer(health_player));
home.addPlayer(Bukkit.getOfflinePlayer(home_player));
space3.addPlayer(Bukkit.getOfflinePlayer(space3_player));
top_title.addPlayer(Bukkit.getOfflinePlayer(top_title_player));
first.addPlayer(Bukkit.getOfflinePlayer(first_player));
second.addPlayer(Bukkit.getOfflinePlayer(second_player));
third.addPlayer(Bukkit.getOfflinePlayer(third_player));
//set scores for positioning
obj.getScore(space1_player).setScore(14);
obj.getScore(town_title_player).setScore(13);
obj.getScore(town_score_player).setScore(12);
obj.getScore(space2_player).setScore(11);
obj.getScore(location_title_player).setScore(10);
obj.getScore(nation_player).setScore(9);
obj.getScore(town_player).setScore(8);
obj.getScore(health_player).setScore(7);
obj.getScore(home_player).setScore(6);
obj.getScore(space3_player).setScore(5);
obj.getScore(top_title_player).setScore(4);
obj.getScore(first_player).setScore(3);
obj.getScore(second_player).setScore(2);
obj.getScore(third_player).setScore(1);
//set the board
p.setScoreboard(board);
}

public void updateHomeTown(boolean scoreboardUpdate)
public void updateHomeTown()
{
String homeTown;
try {
HOMETOWN.value = TownyUniverse.getDataSource().getResident(p.getName()).getTown().getName();
homeTown = TownyUniverse.getDataSource().getResident(p.getName()).getTown().getName();
} catch (NotRegisteredException e) {
HOMETOWN.value = "";
homeTown = "Townless!";
}
if (scoreboardUpdate)
update();
town_title.setSuffix(homeTown);
}

public void updateScore(boolean scoreboardUpdate)
public void updateScore()
{
String score;
try {
Town home = TownyUniverse.getDataSource().getResident(p.getName()).getTown();
SCORE.value = plugin.getTownyUniverse().getWarEvent().getTownScores().get(home) + "";
score = plugin.getTownyUniverse().getWarEvent().getTownScores().get(home) + "";
} catch (NotRegisteredException e) {
SCORE.value = "";
score = "";
}
town_score.setSuffix(score);
}

/**
* Update the location variables of the player.
*
* @param scoreboardUpdate If true, the score board will be rebuilt with the new data
* @throws NotRegisteredException
*/
public void updateLocation(boolean scoreboardUpdate)
public void updateLocation()
{
String nation_loc, town_loc, homeblock_loc;
WorldCoord worldCoord = new WorldCoord(p.getWorld().getName(), Coord.parseCoord(p));
try {
TOWN.value = worldCoord.getTownBlock().getTown().getName();
town_loc = worldCoord.getTownBlock().getTown().getName();
if (worldCoord.getTownBlock().isHomeBlock())
HOME.value = "HOME BLOCK";
homeblock_loc = "HOME BLOCK";
else
HOME.value = "";
} catch (NotRegisteredException e) { TOWN.value = "Wilderness"; HOME.value = "";}
homeblock_loc = "";
} catch (NotRegisteredException e) { town_loc = "Wilderness"; homeblock_loc = "";}
try {
NATION.value = worldCoord.getTownBlock().getTown().getNation().getName();
} catch (NotRegisteredException e) { NATION.value = ""; }
if (scoreboardUpdate)
update();
nation_loc = worldCoord.getTownBlock().getTown().getNation().getName();
} catch (NotRegisteredException e) { nation_loc = ""; }
nation.setSuffix(nation_loc);
town.setSuffix(town_loc);
home.setSuffix(homeblock_loc);
}

public void update()
public void updateTopThree(KeyValue<Town, Integer> f, KeyValue<Town, Integer> s, KeyValue<Town, Integer> t)
{
buffer.unregister();
buffer = board.registerNewObjective("BUFFER", "dummy");
buffer.setDisplayName(ChatColor.GOLD + "WAR");
buffer.getScore(R15.label).setScore(R15.row);
buffer.getScore(HOMETOWN.label).setScore(HOMETOWN.row);
buffer.getScore(SCORE.label + SCORE.value).setScore(SCORE.row);
buffer.getScore(R12.label).setScore(R12.row);
buffer.getScore(LOCATIONTITLE.label).setScore(LOCATIONTITLE.row);
buffer.getScore(NATION.label + NATION.value).setScore(NATION.row);
buffer.getScore(TOWN.label + TOWN.value).setScore(TOWN.row);
buffer.getScore(HOME.label + HOME.value).setScore(HOME.row);
buffer.setDisplaySlot(DisplaySlot.SIDEBAR);
Objective temp = obj;
obj = buffer;
buffer = temp;
}

static class Line {

String label;
String value;
int row;

public Line (String label, String value, int row)
{
this.label = label;
this.value = value;
this.row = row;
}
first.setSuffix((f != null) ? (f.value + "-" + f.key.getName()) : "" );
second.setSuffix((s != null) ? (s.value + "-" + s.key.getName()) : "" );
third.setSuffix((t != null) ? (t.value + "-" + t.key.getName()) : "" );
}

}
27 changes: 24 additions & 3 deletions src/com/palmergames/bukkit/towny/war/eventwar/WarListener.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.palmergames.bukkit.towny.war.eventwar;

import java.util.Map.Entry;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
Expand All @@ -9,7 +11,10 @@
import com.palmergames.bukkit.towny.Towny;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.util.BukkitTools;
import com.palmergames.util.KeyValue;
import com.palmergames.util.KeyValueTable;

public class WarListener implements Listener {

Expand All @@ -32,28 +37,44 @@ public void onPlayerQuit(PlayerQuitEvent event)
}

@EventHandler
public void onPlayerMove(PlayerMoveEvent event) throws NotRegisteredException
public void onPlayerMoveDuringWar(PlayerMoveEvent event) throws NotRegisteredException
{
War warEvent = plugin.getTownyUniverse().getWarEvent();
Player p = event.getPlayer();
if (!warEvent.getPlayersWithHUD().containsKey(p))
return;
if (!event.getFrom().getChunk().equals(event.getTo().getChunk()))
{
warEvent.getPlayersWithHUD().get(p).updateLocation(true);
warEvent.getPlayersWithHUD().get(p).updateLocation();
}
}

@EventHandler
public void onTownScored (TownScoredEvent event)
{
//Update town score
War warEvent = plugin.getTownyUniverse().getWarEvent();
for (Resident r : event.getTown().getResidents())
{
Player player = BukkitTools.getPlayer(r.getName());
if (!warEvent.getPlayersWithHUD().containsKey(player))
continue;
warEvent.getPlayersWithHUD().get(player).updateScore(true);
warEvent.getPlayersWithHUD().get(player).updateScore();
}
//Update top scores for all HUD users
KeyValueTable<Town, Integer> kvTable = new KeyValueTable<Town, Integer>(warEvent.getTownScores());
kvTable.sortByValue();
kvTable.revese();
KeyValue<Town, Integer> first = null;
KeyValue<Town, Integer> second = null;
KeyValue<Town, Integer> third = null;
if (kvTable.getKeyValues().size() >= 1)
first = kvTable.getKeyValues().get(0);
if (kvTable.getKeyValues().size() >= 2)
second = kvTable.getKeyValues().get(1);
if (kvTable.getKeyValues().size() >= 3)
third = kvTable.getKeyValues().get(2);
for (Entry<Player, WarHUD> hud : warEvent.getPlayersWithHUD().entrySet())
hud.getValue().updateTopThree(first, second, third);
}
}

0 comments on commit 85f5b33

Please sign in to comment.