From 299d2aec63eff467970bfc6a77f2ba21dd6f9f4d Mon Sep 17 00:00:00 2001 From: Amaury Carrade Date: Wed, 16 Jul 2014 00:53:03 +0200 Subject: [PATCH] Added a way to synchronize the timer with a clock, to avoid "long" episodes due to lag. --- TODO | 2 +- .../me/azenet/UHPlugin/UHGameManager.java | 34 +++++++++++++++---- .../UHPlugin/UHWorldBorderIntegration.java | 4 +-- src/main/resources/config.yml | 4 ++- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/TODO b/TODO index 9ece148..f060ba2 100644 --- a/TODO +++ b/TODO @@ -1,4 +1,4 @@ -- Synchroniser l'horloge des épisodes avec une vraie horloge (évite le décalage à cause du lag). +- [OK] Synchroniser l'horloge des épisodes avec une vraie horloge (évite le décalage à cause du lag). - [OK] Prendre en compte les cœurs d'absorption en cas de pommes d'or. - [OK] Optimiser la vérification de position des joueurs (ou se reposer uniquement sur WorldBorder, via son API). - Mettre à jour le code pour considérer les UUID (si besoin est). diff --git a/src/main/java/me/azenet/UHPlugin/UHGameManager.java b/src/main/java/me/azenet/UHPlugin/UHGameManager.java index 5b4cca1..39de16c 100644 --- a/src/main/java/me/azenet/UHPlugin/UHGameManager.java +++ b/src/main/java/me/azenet/UHPlugin/UHGameManager.java @@ -44,6 +44,8 @@ public class UHGameManager { private Integer minutesLeft = 0; private Integer secondsLeft = 0; + private Long episodeStartTime = 0L; + public UHGameManager(UHPlugin plugin) { this.p = plugin; @@ -271,6 +273,7 @@ public void run() { private void finalizeStart() { Bukkit.getServer().broadcastMessage(ChatColor.GREEN + "--- GO ---"); this.gameRunning = true; + this.episodeStartTime = System.currentTimeMillis(); } public Integer getTeamsTeleported() { @@ -290,13 +293,28 @@ public void setSlowStartTPFinished(Boolean finished) { public void updateTimer() { - secondsLeft--; - if (secondsLeft == -1) { - minutesLeft--; - secondsLeft = 59; + 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 { + minutesLeft = (int) (this.getEpisodeLength() - diffMinutes) - 1; + secondsLeft = (int) (60 - diffSeconds) - 1; + } } - if (minutesLeft == -1) { - shiftEpisode(); + else { + secondsLeft--; + if (secondsLeft == -1) { + minutesLeft--; + secondsLeft = 59; + } + if (minutesLeft == -1) { + shiftEpisode(); + } } } @@ -323,6 +341,8 @@ public void shiftEpisode(String shifter) { this.episode++; this.minutesLeft = getEpisodeLength(); this.secondsLeft = 0; + + this.episodeStartTime = System.currentTimeMillis(); } /** @@ -394,7 +414,7 @@ public UHScoreboardManager getScoreboardManager() { } public Integer getEpisodeLength() { - return p.getConfig().getInt("episodeLength"); + return p.getConfig().getInt("episodes.length"); } public Integer getAlivePlayersCount() { diff --git a/src/main/java/me/azenet/UHPlugin/UHWorldBorderIntegration.java b/src/main/java/me/azenet/UHPlugin/UHWorldBorderIntegration.java index 214a4ee..e45e5c5 100644 --- a/src/main/java/me/azenet/UHPlugin/UHWorldBorderIntegration.java +++ b/src/main/java/me/azenet/UHPlugin/UHWorldBorderIntegration.java @@ -54,9 +54,7 @@ private void setupBorders() { /** Overworld border **/ - World overworld = getOverworld(); - p.getLogger().info(overworld.getName()); - + World overworld = getOverworld(); BorderData borderOverworld = wb.getWorldBorder(overworld.getName()); if(borderOverworld == null) { // The border needs to be created from scratch diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 8f36565..a87681a 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,4 +1,6 @@ -episodeLength: 20 # minutes +episodes: + length: 20 # minutes + syncTimer: true # if true, the timer will be synchronized with a clock to avoid having long episodes because of lag map: size: 2000