Skip to content

Commit

Permalink
The border warning timer now uses the TimerManager.
Browse files Browse the repository at this point in the history
Related to #30.
  • Loading branch information
AmauryCarrade committed Sep 3, 2014
1 parent c8c996b commit 2423d35
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 67 deletions.
77 changes: 12 additions & 65 deletions src/main/java/me/azenet/UHPlugin/UHBorderManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ public class UHBorderManager {
private BukkitRunnable warningTask = null;

private Boolean warningFinalTimeEnabled = false;
private Boolean warningFinalTimePaused = false;
private Long warningFinalTime = 0L;
private Long warningFinalTimePause = 0L;
private String warningTimerName = null;
private CommandSender warningSender = null;

private Boolean isCircularBorder = null;
Expand All @@ -53,6 +51,8 @@ public UHBorderManager(UHPlugin plugin) {
this.p = plugin;
this.i = p.getI18n();

this.warningTimerName = i.t("borders.warning.nameTimer");

this.currentBorderDiameter = p.getConfig().getInt("map.size");
this.isCircularBorder = p.getConfig().getBoolean("map.circular");
}
Expand Down Expand Up @@ -156,16 +156,6 @@ public int getWarningSize() {
return this.warningSize;
}

/**
* Returns the time stamp of the end of the warning time
* (i.e. the time before the players have to go inside the new border).
*
* @return
*/
public long getWarningFinalTime() {
return this.warningFinalTime;
}

/**
* Returns true if there is currently a warning with a time left displayed.
*
Expand All @@ -175,21 +165,6 @@ public boolean getWarningFinalTimeEnabled() {
return this.warningFinalTimeEnabled;
}

/**
* Returns true if the current warning is paused.
* If there isn't any warning registered, returns false.
*
* @return
*/
public boolean isWarningFinalTimePaused() {
if(getWarningFinalTimeEnabled()) {
return this.warningFinalTimePaused;
}
else {
return false;
}
}

/**
* Returns the sender of the last warning configured.
*
Expand Down Expand Up @@ -218,8 +193,12 @@ public void setWarningSize(int diameter, int timeLeft, CommandSender sender) {
this.warningSize = diameter;

if(timeLeft != 0) {
this.warningFinalTime = System.currentTimeMillis() + 60000L*timeLeft;
this.warningFinalTimeEnabled = true;
UHTimer timer = new UHTimer(this.warningTimerName);
timer.setDuration(timeLeft * 60);

p.getTimerManager().registerTimer(timer);

timer.start();
}

if(sender != null) {
Expand Down Expand Up @@ -255,41 +234,9 @@ public void cancelWarning() {
}
}

stopWarningTime();
}

/**
* (Un)pauses the warning time.
*
* @param pause If true the timer will be paused.
*/
public void setWarningTimePause(boolean pause) {
// The pause is only set once (as example if the user executes /uh freeze all twice).
if(pause && !this.warningFinalTimePaused) {
this.warningFinalTimePaused = true;
this.warningFinalTimePause = System.currentTimeMillis();
}
if(!pause && this.warningFinalTimePaused) {
// We have to add to the time of the end of the warning the elapsed time
// during the pause.
this.warningFinalTime += (System.currentTimeMillis() - this.warningFinalTimePause);
this.warningFinalTimePause = 0L;

this.warningFinalTimePaused = false;
}
}

/**
* Stops the display of the time left in the warning message.
*/
public void stopWarningTime() {
if(this.warningFinalTimePaused) {
setWarningTimePause(false);
}

this.warningFinalTimeEnabled = false;
this.warningFinalTime = 0l;
this.warningSender = null;
UHTimer timer = p.getTimerManager().getTimer(this.warningTimerName);
timer.stop();
p.getTimerManager().unregisterTimer(timer);
}

/**
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/me/azenet/UHPlugin/UHFreezer.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ public void setGlobalFreezeState(Boolean frozen, Boolean showStateInScoreboard)

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

else {
Expand All @@ -124,7 +123,6 @@ public void setGlobalFreezeState(Boolean frozen, Boolean showStateInScoreboard)

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

if(showStateInScoreboard || !frozen) {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/me/azenet/UHPlugin/UHTimerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ public void updateStartedTimersList() {
}
}

/**
* Returns a timer by his name.
*
* @param name The name of the timer.
*
* @return The timer.
*/
public UHTimer getTimer(String name) {
return timers.get(name);
}

/**
* Returns a collection containing the registered timers.
*
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/i18n/en_US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ keys:
messageDistance: "{ci}You have {0} blocks to go before being inside."
messageDistanceTime: "{ci}You have {0} blocks to go before being inside, and {1} minutes to do so."

nameTimer: "Réduction"

timerUp: "{cs}The timer before the new border is up!"

check:
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/i18n/fr_FR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ keys:
messageDistance: "{ci}Il vous reste {0} blocs à parcourir avant d'y être."
messageDistanceTime: "{ci}Il vous reste {0} blocs à parcourir, et {1} minutes pour ce faire."

nameTimer: "Réduction"

timerUp: "{cs}Le temps imparti avant la nouvelle bordure est écoulé !"

check:
Expand Down

0 comments on commit 2423d35

Please sign in to comment.