Skip to content

Commit

Permalink
The main timer now uses the TimerManager.
Browse files Browse the repository at this point in the history
Warning, the code is currently broken (until the new way to display timers in the scoreboard is implemented).

Related to #30.
  • Loading branch information
AmauryCarrade committed Sep 2, 2014
1 parent 7366cfd commit 1a02b15
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 135 deletions.
4 changes: 2 additions & 2 deletions src/main/java/me/azenet/UHPlugin/UHFreezer.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void setGlobalFreezeState(Boolean frozen, Boolean showStateInScoreboard)
}

// Freezes the timers.
p.getGameManager().setTimerPause(true);
p.getTimerManager().pauseAll(true);
p.getBorderManager().setWarningTimePause(true);
}

Expand All @@ -123,7 +123,7 @@ public void setGlobalFreezeState(Boolean frozen, Boolean showStateInScoreboard)
}

// Unfreezes the timers.
p.getGameManager().setTimerPause(false);
p.getTimerManager().pauseAll(false);
p.getBorderManager().setWarningTimePause(false);
}

Expand Down
142 changes: 9 additions & 133 deletions src/main/java/me/azenet/UHPlugin/UHGameManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -418,20 +418,16 @@ public void startEnvironment() {
*/
public void startTimer() {
if(p.getConfig().getBoolean("episodes.enabled")) {
this.episode = 1;
// An empty string is used for the name of the main timer, because
// such a name can't be used by players.
UHTimer mainTimer = new UHTimer("");
mainTimer.setDuration(this.getEpisodeLength() * 60);

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

// 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);
mainTimer.start();

// will be removed
this.scoreboardManager.startTimer();
}
}
Expand Down Expand Up @@ -492,89 +488,6 @@ public void setSlowStartTPFinished(Boolean finished) {
this.slowStartTPFinished = finished;
}




/**
* Updates the timer.
*/
public void updateTimer() {
if(p.getConfig().getBoolean("episodes.enabled") && !this.timerPaused) {
if(p.getConfig().getBoolean("episodes.syncTimer")) {
long timeSinceStart = System.currentTimeMillis() - this.episodeStartTime;
long diffSeconds = timeSinceStart / 1000 % 60;
long diffMinutes = timeSinceStart / (60 * 1000) % 60;

if(diffMinutes >= this.getEpisodeLength()) {
shiftEpisode();
}
else {
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 {
secondsLeft--;
if (secondsLeft == -1) {
minutesLeft--;
secondsLeft = 59;
}
if (minutesLeft == -1) {
if(hoursLeft != 0) {
hoursLeft--;
minutesLeft = 59;
secondsLeft = 59;
}
else {
shiftEpisode();
}
}
}

scoreboardManager.updateTimer();
}
}

/**
* Pauses (or restarts) the timer.
*/
public void setTimerPause(boolean pause) {
// If the game is not started, the timer is not running.
if(p.getGameManager().isGameRunning()) {
// The pause is only set once (as example if the user executes /uh freeze all twice).
if(pause && !this.timerPaused) {
this.timerPaused = true;
this.timerPauseTime = System.currentTimeMillis();
}
if(!pause && this.timerPaused) {
// We have to add to the time of the start of the episode the elapsed time
// during the pause.
this.episodeStartTime += (System.currentTimeMillis() - this.timerPauseTime);
this.timerPauseTime = 0L;

this.timerPaused = false;
}
}
}

/**
* Returns true if the timer is paused.
*
* @return true if the timer is paused.
*/
public boolean isTimerPaused() {
return timerPaused;
}


/**
* Updates the cached values of the numbers of alive players
* and teams.
Expand Down Expand Up @@ -605,11 +518,8 @@ public void shiftEpisode(String shifter) {

this.episode++;

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

this.episodeStartTime = System.currentTimeMillis();
// Restarts the timer
p.getTimerManager().getMainTimer().start();

this.scoreboardManager.updateCounters();
}
Expand Down Expand Up @@ -1009,38 +919,4 @@ public Integer getAliveTeamsCount() {
public Integer getEpisode() {
return episode;
}

/**
* Returns the number of hours left in the current episode.
*
* @return
*/
public Integer getHoursLeft() {
return hoursLeft;
}

/**
* Returns the number of minutes left in the current episode.
*
* @return
*/
public Integer getMinutesLeft() {
return minutesLeft;
}

/**
* Returns the number of seconds left in the current <em>minute</em>.
* @return
*/
public Integer getSecondsLeft() {
return secondsLeft;
}

/**
* Returns true if the hour needs to be displayed in the timer.
* @return
*/
public Boolean displayHourInTimer() {
return this.displayHourInTimer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,12 @@ else if(p.getTeamChatManager().isOtherTeamChatEnabled(ev.getPlayer())) {
@EventHandler
public void onTimerEnds(TimerEndsEvent ev) {
p.getTimerManager().updateStartedTimersList();

if(ev.getTimer().equals(p.getTimerManager().getMainTimer())) {
// If this timer is the main one, we shifts an episode.
// The shift restarts the timer.
p.getGameManager().shiftEpisode();
}
}

@EventHandler
Expand Down

0 comments on commit 1a02b15

Please sign in to comment.