Skip to content

Commit

Permalink
Improved scoreboard.
Browse files Browse the repository at this point in the history
 - Optimizations;
 - better management of long episodes (displayed timer: hh:mm:ss, if an episode is longer than 1 hour).
  • Loading branch information
AmauryCarrade committed Aug 24, 2014
1 parent bd77914 commit 0e0704e
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 60 deletions.
78 changes: 60 additions & 18 deletions src/main/java/me/azenet/UHPlugin/UHGameManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ public class UHGameManager {
private Boolean slowStartTPFinished = false;

private Boolean gameRunning = false;
private Boolean displayHourInTimer = false;
private Integer episode = 0;
private Integer hoursLeft = 0;
private Integer minutesLeft = 0;
private Integer secondsLeft = 0;

Expand Down Expand Up @@ -111,9 +113,9 @@ public void initEnvironment() {
}

public void initScoreboard() {
// Strange: if the scoreboard manager is instanced in the constructor, when the
// If the scoreboard manager is instanced in the constructor, when the
// scoreboard manager try to get the game manager through UHPlugin.getGameManager(),
// the value returned is "null"...
// the value returned is "null" (because the object is not yet constructed).
// This is why we initializes the scoreboard manager later, in this method.
this.scoreboardManager = new UHScoreboardManager(p);
}
Expand Down Expand Up @@ -166,12 +168,12 @@ public void run() {
* and with the fly.
* Then, the fly is removed and the game starts.
*
* @throws RuntimeException if the game is already started.
* @throws IllegalStateException if the game is already started.
*/
public void start(CommandSender sender, Boolean slow) {
public void start(CommandSender sender, Boolean slow) throws IllegalStateException {

if(isGameRunning()) {
throw new RuntimeException("The game is already started!");
throw new IllegalStateException("The game is already started!");
}

/** Initialization of the players and the teams **/
Expand Down Expand Up @@ -294,10 +296,9 @@ public void start(CommandSender sender, Boolean slow) {

// Used to display the number of teams, players... in the scoreboard instead of 0
// while the players are teleported.
scoreboardManager.updateScoreboard();
scoreboardManager.updateCounters();

// A simple information, because this start is slower (yeah, Captain Obvious here)

p.getServer().broadcastMessage(i.t("start.teleportationInProgress"));


Expand Down Expand Up @@ -378,23 +379,30 @@ public void startEnvironment() {
/**
* Launches the timer by launching the task that updates the scoreboard every second.
*/
private void startTimer() {
public void startTimer() {
if(p.getConfig().getBoolean("episodes.enabled")) {
this.episode = 1;
this.minutesLeft = getEpisodeLength();

this.hoursLeft = (int) Math.floor(getEpisodeLength() / 60);
this.minutesLeft = getEpisodeLength() - (60 * hoursLeft);
this.secondsLeft = 0;

// Lower than 100 because else the counter text is longer than 16 characters.
this.displayHourInTimer = (this.hoursLeft != 0 && this.hoursLeft < 100);

this.episodeStartTime = System.currentTimeMillis();

BukkitRunnable updateTimer = new UpdateTimerTask(p);
updateTimer.runTaskTimer(p, 20L, 20L);

this.scoreboardManager.startTimer();
}
}

/**
* Enables the damages 30 seconds (600 ticks) later.
*/
private void scheduleDamages() {
public void scheduleDamages() {
// 30 seconds later, damages are enabled.
Bukkit.getScheduler().runTaskLater(p, new BukkitRunnable() {
@Override
Expand All @@ -405,12 +413,13 @@ public void run() {
}

/**
* Broadcast the start message and change the state of the game.
* Broadcasts the start message and change the state of the game.
* Also, force the global freeze start to false, to avoid toggle bugs (like inverted state).
*/
private void finalizeStart() {
public void finalizeStart() {
Bukkit.getServer().broadcastMessage(i.t("start.go"));
this.scoreboardManager.updateScoreboard();
this.scoreboardManager.updateCounters();
this.scoreboardManager.updateTimer();

p.getFreezer().setGlobalFreezeState(false);

Expand Down Expand Up @@ -444,8 +453,16 @@ public void updateTimer() {
shiftEpisode();
}
else {
minutesLeft = (int) (this.getEpisodeLength() - diffMinutes) - 1;
secondsLeft = (int) (60 - diffSeconds) - 1;
if(displayHourInTimer) {
int rawMinutesLeft = (int) ((this.getEpisodeLength() - diffMinutes) - 1);
hoursLeft = (int) Math.floor(rawMinutesLeft / 60);
minutesLeft = (int) rawMinutesLeft - (60 * hoursLeft);
secondsLeft = (int) (60 - diffSeconds) - 1;
}
else {
minutesLeft = (int) (this.getEpisodeLength() - diffMinutes) - 1;
secondsLeft = (int) (60 - diffSeconds) - 1;
}
}
}
else {
Expand All @@ -455,9 +472,18 @@ public void updateTimer() {
secondsLeft = 59;
}
if (minutesLeft == -1) {
shiftEpisode();
if(hoursLeft != 0) {
hoursLeft--;
minutesLeft = 59;
secondsLeft = 59;
}
else {
shiftEpisode();
}
}
}

scoreboardManager.updateTimer();
}
}

Expand Down Expand Up @@ -501,7 +527,7 @@ public void updateAliveCounters() {
this.alivePlayersCount = alivePlayers.size();
this.aliveTeamsCount = getAliveTeams().size();

this.scoreboardManager.updateScoreboard();
this.scoreboardManager.updateCounters();
}


Expand All @@ -522,10 +548,14 @@ public void shiftEpisode(String shifter) {
p.getServer().broadcastMessage(message);

this.episode++;
this.minutesLeft = getEpisodeLength();

this.hoursLeft = (int) Math.floor(getEpisodeLength() / 60);
this.minutesLeft = getEpisodeLength() - (60 * hoursLeft);
this.secondsLeft = 0;

this.episodeStartTime = System.currentTimeMillis();

this.scoreboardManager.updateCounters();
}
}

Expand Down Expand Up @@ -869,6 +899,10 @@ public Integer getEpisode() {
return episode;
}

public Integer getHoursLeft() {
return hoursLeft;
}

/**
* Returns the number of minutes left in the current episode.
*
Expand All @@ -885,4 +919,12 @@ public Integer getMinutesLeft() {
public Integer getSecondsLeft() {
return secondsLeft;
}

/**
* Returns true if the hour needs to be displayed in the timer.
* @return
*/
public Boolean displayHourInTimer() {
return this.displayHourInTimer;
}
}
1 change: 0 additions & 1 deletion src/main/java/me/azenet/UHPlugin/UHPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import me.azenet.UHPlugin.listeners.UHGameListener;
import me.azenet.UHPlugin.listeners.UHGameplayListener;

import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/me/azenet/UHPlugin/UHPluginCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ private void doStart(CommandSender sender, Command command, String label, String
else if(args.length == 2 && args[1].equalsIgnoreCase("slow")) { // /uh start slow
try {
p.getGameManager().start(sender, true);
} catch(RuntimeException e) {
} catch(IllegalStateException e) {
sender.sendMessage(i.t("start.already"));
}
}
Expand Down Expand Up @@ -1047,7 +1047,7 @@ private void doFreeze(CommandSender sender, Command command, String label, Strin

if(subcommand.equalsIgnoreCase("on") || subcommand.equalsIgnoreCase("off")) {

boolean on = subcommand.equalsIgnoreCase("on") ? true : false;
boolean on = subcommand.equalsIgnoreCase("on");

if(args.length == 2) { // /uh freeze on: freezes the sender
if(sender instanceof Player) {
Expand Down Expand Up @@ -1084,7 +1084,7 @@ else if(args.length == 3) { // /uh freeze on <player>: freezes <player>.

else if(subcommand.equalsIgnoreCase("all") || subcommand.equalsIgnoreCase("none")) {

boolean on = subcommand.equalsIgnoreCase("all") ? true : false;
boolean on = subcommand.equalsIgnoreCase("all");

p.getFreezer().setGlobalFreezeState(on);

Expand Down
100 changes: 64 additions & 36 deletions src/main/java/me/azenet/UHPlugin/UHScoreboardManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class UHScoreboardManager {
private Integer oldEpisode = -1;
private Integer oldAlivePlayersCount = -1;
private Integer oldAliveTeamsCount = -1;
private Integer oldHours = 0;
private Integer oldMinutes = 0;
private Integer oldSeconds = 0;

Expand Down Expand Up @@ -82,7 +83,8 @@ public UHScoreboardManager(UHPlugin plugin) {
this.objective.getScore("").setScore(3);
}

updateScoreboard();
updateCounters();
updateTimer();
}

// Initialization of the scoreboard (health in players' list)
Expand All @@ -100,15 +102,42 @@ public UHScoreboardManager(UHPlugin plugin) {
}

/**
* Updates the scoreboard (if needed).
* Resets the score of the timer without hours,
* because this score is not reset if the timer is with hours.
*/
public void updateScoreboard() {
public void startTimer() {
sb.resetScores(getTimerText(oldHours, oldMinutes, oldSeconds, true));
}

/**
* Updates the timer of the scoreboard (if needed).
*/
public void updateTimer() {
if(p.getConfig().getBoolean("scoreboard.enabled")) {
Integer hoursLeft = gm.getHoursLeft();
Integer minutesLeft = gm.getMinutesLeft();
Integer secondsLeft = gm.getSecondsLeft();

// The timer score is reset every time.
if(p.getConfig().getBoolean("episodes.enabled") && p.getConfig().getBoolean("scoreboard.timer") && !p.getGameManager().isTimerPaused()) {
sb.resetScores(getTimerText(oldHours, oldMinutes, oldSeconds, false));
objective.getScore(getTimerText(hoursLeft, minutesLeft, secondsLeft, false)).setScore(2);

oldHours = hoursLeft;
oldMinutes = minutesLeft;
oldSeconds = secondsLeft;
}
}
}

/**
* Updates the counters of the scoreboard (if needed).
*/
public void updateCounters() {
if(p.getConfig().getBoolean("scoreboard.enabled")) {
Integer episode = gm.getEpisode();
Integer alivePlayersCount = gm.getAlivePlayersCount();
Integer aliveTeamsCount = gm.getAliveTeamsCount();
Integer minutesLeft = gm.getMinutesLeft();
Integer secondsLeft = gm.getSecondsLeft();

if(!episode.equals(oldEpisode) && p.getConfig().getBoolean("episodes.enabled") && p.getConfig().getBoolean("scoreboard.episode")) {
sb.resetScores(getText("episode", oldEpisode));
Expand All @@ -129,35 +158,6 @@ public void updateScoreboard() {
objective.getScore(getText("teams", aliveTeamsCount)).setScore(4);
oldAliveTeamsCount = aliveTeamsCount;
}

// The timer score is reset every time.
if(p.getConfig().getBoolean("episodes.enabled") && p.getConfig().getBoolean("scoreboard.timer") && !p.getGameManager().isTimerPaused()) {
sb.resetScores(getTimerText(oldMinutes, oldSeconds));
objective.getScore(getTimerText(minutesLeft, secondsLeft)).setScore(2);
oldMinutes = minutesLeft;
oldSeconds = secondsLeft;
}
}
}

/**
* Returns the text displayed in the scoreboard.
*
* @param textType Either "episode", "players" or "teams".
* @param arg Respectively, the episode number, the players count and the teams count.
* @return The text.
* @throws IllegalArgumentException if the textType is not one of the listed types.
*/
private String getText(String textType, Integer arg) {
switch(textType) {
case "episode":
return i.t("scoreboard.episode", arg.toString());
case "players":
return i.t("scoreboard.players", arg.toString());
case "teams":
return i.t("scoreboard.teams", arg.toString());
default:
throw new IllegalArgumentException("Incorrect text type, see javadoc");
}
}

Expand Down Expand Up @@ -186,15 +186,43 @@ public void run() {
}
}

/**
* Returns the text displayed in the scoreboard.
*
* @param textType Either "episode", "players" or "teams".
* @param arg Respectively, the episode number, the players count and the teams count.
* @return The text.
* @throws IllegalArgumentException if the textType is not one of the listed types.
*/
private String getText(String textType, Integer arg) {
switch(textType) {
case "episode":
return i.t("scoreboard.episode", arg.toString());
case "players":
return i.t("scoreboard.players", arg.toString());
case "teams":
return i.t("scoreboard.teams", arg.toString());
default:
throw new IllegalArgumentException("Incorrect text type, see javadoc");
}
}

/**
* Returns the text displayed in the scoreboard, for the timer.
*
* @param hours The hours in the timer
* @param minutes The minute in the timer
* @param seconds The second in the timer
* @param forceNonHoursTimer If true, the non-hours timer text will be returned.
* @return The text of the timer
*/
private String getTimerText(Integer minutes, Integer seconds) {
return i.t("scoreboard.timer", formatter.format(minutes), formatter.format(seconds));
private String getTimerText(Integer hours, Integer minutes, Integer seconds, Boolean forceNonHoursTimer) {
if(gm.displayHourInTimer() && !forceNonHoursTimer) {
return i.t("scoreboard.timerWithHours", formatter.format(hours), formatter.format(minutes), formatter.format(seconds));
}
else {
return i.t("scoreboard.timer", formatter.format(minutes), formatter.format(seconds));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ public void run() {

// Updates the number of alive players/teams
p.getGameManager().updateAliveCounters();
p.getGameManager().getScoreboardManager().updateScoreboard();

// Saves the location of the death
p.getGameManager().addDeathLocation(ev.getEntity(), ev.getEntity().getLocation());
Expand Down
1 change: 0 additions & 1 deletion src/main/java/me/azenet/UHPlugin/task/UpdateTimerTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public UpdateTimerTask(UHPlugin p) {
@Override
public void run() {
p.getGameManager().updateTimer();
p.getGameManager().getScoreboardManager().updateScoreboard();
}

}
1 change: 1 addition & 0 deletions src/main/resources/i18n/en_US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ keys:
players: "{white}{0}{gray} players"
teams: "{white}{0}{gray} teams"
timer: "{white}{0}{gray}:{white}{1}"
timerWithHours: "{0}{gray}:{white}{1}{gray}:{white}{2}"

death:
kickMessage: "jayjay"
Expand Down
Loading

0 comments on commit 0e0704e

Please sign in to comment.