Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.kitteh.nametags;
- import java.io.File;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.List;
- import java.util.Map;
- import java.util.concurrent.ConcurrentHashMap;
- import java.util.logging.Logger;
- import org.bukkit.ChatColor;
- import org.bukkit.Server;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandSender;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.entity.Player;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.EventPriority;
- import org.bukkit.event.Listener;
- import org.bukkit.event.player.PlayerJoinEvent;
- import org.bukkit.metadata.FixedMetadataValue;
- import org.bukkit.plugin.PluginManager;
- import org.bukkit.plugin.java.JavaPlugin;
- import org.bukkit.scheduler.BukkitScheduler;
- import org.kitteh.tag.AsyncPlayerReceiveNameTagEvent;
- import org.kitteh.tag.TagAPI;
- public class NameTags extends JavaPlugin
- implements Listener
- {
- private static final String CONFIG_BASECOLOR = "baseColor";
- private static final String CONFIG_BASECOLOR_DEFAULT = "white";
- private static final String CONFIG_NOLONGNAMES = "noChangeForLongNames";
- private static final String CONFIG_ONLYSAME = "onlySeeSame";
- private static final String CONFIG_REFRESH = "refreshAutomatically";
- private static final String CONFIG_SET_DISPLAYNAME = "setDisplayName";
- private static final String CONFIG_SET_TABNAME = "setTabName";
- private static final String METADATA_NAME = "nametags.displayname";
- private static final Object ADORABLE_OBJECT = new Object();
- private File configFile;
- private int refreshTaskID;
- private boolean setDisplayName;
- private boolean setTabName;
- private boolean noLongNames;
- private boolean onlySeeSelf;
- private ChatColor baseColor;
- private final Map<String, String> nameTagMap = new ConcurrentHashMap();
- private final Map<String, Object> seenAlways = new ConcurrentHashMap();
- public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
- {
- if ((args.length > 0) && (args[0].equalsIgnoreCase("reload"))) {
- load();
- sender.sendMessage("Reloaded!");
- }
- return true;
- }
- public void onDisable()
- {
- for (Player player : getServer().getOnlinePlayers())
- if ((player != null) && (player.isOnline()))
- player.removeMetadata("nametags.displayname", this);
- }
- public void onEnable()
- {
- if (!getServer().getPluginManager().isPluginEnabled("TagAPI")) {
- getLogger().severe("TagAPI required. Get it at http://dev.bukkit.org/server-mods/tag/");
- getServer().getPluginManager().disablePlugin(this);
- return;
- }
- try {
- Class.forName("org.kitteh.tag.AsyncPlayerReceiveNameTagEvent");
- } catch (ClassNotFoundException e) {
- getLogger().severe("You need a newer version of TagAPI! Get it at http://dev.bukkit.org/server-mods/tag/");
- return;
- }
- this.configFile = new File(getDataFolder(), "config.yml");
- load();
- getServer().getPluginManager().registerEvents(this, this);
- }
- @EventHandler(priority=EventPriority.MONITOR)
- public void onJoin(PlayerJoinEvent event) {
- calculate(event.getPlayer());
- }
- @EventHandler(priority=EventPriority.LOW)
- public void onNameTag(AsyncPlayerReceiveNameTagEvent event) {
- String tag = getDisplay(event.getNamedPlayer());
- if (tag != null) {
- if ((this.onlySeeSelf) && (!this.seenAlways.containsKey(event.getNamedPlayer().getName()))) {
- String otherTag = getDisplay(event.getPlayer());
- if (otherTag == null) {
- event.setTag((this.baseColor != null ? this.baseColor : "") + event.getNamedPlayer().getName());
- return;
- }
- int ionamed = tag.indexOf(event.getNamedPlayer().getName());
- int iosee = otherTag.indexOf(event.getPlayer().getName());
- if ((ionamed <= 0) || (ionamed != iosee) || (!tag.substring(0, ionamed).equals(otherTag.substring(0, iosee)))) {
- event.setTag((this.baseColor != null ? this.baseColor : "") + event.getNamedPlayer().getName());
- return;
- }
- }
- event.setTag(tag);
- }
- }
- private void calculate(Player player) {
- StringBuilder name = new StringBuilder();
- List colors = Arrays.asList(Color.values());
- Collections.shuffle(colors);
- for (Color color : colors) {
- if (player.hasPermission(color.getNode())) {
- name.append(color.getColor());
- break;
- }
- }
- if ((name.length() == 0) && (this.baseColor != null)) {
- name.append(this.baseColor);
- }
- if ((name.length() > 1) && (name.charAt(1) == 'f')) {
- name.setLength(0);
- }
- List formats = Arrays.asList(Format.values());
- Collections.shuffle(formats);
- for (Format format : formats) {
- if (player.hasPermission(format.getNode())) {
- name.append(format.getColor());
- break;
- }
- }
- String cleanName = player.getName();
- name.append(cleanName);
- if (name.length() > 16) {
- if (this.noLongNames)
- name.delete(0, name.length()).append(cleanName);
- else {
- name.setLength(16);
- }
- }
- String newName = name.toString();
- player.setMetadata("nametags.displayname", new FixedMetadataValue(this, newName));
- this.nameTagMap.put(cleanName, newName);
- if (player.hasPermission("nametags.seenalways"))
- this.seenAlways.put(cleanName, ADORABLE_OBJECT);
- else {
- this.seenAlways.remove(cleanName);
- }
- if (this.setDisplayName) {
- player.setDisplayName(newName + ChatColor.RESET);
- }
- if (this.setTabName)
- player.setPlayerListName(newName);
- }
- private String getDisplay(Player player)
- {
- return (String)this.nameTagMap.get(player.getName());
- }
- private void load() {
- if (this.refreshTaskID != -1) {
- getServer().getScheduler().cancelTask(this.refreshTaskID);
- this.refreshTaskID = -1;
- }
- if (!this.configFile.exists()) {
- saveDefaultConfig();
- }
- reloadConfig();
- if (!getConfig().contains("baseColor")) {
- getConfig().set("baseColor", "white");
- }
- if (!getConfig().contains("noChangeForLongNames")) {
- getConfig().set("noChangeForLongNames", Boolean.valueOf(false));
- }
- if (!getConfig().contains("onlySeeSame")) {
- getConfig().set("onlySeeSame", Boolean.valueOf(false));
- }
- if (!getConfig().contains("refreshAutomatically")) {
- getConfig().set("refreshAutomatically", Boolean.valueOf(false));
- }
- if (!getConfig().contains("setDisplayName")) {
- getConfig().set("setDisplayName", Boolean.valueOf(false));
- }
- if (!getConfig().contains("setTabName")) {
- getConfig().set("setTabName", Boolean.valueOf(false));
- }
- saveConfig();
- if (getConfig().getBoolean("refreshAutomatically", false)) {
- this.refreshTaskID = getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable()
- {
- public void run() {
- NameTags.this.playerRefresh();
- }
- }
- , 1200L, 1200L);
- }
- boolean newSetDisplayName = getConfig().getBoolean("setDisplayName", false);
- boolean forceDisplayName = (this.setDisplayName) && (!newSetDisplayName);
- boolean newSetTabName = getConfig().getBoolean("setTabName", false);
- boolean forceTabName = (this.setTabName) && (!newSetTabName);
- if ((forceDisplayName) || (forceTabName))
- for (Player player : getServer().getOnlinePlayers()) {
- if (forceDisplayName) {
- player.setDisplayName(player.getName());
- }
- if (forceTabName)
- player.setPlayerListName(player.getName());
- }
- ChatColor newBaseColor;
- try
- {
- newBaseColor = ChatColor.valueOf(getConfig().getString("baseColor", "white").toUpperCase());
- } catch (Exception e) {
- newBaseColor = null;
- }
- this.baseColor = (newBaseColor == ChatColor.WHITE ? null : newBaseColor);
- this.setDisplayName = newSetDisplayName;
- this.setTabName = newSetTabName;
- this.noLongNames = getConfig().getBoolean("noChangeForLongNames", false);
- this.onlySeeSelf = getConfig().getBoolean("onlySeeSame", false);
- getServer().getScheduler().runTaskLater(this, new Runnable()
- {
- public void run() {
- NameTags.this.playerRefresh();
- }
- }
- , 2L);
- }
- private void playerRefresh()
- {
- for (Player player : getServer().getOnlinePlayers())
- if ((player != null) && (player.isOnline())) {
- String oldTag = getDisplay(player);
- calculate(player);
- String newTag = getDisplay(player);
- boolean one = (oldTag == null) && (newTag != null);
- boolean two = (oldTag != null) && (newTag == null);
- boolean three = (oldTag != null) && (newTag != null) && (!oldTag.equals(newTag));
- if ((one) || (two) || (three))
- TagAPI.refreshPlayer(player);
- }
- }
- private static enum Format
- {
- bold,
- italic,
- magic,
- strikethrough,
- underline;
- private final ChatColor color;
- private final String node;
- private Format() { this.color = ChatColor.valueOf(name().toUpperCase());
- this.node = ("nametags.format." + name()); }
- public ChatColor getColor()
- {
- return this.color;
- }
- public String getNode() {
- return this.node;
- }
- }
- private static enum Color
- {
- aqua,
- black,
- blue,
- dark_aqua,
- dark_blue,
- dark_gray,
- dark_green,
- dark_purple,
- dark_red,
- gold,
- gray,
- green,
- light_purple,
- red,
- yellow,
- white;
- private final ChatColor color;
- private final String node;
- private Color() { this.color = ChatColor.valueOf(name().toUpperCase());
- this.node = ("nametags.color." + name()); }
- public ChatColor getColor()
- {
- return this.color;
- }
- public String getNode() {
- return this.node;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement