ItsAlexousd

DEV' PLUGIN SPIGOT #25 - ScoreboardManager

Feb 4th, 2019
2,395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.84 KB | None | 0 0
  1. package fr.itsalexousd.devplugin.scoreboard;
  2.  
  3. import fr.itsalexousd.devplugin.Main;
  4. import org.bukkit.ChatColor;
  5. import org.bukkit.entity.Player;
  6.  
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. import java.util.UUID;
  10. import java.util.concurrent.ScheduledFuture;
  11. import java.util.concurrent.TimeUnit;
  12.  
  13. /*
  14.  * This file is part of SamaGamesAPI.
  15.  *
  16.  * SamaGamesAPI is free software: you can redistribute it and/or modify
  17.  * it under the terms of the GNU General Public License as published by
  18.  * the Free Software Foundation, either version 3 of the License, or
  19.  * (at your option) any later version.
  20.  *
  21.  * SamaGamesAPI is distributed in the hope that it will be useful,
  22.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24.  * GNU General Public License for more details.
  25.  *
  26.  * You should have received a copy of the GNU General Public License
  27.  * along with SamaGamesAPI.  If not, see <http://www.gnu.org/licenses/>.
  28.  */
  29. public class ScoreboardManager {
  30.     private final Map<UUID, PersonalScoreboard> scoreboards;
  31.     private final ScheduledFuture glowingTask;
  32.     private final ScheduledFuture reloadingTask;
  33.     private int ipCharIndex;
  34.     private int cooldown;
  35.  
  36.     public ScoreboardManager() {
  37.         scoreboards = new HashMap<>();
  38.         ipCharIndex = 0;
  39.         cooldown = 0;
  40.  
  41.         glowingTask = Main.getInstance().getScheduledExecutorService().scheduleAtFixedRate(() ->
  42.         {
  43.             String ip = colorIpAt();
  44.             for (PersonalScoreboard scoreboard : scoreboards.values())
  45.                 Main.getInstance().getExecutorMonoThread().execute(() -> scoreboard.setLines(ip));
  46.         }, 80, 80, TimeUnit.MILLISECONDS);
  47.  
  48.         reloadingTask = Main.getInstance().getScheduledExecutorService().scheduleAtFixedRate(() ->
  49.         {
  50.             for (PersonalScoreboard scoreboard : scoreboards.values())
  51.                 Main.getInstance().getExecutorMonoThread().execute(scoreboard::reloadData);
  52.         }, 1, 1, TimeUnit.SECONDS);
  53.     }
  54.  
  55.     public void onDisable() {
  56.         scoreboards.values().forEach(PersonalScoreboard::onLogout);
  57.     }
  58.  
  59.     public void onLogin(Player player) {
  60.         if (scoreboards.containsKey(player.getUniqueId())) {
  61.             return;
  62.         }
  63.         scoreboards.put(player.getUniqueId(), new PersonalScoreboard(player));
  64.     }
  65.  
  66.     public void onLogout(Player player) {
  67.         if (scoreboards.containsKey(player.getUniqueId())) {
  68.             scoreboards.get(player.getUniqueId()).onLogout();
  69.             scoreboards.remove(player.getUniqueId());
  70.         }
  71.     }
  72.  
  73.     public void update(Player player) {
  74.         if (scoreboards.containsKey(player.getUniqueId())) {
  75.             scoreboards.get(player.getUniqueId()).reloadData();
  76.         }
  77.     }
  78.  
  79.     private String colorIpAt() {
  80.         String ip = "play.serveur.fr";
  81.  
  82.         if (cooldown > 0) {
  83.             cooldown--;
  84.             return ChatColor.YELLOW + ip;
  85.         }
  86.  
  87.         StringBuilder formattedIp = new StringBuilder();
  88.  
  89.         if (ipCharIndex > 0) {
  90.             formattedIp.append(ip.substring(0, ipCharIndex - 1));
  91.             formattedIp.append(ChatColor.GOLD).append(ip.substring(ipCharIndex - 1, ipCharIndex));
  92.         } else {
  93.             formattedIp.append(ip.substring(0, ipCharIndex));
  94.         }
  95.  
  96.         formattedIp.append(ChatColor.RED).append(ip.charAt(ipCharIndex));
  97.  
  98.         if (ipCharIndex + 1 < ip.length()) {
  99.             formattedIp.append(ChatColor.GOLD).append(ip.charAt(ipCharIndex + 1));
  100.  
  101.             if (ipCharIndex + 2 < ip.length())
  102.                 formattedIp.append(ChatColor.YELLOW).append(ip.substring(ipCharIndex + 2));
  103.  
  104.             ipCharIndex++;
  105.         } else {
  106.             ipCharIndex = 0;
  107.             cooldown = 50;
  108.         }
  109.  
  110.         return ChatColor.YELLOW + formattedIp.toString();
  111.     }
  112.  
  113. }
Add Comment
Please, Sign In to add comment