Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. package server.model;
  2.  
  3. import com.google.common.base.Preconditions;
  4.  
  5. import server.model.players.Player;
  6. import server.model.players.World;
  7.  
  8. /**
  9.  * Handles sending a notice to players
  10.  *
  11.  * @author Arithium
  12.  *
  13.  */
  14. public class Notices {
  15.    
  16.     /**
  17.      * The announcement type of the notice to send, whether its global or a single player
  18.      * @author Arithium
  19.      *
  20.      */
  21.     public enum NoticeAnnouncementType {
  22.     PLAYER,
  23.     GLOBAL,
  24.     }
  25.    
  26.     /**
  27.      * The type of notice to send to the players
  28.      *
  29.      * @author Arithium
  30.      *
  31.      */
  32.     public enum NoticeType {
  33.     DROP_MESSAGE("[Drop]", "255", NoticeAnnouncementType.GLOBAL),
  34.     GAME_MESSAGE(null, null, NoticeAnnouncementType.PLAYER),
  35.    
  36.     ;
  37.    
  38.     private final String prefix, color;
  39.     private final NoticeAnnouncementType type;
  40.     NoticeType(String prefix, String color, NoticeAnnouncementType type) {
  41.         this.prefix = prefix;
  42.         this.color = color;
  43.         this.type = type;
  44.     }
  45.    
  46.     public String getPrefix() {
  47.         return prefix;
  48.     }
  49.    
  50.     public String getColor() {
  51.         return color;
  52.     }
  53.    
  54.     public NoticeAnnouncementType getType() {
  55.         return type;
  56.     }
  57.    
  58.     }
  59.  
  60.     /**
  61.      * Sends a notice to all players online
  62.      *
  63.      * @param type
  64.      *            The {@link NoticeType} of the notice to send
  65.      * @param message
  66.      *            The message of the notice to send
  67.      */
  68.     public static void send(NoticeType type, String message) {
  69.     Preconditions.checkArgument(type.getType() != NoticeAnnouncementType.PLAYER);
  70.     send(type, message, null);
  71.     }
  72.  
  73.     /**
  74.      * Sends a notice to either a single player or to all players
  75.      *
  76.      * @param type
  77.      *            The {@link NoticeType} of the notice to send
  78.      * @param message
  79.      *            The message of the notice to send
  80.      * @param player
  81.      *            The individual player to send the notice to
  82.      */
  83.     public static void send(NoticeType type, String message, Player player) {
  84.     StringBuilder bldr = new StringBuilder();
  85.     if (type.getColor() != null) {
  86.         bldr.append("<col=" + type.getColor() + ">");
  87.     }
  88.     if (type.getPrefix() != null) {
  89.         bldr.append(type.getPrefix() + " ");
  90.     }
  91.     bldr.append(message);
  92.     final String toSend = bldr.toString();
  93.    
  94.     if (type.getType().equals(NoticeAnnouncementType.GLOBAL)) {
  95.         World.getPlayerStream().forEach(i -> i.sendMessage(toSend));
  96.     } else if (type.getType().equals(NoticeAnnouncementType.PLAYER)) {
  97.         if (player != null) {
  98.         player.sendMessage(toSend);
  99.         }
  100.     }
  101.     }
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement