Advertisement
riking

example?

May 13th, 2013
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.16 KB | None | 0 0
  1. package com.gmail.nossr50.util.scoreboards;
  2.  
  3. import java.util.Set;
  4.  
  5. import org.apache.commons.lang.Validate;
  6. import org.bukkit.Bukkit;
  7. import org.bukkit.entity.Player;
  8. import org.bukkit.scheduler.BukkitTask;
  9. import org.bukkit.scoreboard.Objective;
  10. import org.bukkit.scoreboard.Scoreboard;
  11.  
  12. import com.gmail.nossr50.mcMMO;
  13. import com.gmail.nossr50.datatypes.player.McMMOPlayer;
  14. import com.gmail.nossr50.runnables.scoreboards.ScoreboardRevertTask;
  15. import com.gmail.nossr50.util.StringUtils;
  16. import com.gmail.nossr50.util.player.UserManager;
  17.  
  18. /**
  19.  * This class serves as a wrapper around the (Player, Scoreboard) pairing to
  20.  * keep some extra information - such as the reversion to previous
  21.  * scoreboards, the updating behavior, and the scoreboard we want to revert to.
  22.  * <p>
  23.  * Do not store - the information in here is considered transient to each
  24.  * login session.
  25.  */
  26. public class McmmoPlayerScoreboard {
  27.     protected String player;
  28.     protected Scoreboard scoreboard;
  29.     protected ScoreboardUpdater handler;
  30.  
  31.     protected Scoreboard oldScoreboard = null;
  32.     protected BukkitTask revertTask = null;
  33.  
  34.     public enum McmmoScoreboardType {
  35.         EMPTY,
  36.         PLAYER_SKILL,
  37.         PLAYER_STATS,
  38.         PLAYER_RANK,
  39.         GLOBAL_RANK,
  40.         INSPECT_ONLINE,
  41.         INSPECT_OFFLINE,
  42.         ;
  43.     }
  44.  
  45.  
  46.     public McmmoPlayerScoreboard(String player, Scoreboard toUse) {
  47.         this.player = player;
  48.         scoreboard = toUse;
  49.         handler = ScoreboardUpdaterEmpty.get();
  50.     }
  51.  
  52.     public String getPlayerName() {
  53.         return player;
  54.     }
  55.  
  56.     public Player getBukkitPlayer() {
  57.         return Bukkit.getPlayer(player);
  58.     }
  59.  
  60.     public McMMOPlayer getMcmmoPlayer() {
  61.         return UserManager.getPlayer(player);
  62.     }
  63.  
  64.  
  65.     public ScoreboardUpdater getUpdater() {
  66.         return handler;
  67.     }
  68.  
  69.     /**
  70.      * Set the ScoreboardUpdater for this McmmoPlayerScoreboard.
  71.      * If the provided updater is null, {@link ScoreboardUpdaterEmpty#get()}
  72.      * will be substituted.
  73.      * @param updater new ScoreboardUpdater
  74.      */
  75.     public void setUpdater(ScoreboardUpdater updater) {
  76.         if (updater == null) {
  77.             handler = ScoreboardUpdaterEmpty.get();
  78.         } else {
  79.             handler = updater;
  80.         }
  81.     }
  82.  
  83.  
  84.     /**
  85.      * Schedule a task to revert the player's scoreboard from ours to its
  86.      * predecessor.
  87.      * <p>
  88.      * After this call, {@link #getOldScoreboard()} will return
  89.      * the revertTo parameter, except in the case that the provided
  90.      * scoreboard is the one returned with {@link #getScoreboard()}, in which
  91.      * case the value will not be changed.
  92.      *
  93.      * @param revertTo scoreboard to revert to when time is up or dismissed
  94.      * @param delay ticks before reversion, or -1 to wait for dismissal
  95.      */
  96.     public void scheduleRevert(Scoreboard revertTo, long delay) {
  97.         if (revertTask != null) {
  98.             revertTask.cancel(); // no premature reversions
  99.         }
  100.         if (revertTo != scoreboard) {
  101.             // don't want to revert to ourself. This will happen.
  102.             oldScoreboard = revertTo;
  103.         }
  104.         if (delay != -1) { // Don't schedule a task if indefinite
  105.             revertTask = Bukkit.getScheduler().runTaskLater(mcMMO.p, new ScoreboardRevertTask(this), delay * 20); // seconds * 20
  106.         } else {
  107.             revertTask = null;
  108.         }
  109.     }
  110.  
  111.     public void cancelRevertTask() {
  112.         if (revertTask != null) {
  113.             revertTask.cancel();
  114.         }
  115.     }
  116.  
  117.  
  118.     public Scoreboard getScoreboard() {
  119.         return scoreboard;
  120.     }
  121.  
  122.     public Scoreboard getOldScoreboard() {
  123.         if (oldScoreboard == null) {
  124.             return Bukkit.getScoreboardManager().getMainScoreboard();
  125.         }
  126.         return oldScoreboard;
  127.     }
  128.  
  129.     public void clearOldScoreboard() {
  130.         oldScoreboard = null;
  131.     }
  132.  
  133.     /**
  134.      * For use after a player leaves - we should discard our objects just in
  135.      * case they stick around somehow.
  136.      */
  137.     public void clearReferences() {
  138.         cancelRevertTask();
  139.         scoreboard = null;
  140.         oldScoreboard = null;
  141.         handler = null;
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement