From 00819d8eeda024a6636e1fd4dd5fc8ed4a10bdf0 Mon Sep 17 00:00:00 2001 From: Zrips Date: Thu, 22 Jun 2017 15:17:34 +0300 Subject: [PATCH] Properly return to previous scoreboard --- .../gamingmesh/jobs/commands/list/gtop.java | 12 ++--- .../gamingmesh/jobs/commands/list/top.java | 12 ++--- .../jobs/config/ScboardManager.java | 20 ++++++-- .../jobs/container/ScoreboardInfo.java | 48 +++++++++++++++++++ 4 files changed, 73 insertions(+), 19 deletions(-) create mode 100644 src/main/java/com/gamingmesh/jobs/container/ScoreboardInfo.java diff --git a/src/main/java/com/gamingmesh/jobs/commands/list/gtop.java b/src/main/java/com/gamingmesh/jobs/commands/list/gtop.java index 6f6c06e5..d0e925ab 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/gtop.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/gtop.java @@ -65,11 +65,11 @@ public class gtop implements Cmd { } } else { - player.getScoreboard().clearSlot(DisplaySlot.SIDEBAR); - - ScoreboardManager manager = Bukkit.getScoreboardManager(); - Scoreboard board = manager.getNewScoreboard(); - Objective objective = board.registerNewObjective("JobsTopPlayers", "dummy"); + Jobs.getScboard().addNew(player); + Scoreboard board = player.getScoreboard(); + Objective objective = board.getObjective("JobsTopPlayers"); + if (objective == null) + objective = board.registerNewObjective("JobsTopPlayers", "dummy"); objective.setDisplaySlot(DisplaySlot.SIDEBAR); objective.setDisplayName(Jobs.getLanguage().getMessage("scoreboard.gtopline")); int i = page * 15 - 15; @@ -83,8 +83,6 @@ public class gtop implements Cmd { } player.setScoreboard(board); - Jobs.getScboard().addNew(player); - int prev = page < 2 ? 1 : page - 1; int next = page + 1; diff --git a/src/main/java/com/gamingmesh/jobs/commands/list/top.java b/src/main/java/com/gamingmesh/jobs/commands/list/top.java index bd6c64cc..e7592772 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/list/top.java +++ b/src/main/java/com/gamingmesh/jobs/commands/list/top.java @@ -78,12 +78,11 @@ public class top implements Cmd { One.getExp())); } } else { - - player.getScoreboard().clearSlot(DisplaySlot.SIDEBAR); - - ScoreboardManager manager = Bukkit.getScoreboardManager(); - Scoreboard board = manager.getNewScoreboard(); - Objective objective = board.registerNewObjective("JobsTopPlayers", "dummy"); + Jobs.getScboard().addNew(player); + Scoreboard board = player.getScoreboard(); + Objective objective = board.getObjective("JobsTopPlayers"); + if (objective == null) + objective = board.registerNewObjective("JobsTopPlayers", "dummy"); objective.setDisplaySlot(DisplaySlot.SIDEBAR); objective.setDisplayName(Jobs.getLanguage().getMessage("scoreboard.topline", "%jobname%", jobName)); int i = start; @@ -99,7 +98,6 @@ public class top implements Cmd { } player.setScoreboard(board); - Jobs.getScboard().addNew(player); int from = start; if (start >= 15) diff --git a/src/main/java/com/gamingmesh/jobs/config/ScboardManager.java b/src/main/java/com/gamingmesh/jobs/config/ScboardManager.java index b93cfd3a..64a6fa5e 100644 --- a/src/main/java/com/gamingmesh/jobs/config/ScboardManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/ScboardManager.java @@ -2,17 +2,21 @@ package com.gamingmesh.jobs.config; import java.util.Iterator; import java.util.Map.Entry; +import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.scoreboard.DisplaySlot; +import org.bukkit.scoreboard.Objective; +import org.bukkit.scoreboard.Scoreboard; import com.gamingmesh.jobs.Jobs; +import com.gamingmesh.jobs.container.ScoreboardInfo; public class ScboardManager { - private ConcurrentHashMap timerMap = new ConcurrentHashMap(); + private ConcurrentHashMap timerMap = new ConcurrentHashMap(); private Jobs plugin; public ScboardManager(Jobs plugin) { @@ -20,14 +24,19 @@ public class ScboardManager { } private void RunScheduler() { - Iterator> MeinMapIter = timerMap.entrySet().iterator(); + Iterator> MeinMapIter = timerMap.entrySet().iterator(); while (MeinMapIter.hasNext()) { - Entry Map = MeinMapIter.next(); + Entry Map = MeinMapIter.next(); - if (System.currentTimeMillis() > Map.getValue() + (Jobs.getGCManager().ToplistInScoreboardInterval * 1000)) { + if (System.currentTimeMillis() > Map.getValue().getTime() + (Jobs.getGCManager().ToplistInScoreboardInterval * 1000)) { Player player = Bukkit.getPlayer(Map.getKey()); if (player != null) { player.getScoreboard().clearSlot(DisplaySlot.SIDEBAR); + if (Map.getValue().getObj() != null) { + Objective obj = player.getScoreboard().getObjective(Map.getValue().getObj().getName()); + if (obj != null) + obj.setDisplaySlot(DisplaySlot.SIDEBAR); + } } timerMap.remove(Map.getKey()); } @@ -45,7 +54,8 @@ public class ScboardManager { } public void addNew(Player player) { - timerMap.put(player.getName(), System.currentTimeMillis()); + Scoreboard scoreBoard = player.getScoreboard(); + timerMap.put(player.getUniqueId(), new ScoreboardInfo(scoreBoard, DisplaySlot.SIDEBAR)); RunScheduler(); } diff --git a/src/main/java/com/gamingmesh/jobs/container/ScoreboardInfo.java b/src/main/java/com/gamingmesh/jobs/container/ScoreboardInfo.java new file mode 100644 index 00000000..de0ae090 --- /dev/null +++ b/src/main/java/com/gamingmesh/jobs/container/ScoreboardInfo.java @@ -0,0 +1,48 @@ +package com.gamingmesh.jobs.container; + +import org.bukkit.scoreboard.DisplaySlot; +import org.bukkit.scoreboard.Objective; +import org.bukkit.scoreboard.Scoreboard; + +public class ScoreboardInfo { + + private Scoreboard scoreBoard; + private Objective obj; + private Long time; + + public ScoreboardInfo(Scoreboard scoreBoard, DisplaySlot slot) { + this.scoreBoard = scoreBoard; + + for (Objective one : this.scoreBoard.getObjectives()) { + if (one.getDisplaySlot() == slot) + obj = one; + } + + time = System.currentTimeMillis(); + } + + public Scoreboard getScoreBoard() { + return scoreBoard; + } + + public void setScoreBoard(Scoreboard scoreBoard) { + this.scoreBoard = scoreBoard; + } + + public Long getTime() { + return time; + } + + public void setTime(Long time) { + this.time = time; + } + + public Objective getObj() { + return obj; + } + + public void setObj(Objective obj) { + this.obj = obj; + } + +}