Advertisement
Yuki_20

Untitled

Jan 5th, 2015
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.89 KB | None | 0 0
  1. package org.kitteh.nametags;
  2.  
  3. import java.io.File;
  4. import java.util.Arrays;
  5. import java.util.Collections;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.concurrent.ConcurrentHashMap;
  9. import java.util.logging.Logger;
  10. import org.bukkit.ChatColor;
  11. import org.bukkit.Server;
  12. import org.bukkit.command.Command;
  13. import org.bukkit.command.CommandSender;
  14. import org.bukkit.configuration.file.FileConfiguration;
  15. import org.bukkit.entity.Player;
  16. import org.bukkit.event.EventHandler;
  17. import org.bukkit.event.EventPriority;
  18. import org.bukkit.event.Listener;
  19. import org.bukkit.event.player.PlayerJoinEvent;
  20. import org.bukkit.metadata.FixedMetadataValue;
  21. import org.bukkit.plugin.PluginManager;
  22. import org.bukkit.plugin.java.JavaPlugin;
  23. import org.bukkit.scheduler.BukkitScheduler;
  24. import org.kitteh.tag.AsyncPlayerReceiveNameTagEvent;
  25. import org.kitteh.tag.TagAPI;
  26.  
  27. public class NameTags extends JavaPlugin
  28. implements Listener
  29. {
  30. private static final String CONFIG_BASECOLOR = "baseColor";
  31. private static final String CONFIG_BASECOLOR_DEFAULT = "white";
  32. private static final String CONFIG_NOLONGNAMES = "noChangeForLongNames";
  33. private static final String CONFIG_ONLYSAME = "onlySeeSame";
  34. private static final String CONFIG_REFRESH = "refreshAutomatically";
  35. private static final String CONFIG_SET_DISPLAYNAME = "setDisplayName";
  36. private static final String CONFIG_SET_TABNAME = "setTabName";
  37. private static final String METADATA_NAME = "nametags.displayname";
  38. private static final Object ADORABLE_OBJECT = new Object();
  39. private File configFile;
  40. private int refreshTaskID;
  41. private boolean setDisplayName;
  42. private boolean setTabName;
  43. private boolean noLongNames;
  44. private boolean onlySeeSelf;
  45. private ChatColor baseColor;
  46. private final Map<String, String> nameTagMap = new ConcurrentHashMap();
  47. private final Map<String, Object> seenAlways = new ConcurrentHashMap();
  48.  
  49. public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
  50. {
  51. if ((args.length > 0) && (args[0].equalsIgnoreCase("reload"))) {
  52. load();
  53. sender.sendMessage("Reloaded!");
  54. }
  55. return true;
  56. }
  57.  
  58. public void onDisable()
  59. {
  60. for (Player player : getServer().getOnlinePlayers())
  61. if ((player != null) && (player.isOnline()))
  62. player.removeMetadata("nametags.displayname", this);
  63. }
  64.  
  65. public void onEnable()
  66. {
  67. if (!getServer().getPluginManager().isPluginEnabled("TagAPI")) {
  68. getLogger().severe("TagAPI required. Get it at http://dev.bukkit.org/server-mods/tag/");
  69. getServer().getPluginManager().disablePlugin(this);
  70. return;
  71. }
  72. try {
  73. Class.forName("org.kitteh.tag.AsyncPlayerReceiveNameTagEvent");
  74. } catch (ClassNotFoundException e) {
  75. getLogger().severe("You need a newer version of TagAPI! Get it at http://dev.bukkit.org/server-mods/tag/");
  76. return;
  77. }
  78. this.configFile = new File(getDataFolder(), "config.yml");
  79. load();
  80. getServer().getPluginManager().registerEvents(this, this);
  81. }
  82. @EventHandler(priority=EventPriority.MONITOR)
  83. public void onJoin(PlayerJoinEvent event) {
  84. calculate(event.getPlayer());
  85. }
  86. @EventHandler(priority=EventPriority.LOW)
  87. public void onNameTag(AsyncPlayerReceiveNameTagEvent event) {
  88. String tag = getDisplay(event.getNamedPlayer());
  89. if (tag != null) {
  90. if ((this.onlySeeSelf) && (!this.seenAlways.containsKey(event.getNamedPlayer().getName()))) {
  91. String otherTag = getDisplay(event.getPlayer());
  92. if (otherTag == null) {
  93. event.setTag((this.baseColor != null ? this.baseColor : "") + event.getNamedPlayer().getName());
  94. return;
  95. }
  96. int ionamed = tag.indexOf(event.getNamedPlayer().getName());
  97. int iosee = otherTag.indexOf(event.getPlayer().getName());
  98. if ((ionamed <= 0) || (ionamed != iosee) || (!tag.substring(0, ionamed).equals(otherTag.substring(0, iosee)))) {
  99. event.setTag((this.baseColor != null ? this.baseColor : "") + event.getNamedPlayer().getName());
  100. return;
  101. }
  102. }
  103. event.setTag(tag);
  104. }
  105. }
  106.  
  107. private void calculate(Player player) {
  108. StringBuilder name = new StringBuilder();
  109. List colors = Arrays.asList(Color.values());
  110. Collections.shuffle(colors);
  111. for (Color color : colors) {
  112. if (player.hasPermission(color.getNode())) {
  113. name.append(color.getColor());
  114. break;
  115. }
  116. }
  117. if ((name.length() == 0) && (this.baseColor != null)) {
  118. name.append(this.baseColor);
  119. }
  120. if ((name.length() > 1) && (name.charAt(1) == 'f')) {
  121. name.setLength(0);
  122. }
  123. List formats = Arrays.asList(Format.values());
  124. Collections.shuffle(formats);
  125. for (Format format : formats) {
  126. if (player.hasPermission(format.getNode())) {
  127. name.append(format.getColor());
  128. break;
  129. }
  130. }
  131. String cleanName = player.getName();
  132. name.append(cleanName);
  133. if (name.length() > 16) {
  134. if (this.noLongNames)
  135. name.delete(0, name.length()).append(cleanName);
  136. else {
  137. name.setLength(16);
  138. }
  139. }
  140. String newName = name.toString();
  141. player.setMetadata("nametags.displayname", new FixedMetadataValue(this, newName));
  142.  
  143. this.nameTagMap.put(cleanName, newName);
  144. if (player.hasPermission("nametags.seenalways"))
  145. this.seenAlways.put(cleanName, ADORABLE_OBJECT);
  146. else {
  147. this.seenAlways.remove(cleanName);
  148. }
  149.  
  150. if (this.setDisplayName) {
  151. player.setDisplayName(newName + ChatColor.RESET);
  152. }
  153. if (this.setTabName)
  154. player.setPlayerListName(newName);
  155. }
  156.  
  157. private String getDisplay(Player player)
  158. {
  159. return (String)this.nameTagMap.get(player.getName());
  160. }
  161.  
  162. private void load() {
  163. if (this.refreshTaskID != -1) {
  164. getServer().getScheduler().cancelTask(this.refreshTaskID);
  165. this.refreshTaskID = -1;
  166. }
  167. if (!this.configFile.exists()) {
  168. saveDefaultConfig();
  169. }
  170. reloadConfig();
  171. if (!getConfig().contains("baseColor")) {
  172. getConfig().set("baseColor", "white");
  173. }
  174. if (!getConfig().contains("noChangeForLongNames")) {
  175. getConfig().set("noChangeForLongNames", Boolean.valueOf(false));
  176. }
  177. if (!getConfig().contains("onlySeeSame")) {
  178. getConfig().set("onlySeeSame", Boolean.valueOf(false));
  179. }
  180. if (!getConfig().contains("refreshAutomatically")) {
  181. getConfig().set("refreshAutomatically", Boolean.valueOf(false));
  182. }
  183. if (!getConfig().contains("setDisplayName")) {
  184. getConfig().set("setDisplayName", Boolean.valueOf(false));
  185. }
  186. if (!getConfig().contains("setTabName")) {
  187. getConfig().set("setTabName", Boolean.valueOf(false));
  188. }
  189. saveConfig();
  190. if (getConfig().getBoolean("refreshAutomatically", false)) {
  191. this.refreshTaskID = getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable()
  192. {
  193. public void run() {
  194. NameTags.this.playerRefresh();
  195. }
  196. }
  197. , 1200L, 1200L);
  198. }
  199.  
  200. boolean newSetDisplayName = getConfig().getBoolean("setDisplayName", false);
  201. boolean forceDisplayName = (this.setDisplayName) && (!newSetDisplayName);
  202. boolean newSetTabName = getConfig().getBoolean("setTabName", false);
  203. boolean forceTabName = (this.setTabName) && (!newSetTabName);
  204. if ((forceDisplayName) || (forceTabName))
  205. for (Player player : getServer().getOnlinePlayers()) {
  206. if (forceDisplayName) {
  207. player.setDisplayName(player.getName());
  208. }
  209. if (forceTabName)
  210. player.setPlayerListName(player.getName());
  211. }
  212. ChatColor newBaseColor;
  213. try
  214. {
  215. newBaseColor = ChatColor.valueOf(getConfig().getString("baseColor", "white").toUpperCase());
  216. } catch (Exception e) {
  217. newBaseColor = null;
  218. }
  219. this.baseColor = (newBaseColor == ChatColor.WHITE ? null : newBaseColor);
  220. this.setDisplayName = newSetDisplayName;
  221. this.setTabName = newSetTabName;
  222. this.noLongNames = getConfig().getBoolean("noChangeForLongNames", false);
  223. this.onlySeeSelf = getConfig().getBoolean("onlySeeSame", false);
  224. getServer().getScheduler().runTaskLater(this, new Runnable()
  225. {
  226. public void run() {
  227. NameTags.this.playerRefresh();
  228. }
  229. }
  230. , 2L);
  231. }
  232.  
  233. private void playerRefresh()
  234. {
  235. for (Player player : getServer().getOnlinePlayers())
  236. if ((player != null) && (player.isOnline())) {
  237. String oldTag = getDisplay(player);
  238. calculate(player);
  239. String newTag = getDisplay(player);
  240. boolean one = (oldTag == null) && (newTag != null);
  241. boolean two = (oldTag != null) && (newTag == null);
  242. boolean three = (oldTag != null) && (newTag != null) && (!oldTag.equals(newTag));
  243. if ((one) || (two) || (three))
  244. TagAPI.refreshPlayer(player);
  245. }
  246. }
  247.  
  248. private static enum Format
  249. {
  250. bold,
  251. italic,
  252. magic,
  253. strikethrough,
  254. underline;
  255.  
  256. private final ChatColor color;
  257. private final String node;
  258.  
  259. private Format() { this.color = ChatColor.valueOf(name().toUpperCase());
  260. this.node = ("nametags.format." + name()); }
  261.  
  262. public ChatColor getColor()
  263. {
  264. return this.color;
  265. }
  266.  
  267. public String getNode() {
  268. return this.node;
  269. }
  270. }
  271.  
  272. private static enum Color
  273. {
  274. aqua,
  275. black,
  276. blue,
  277. dark_aqua,
  278. dark_blue,
  279. dark_gray,
  280. dark_green,
  281. dark_purple,
  282. dark_red,
  283. gold,
  284. gray,
  285. green,
  286. light_purple,
  287. red,
  288. yellow,
  289. white;
  290.  
  291. private final ChatColor color;
  292. private final String node;
  293.  
  294. private Color() { this.color = ChatColor.valueOf(name().toUpperCase());
  295. this.node = ("nametags.color." + name()); }
  296.  
  297. public ChatColor getColor()
  298. {
  299. return this.color;
  300. }
  301.  
  302. public String getNode() {
  303. return this.node;
  304. }
  305. }
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement