From 0802de4b6f5442db4b24535fefe3db0fbc51bad8 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Thu, 17 Jan 2019 22:23:04 +0100 Subject: [PATCH] TC/NC & Removed old config stuff Moved town/nation colors into their component Removed save command that was apparently there since 2015 It still loads the town/nation colors from the old config if needed Notification sound/pitch were the last config options to be converted over #85 --- pom.xml | 542 +++++++++--------- .../buttondevteam/chat/ChatProcessing.java | 18 +- .../java/buttondevteam/chat/PluginMain.java | 119 +--- .../commands/ucmds/admin/SaveCommand.java | 23 - .../chat/components/TownColorComponent.java | 24 - .../towncolors}/NColorCommand.java | 11 +- .../towncolors}/NationColorCommand.java | 5 +- .../towncolors}/TownColorCommand.java | 16 +- .../towncolors/TownColorComponent.java | 201 +++++++ .../towncolors}/TownyListener.java | 18 +- .../towncolors}/admin/NationColorCommand.java | 10 +- .../towncolors}/admin/TCCount.java | 8 +- .../towncolors}/admin/TownColorCommand.java | 16 +- .../listener/PlayerJoinLeaveListener.java | 69 +-- .../chat/listener/PlayerListener.java | 6 +- 15 files changed, 556 insertions(+), 530 deletions(-) delete mode 100644 src/main/java/buttondevteam/chat/commands/ucmds/admin/SaveCommand.java delete mode 100644 src/main/java/buttondevteam/chat/components/TownColorComponent.java rename src/main/java/buttondevteam/chat/{commands/ucmds => components/towncolors}/NColorCommand.java (87%) rename src/main/java/buttondevteam/chat/{commands/ucmds => components/towncolors}/NationColorCommand.java (89%) rename src/main/java/buttondevteam/chat/{commands/ucmds => components/towncolors}/TownColorCommand.java (76%) create mode 100644 src/main/java/buttondevteam/chat/components/towncolors/TownColorComponent.java rename src/main/java/buttondevteam/chat/{listener => components/towncolors}/TownyListener.java (77%) rename src/main/java/buttondevteam/chat/{commands/ucmds => components/towncolors}/admin/NationColorCommand.java (82%) rename src/main/java/buttondevteam/chat/{commands/ucmds => components/towncolors}/admin/TCCount.java (67%) rename src/main/java/buttondevteam/chat/{commands/ucmds => components/towncolors}/admin/TownColorCommand.java (85%) diff --git a/pom.xml b/pom.xml index 67ca3b2..b8cbcb0 100644 --- a/pom.xml +++ b/pom.xml @@ -1,263 +1,281 @@ - - 4.0.0 - 0.0.1-SNAPSHOT - The Button Minecraft Chat Plugin - The Button Minecraft Chat Plugin - - src/main/java - - - src - - **/*.java - - - - src/main/resources - - *.properties - *.yml - *.csv - *.txt - - true - - - ButtonChat - - - maven-compiler-plugin - 3.3 - - 1.8 - 1.8 - - - - org.apache.maven.plugins - maven-shade-plugin - 2.4.2 - - - package - - shade - - - - - net.sourceforge.htmlcleaner:htmlcleaner - org.javassist:javassist - - - - - - - - org.apache.maven.plugins - maven-resources-plugin - 3.0.1 - - - copy - compile - - copy-resources - - - target - - - resources - - - - - - - - org.apache.maven.plugins - maven-failsafe-plugin - 2.12.4 - - - - integration-test - verify - - test - - - - true - false - - - - - - - - buttondevteam - - - spigot-repo - https://hub.spigotmc.org/nexus/content/repositories/snapshots/ - - - jitpack - https://jitpack.io/ - - - Essentials - http://repo.ess3.net/content/repositories/essrel/ - - - Votifier - https://dl.bintray.com/nuvotifier/maven/ - - - Minigames - http://maven.addstar.com.au/artifactory/release - - - vault-repo - http://nexus.hc.to/content/repositories/pub_releases - - - - projectlombok.org - http://projectlombok.org/mavenrepo - - - - - org.spigotmc - spigot-api - 1.12.2-R0.1-SNAPSHOT - - - com.github.TBMCPlugins.ButtonCore - ButtonCore - ${env.TRAVIS_BRANCH}-SNAPSHOT - - - - net.sourceforge.htmlcleaner - htmlcleaner - 2.16 - - - - org.reflections - reflections - 0.9.10 - - - net.ess3 - Essentials - 2.13.1 - - - com.github.TBMCPlugins.ButtonCore - Towny - 8d3b6b6 - - - com.vexsoftware - nuvotifier-universal - 2.3.4 - - - - net.milkbowl.vault - VaultAPI - 1.6 - provided - - - org.projectlombok - lombok - 1.16.16 - provided - - - org.spigotmc - spigot - 1.12.2-R0.1-SNAPSHOT - provided - - - com.github.webbukkit - Dynmap-Towny - master-SNAPSHOT - - - com.github.webbukkit - Dynmap - v2.5 - - - - junit - junit - 4.12 - test - - - ButtonChat - - TBMCPlugins - https://github.com/TBMCPlugins - - - - githubo - UTF-8 - + + 4.0.0 + 0.0.1-SNAPSHOT + The Button Minecraft Chat Plugin + The Button Minecraft Chat Plugin + + src/main/java + + + src + + **/*.java + + + + src/main/resources + + *.properties + *.yml + *.csv + *.txt + + true + + + ButtonChat + + + maven-compiler-plugin + 3.3 + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-shade-plugin + 2.4.2 + + + package + + shade + + + + + net.sourceforge.htmlcleaner:htmlcleaner + org.javassist:javassist + + + + + + + + org.apache.maven.plugins + maven-resources-plugin + 3.0.1 + + + copy + compile + + copy-resources + + + target + + + resources + + + + + + + + org.apache.maven.plugins + maven-failsafe-plugin + 2.12.4 + + + + integration-test + verify + + test + + + + true + false + + + + + + + + buttondevteam + + + spigot-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + + jitpack + https://jitpack.io/ + + + Essentials + http://repo.ess3.net/content/repositories/essrel/ + + + Votifier + https://dl.bintray.com/nuvotifier/maven/ + + + Minigames + http://maven.addstar.com.au/artifactory/release + + + vault-repo + http://nexus.hc.to/content/repositories/pub_releases + + + + projectlombok.org + http://projectlombok.org/mavenrepo + + + + + org.spigotmc + spigot-api + 1.12.2-R0.1-SNAPSHOT + + + com.github.TBMCPlugins.ButtonCore + ButtonCore + ${branch}-SNAPSHOT + + + + net.sourceforge.htmlcleaner + htmlcleaner + 2.16 + + + + org.reflections + reflections + 0.9.10 + + + net.ess3 + Essentials + 2.13.1 + + + com.github.TBMCPlugins.ButtonCore + Towny + 8d3b6b6 + + + com.vexsoftware + nuvotifier-universal + 2.3.4 + + + + net.milkbowl.vault + VaultAPI + 1.6 + provided + + + org.projectlombok + lombok + 1.16.16 + provided + + + org.spigotmc + spigot + 1.12.2-R0.1-SNAPSHOT + provided + + + com.github.webbukkit + Dynmap-Towny + master-SNAPSHOT + + + com.github.webbukkit + Dynmap + v2.5 + + + + junit + junit + 4.12 + test + + + ButtonChat + + TBMCPlugins + https://github.com/TBMCPlugins + + + + githubo + UTF-8 + + master + + + + + + ci + + + env.TRAVIS_BRANCH + + + + + ${env.TRAVIS_BRANCH} + + + \ No newline at end of file diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index 5bcf174..0fc251c 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -46,7 +46,7 @@ public class ChatProcessing { private static final Pattern ITALIC_PATTERN = Pattern.compile("\\*"); private static final Pattern BOLD_PATTERN = Pattern.compile("\\*\\*"); private static final Pattern CODE_PATTERN = Pattern.compile("`"); - private static final Pattern MASKED_LINK_PATTERN = Pattern.compile("\\[([^\\[\\]])\\]\\(([^()])\\)"); + private static final Pattern MASKED_LINK_PATTERN = Pattern.compile("\\[([^\\[\\]])]\\(([^()])\\)"); private static final Pattern SOMEONE_PATTERN = Pattern.compile("@someone"); //TODO private static final Pattern STRIKETHROUGH_PATTERN = Pattern.compile("~~"); private static final Color[] RainbowPresserColors = new Color[]{Color.Red, Color.Gold, Color.Yellow, Color.Green, @@ -285,11 +285,11 @@ public class ChatProcessing { } ChatPlayer mpp = TBMCPlayer.getPlayer(nottest ? p.getUniqueId() : new UUID(0, 0), ChatPlayer.class); if (nottest) { - if (PlayerListener.NotificationSound == null) + if (PluginMain.Instance.notificationSound().get().length() == 0) p.playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1.0f, 0.5f); // TODO: Airhorn else - p.playSound(p.getLocation(), PlayerListener.NotificationSound, 1.0f, - (float) PlayerListener.NotificationPitch); + p.playSound(p.getLocation(), PluginMain.Instance.notificationSound().get(), 1.0f, + PluginMain.Instance.notificationPitch().get()); } String color = String.format("§%x", (mpp.GetFlairColor() == 0x00 ? 0xb : mpp.GetFlairColor())); return color + (nottest ? p.getName() : pn.get()) + "§r"; //Fix name casing, except when testing @@ -305,11 +305,11 @@ public class ChatProcessing { + match.toLowerCase() + " but was reported as online."); return "§c" + match + "§r"; } - if (PlayerListener.NotificationSound == null) - p.playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1.0f, 0.5f); - else - p.playSound(p.getLocation(), PlayerListener.NotificationSound, 1.0f, - (float) PlayerListener.NotificationPitch); + if (PluginMain.Instance.notificationSound().get().length() == 0) + p.playSound(p.getLocation(), Sound.ENTITY_ARROW_HIT_PLAYER, 1.0f, 0.5f); // TODO: Airhorn + else + p.playSound(p.getLocation(), PluginMain.Instance.notificationSound().get(), 1.0f, + PluginMain.Instance.notificationPitch().get()); return PluginMain.essentials.getUser(p).getNickname(); } error.accept("Player nicknamed " + match.toLowerCase() diff --git a/src/main/java/buttondevteam/chat/PluginMain.java b/src/main/java/buttondevteam/chat/PluginMain.java index 1599d6f..16cd62f 100644 --- a/src/main/java/buttondevteam/chat/PluginMain.java +++ b/src/main/java/buttondevteam/chat/PluginMain.java @@ -1,42 +1,30 @@ package buttondevteam.chat; import buttondevteam.chat.commands.YeehawCommand; -import buttondevteam.chat.commands.ucmds.TownColorCommand; -import buttondevteam.chat.components.TownColorComponent; import buttondevteam.chat.components.announce.AnnouncerComponent; import buttondevteam.chat.components.flair.FlairComponent; +import buttondevteam.chat.components.towncolors.TownColorComponent; +import buttondevteam.chat.components.towncolors.TownyListener; import buttondevteam.chat.components.towny.TownyComponent; import buttondevteam.chat.listener.PlayerJoinLeaveListener; import buttondevteam.chat.listener.PlayerListener; -import buttondevteam.chat.listener.TownyListener; import buttondevteam.component.channel.Channel; import buttondevteam.lib.TBMCCoreAPI; +import buttondevteam.lib.architecture.ButtonPlugin; import buttondevteam.lib.architecture.Component; +import buttondevteam.lib.architecture.ConfigData; import buttondevteam.lib.chat.Color; import buttondevteam.lib.chat.TBMCChatAPI; import com.earth2me.essentials.Essentials; -import lombok.val; import net.milkbowl.vault.chat.Chat; import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.permission.Permission; import org.bukkit.Bukkit; import org.bukkit.command.ConsoleCommandSender; -import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.plugin.RegisteredServiceProvider; -import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scoreboard.Scoreboard; -import org.dynmap.towny.DTBridge; -import org.dynmap.towny.DynmapTownyPlugin; -import java.io.File; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.function.Function; -import java.util.stream.Collectors; - -public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. +public class PluginMain extends ButtonPlugin { // Translated to Java: 2015.07.15. // A user, which flair isn't obtainable: // https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ public static PluginMain Instance; @@ -47,9 +35,17 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. public static Channel TownChat; public static Channel NationChat; + public ConfigData notificationSound() { + return getIConfig().getData("notificationSound", ""); + } + + public ConfigData notificationPitch() { + return getIConfig().getData("notificationPitch", 1.0f); + } + // Fired when plugin is first enabled @Override - public void onEnable() { + public void pluginEnable() { Instance = this; PluginMain.essentials = (Essentials) (Bukkit.getPluginManager().getPlugin("Essentials")); @@ -58,24 +54,13 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. TBMCCoreAPI.RegisterEventsForExceptions(new TownyListener(), this); TBMCChatAPI.AddCommands(this, YeehawCommand.class); Console = this.getServer().getConsoleSender(); - LoadFiles(); SB = getServer().getScoreboardManager().getMainScoreboard(); // Main can be detected with @a[score_...] Component.registerComponent(this, new TownyComponent()); - TownColors.keySet().removeIf(t -> !TownyComponent.TU.getTownsMap().containsKey(t)); // Removes town colors for deleted/renamed towns - NationColor.keySet().removeIf(n -> !TownyComponent.TU.getNationsMap().containsKey(n)); // Removes nation colors for deleted/renamed nations TBMCChatAPI.RegisterChatChannel(new Channel("§7RP§f", Color.Gray, "rp", null)); //Since it's null, it's recognised as global - Bukkit.getScheduler().runTask(this, () -> { - val dtp = (DynmapTownyPlugin) Bukkit.getPluginManager().getPlugin("Dynmap-Towny"); - if (dtp == null) - return; - for (val entry : TownColors.entrySet()) - setTownColor(dtp, buttondevteam.chat.commands.ucmds.admin.TownColorCommand.getTownNameCased(entry.getKey()), entry.getValue()); - }); - if (!setupEconomy() || !setupPermissions()) TBMCCoreAPI.SendException("We're in trouble", new Exception("Failed to set up economy or permissions!")); @@ -84,85 +69,11 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. Component.registerComponent(this, new AnnouncerComponent()); } - /** - * Sets a town's color on Dynmap. - * - * @param dtp A reference for the Dynmap-Towny plugin - * @param town The town's name using the correct casing - * @param colors The town's colors - */ - public static void setTownColor(DynmapTownyPlugin dtp, String town, Color[] colors) { - Function c2i = c -> c.getRed() << 16 | c.getGreen() << 8 | c.getBlue(); - try { - DTBridge.setTownColor(dtp, town, c2i.apply(colors[0]), - c2i.apply(colors.length > 1 ? colors[1] : colors[0])); - } catch (Exception e) { - TBMCCoreAPI.SendException("Failed to set town color for town " + town + "!", e); - } - } - public static Essentials essentials = null; // Fired when plugin is disabled @Override - public void onDisable() { - SaveFiles(); - } - - /** - * Names lowercased - */ - public static Map TownColors = new HashMap<>(); - /** - * Names lowercased - nation color gets added to town colors when needed - */ - public static Map NationColor = new HashMap<>(); - - @SuppressWarnings("unchecked") - private static void LoadFiles() { - PluginMain.Instance.getLogger().info("Loading files..."); - try { - File file = new File("TBMC/chatsettings.yml"); - if (file.exists()) { - YamlConfiguration yc = new YamlConfiguration(); - yc.load(file); - PlayerListener.NotificationSound = yc.getString("notificationsound"); - PlayerListener.NotificationPitch = yc.getDouble("notificationpitch"); - val cs = yc.getConfigurationSection("towncolors"); - if (cs != null) - TownColors.putAll(cs.getValues(true).entrySet().stream() - .collect(Collectors.toMap(Map.Entry::getKey, v -> ((List) v.getValue()).stream() - .map(Color::valueOf).toArray(Color[]::new)))); - TownColorCommand.ColorCount = (byte) yc.getInt("towncolorcount", 1); - val ncs = yc.getConfigurationSection("nationcolors"); - if (ncs != null) - NationColor.putAll(ncs.getValues(true).entrySet().stream() - .collect(Collectors.toMap(Map.Entry::getKey, v -> Color.valueOf((String) v.getValue())))); - PluginMain.Instance.getLogger().info("Loaded files!"); - } else - PluginMain.Instance.getLogger().info("No files to load, first run probably."); - } catch (Exception e) { - TBMCCoreAPI.SendException("Error while loading chat files!", e); - } - } - - public static void SaveFiles() { - PluginMain.Instance.getLogger().info("Saving files..."); - try { - File file = new File("TBMC/chatsettings.yml"); - YamlConfiguration yc = new YamlConfiguration(); - yc.set("notificationsound", PlayerListener.NotificationSound); - yc.set("notificationpitch", PlayerListener.NotificationPitch); - yc.createSection("towncolors", TownColors.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, - v -> Arrays.stream(v.getValue()).map(Enum::toString).toArray(String[]::new)))); - yc.set("towncolorcount", TownColorCommand.ColorCount); - yc.createSection("nationcolors", NationColor.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, - v -> v.getValue().toString()))); - yc.save(file); - PluginMain.Instance.getLogger().info("Saved files!"); - } catch (Exception e) { - TBMCCoreAPI.SendException("Error while loading chat files!", e); - } + public void pluginDisable() { } public static Permission permission = null; diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/admin/SaveCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/admin/SaveCommand.java deleted file mode 100644 index 552d1b6..0000000 --- a/src/main/java/buttondevteam/chat/commands/ucmds/admin/SaveCommand.java +++ /dev/null @@ -1,23 +0,0 @@ -package buttondevteam.chat.commands.ucmds.admin; - -import org.bukkit.command.CommandSender; - -import buttondevteam.chat.PluginMain; - -public class SaveCommand extends AdminCommandBase { - - @Override - public String[] GetHelpText(String alias) { - return new String[] { "§6---- Save config ----", - "This command saves the config file(s)" }; - } - - @Override - public boolean OnCommand(CommandSender sender, String alias, - String[] args) { - PluginMain.SaveFiles(); // 2015.08.09. - sender.sendMessage("§bSaved files. Now you can edit them and reload if you want.§r"); - return true; - } - -} diff --git a/src/main/java/buttondevteam/chat/components/TownColorComponent.java b/src/main/java/buttondevteam/chat/components/TownColorComponent.java deleted file mode 100644 index 8a82040..0000000 --- a/src/main/java/buttondevteam/chat/components/TownColorComponent.java +++ /dev/null @@ -1,24 +0,0 @@ -package buttondevteam.chat.components; - -import buttondevteam.lib.architecture.Component; -import buttondevteam.lib.architecture.ConfigData; - -public class TownColorComponent extends Component { - public ConfigData colorCount() { //TODO - return getConfig().getData("colorCount", (byte) 1, cc -> (byte) cc, cc -> (int) cc); - } - - public ConfigData useNationColors() { //TODO - return getConfig().getData("useNationColors", true); - } - - @Override - protected void enable() { - //TODO: Don't register all commands automatically (welp) - } - - @Override - protected void disable() { - - } -} diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/NColorCommand.java b/src/main/java/buttondevteam/chat/components/towncolors/NColorCommand.java similarity index 87% rename from src/main/java/buttondevteam/chat/commands/ucmds/NColorCommand.java rename to src/main/java/buttondevteam/chat/components/towncolors/NColorCommand.java index 4c9943e..5234490 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/NColorCommand.java +++ b/src/main/java/buttondevteam/chat/components/towncolors/NColorCommand.java @@ -1,9 +1,8 @@ -package buttondevteam.chat.commands.ucmds; +package buttondevteam.chat.components.towncolors; import buttondevteam.chat.ChatPlayer; -import buttondevteam.chat.PluginMain; +import buttondevteam.chat.commands.ucmds.UCommandBase; import buttondevteam.chat.components.towny.TownyComponent; -import buttondevteam.chat.listener.PlayerJoinLeaveListener; import buttondevteam.lib.chat.Color; import buttondevteam.lib.chat.CommandClass; import buttondevteam.lib.chat.OptionallyPlayerCommandClass; @@ -54,7 +53,7 @@ public class NColorCommand extends UCommandBase { return true; } String[] nameparts = arg.split("[|:]"); - Color[] towncolors = PluginMain.TownColors.get(town.getName().toLowerCase()); + Color[] towncolors = TownColorComponent.TownColors.get(town.getName().toLowerCase()); if (towncolors == null) { player.sendMessage("§cYour town doesn't have a color set. The town mayor can set it using /u towncolor."); return true; @@ -72,8 +71,8 @@ public class NColorCommand extends UCommandBase { return true; } ChatPlayer.getPlayer(player.getUniqueId(), ChatPlayer.class).NameColorLocations() - .set(new ArrayList<>(Arrays.stream(nameparts).map(np -> np.length()).collect(Collectors.toList()))); // No byte[], no TIntArrayList - PlayerJoinLeaveListener.updatePlayerColors(player); + .set(new ArrayList<>(Arrays.stream(nameparts).map(String::length).collect(Collectors.toList()))); // No byte[], no TIntArrayList + TownColorComponent.updatePlayerColors(player); player.sendMessage("§bName colors set: " + player.getDisplayName()); return true; } diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/NationColorCommand.java b/src/main/java/buttondevteam/chat/components/towncolors/NationColorCommand.java similarity index 89% rename from src/main/java/buttondevteam/chat/commands/ucmds/NationColorCommand.java rename to src/main/java/buttondevteam/chat/components/towncolors/NationColorCommand.java index 8cba718..4c4c1ad 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/NationColorCommand.java +++ b/src/main/java/buttondevteam/chat/components/towncolors/NationColorCommand.java @@ -1,5 +1,6 @@ -package buttondevteam.chat.commands.ucmds; +package buttondevteam.chat.components.towncolors; +import buttondevteam.chat.commands.ucmds.UCommandBase; import buttondevteam.chat.components.towny.TownyComponent; import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.chat.CommandClass; @@ -44,6 +45,6 @@ public class NationColorCommand extends UCommandBase { player.sendMessage("§cCouldn't find your town/nation... Error reported."); return true; } - return buttondevteam.chat.commands.ucmds.admin.NationColorCommand.SetNationColor(player, alias, a); + return buttondevteam.chat.components.towncolors.admin.NationColorCommand.SetNationColor(player, alias, a); } } diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/TownColorCommand.java b/src/main/java/buttondevteam/chat/components/towncolors/TownColorCommand.java similarity index 76% rename from src/main/java/buttondevteam/chat/commands/ucmds/TownColorCommand.java rename to src/main/java/buttondevteam/chat/components/towncolors/TownColorCommand.java index bdda3d8..685d5ac 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/TownColorCommand.java +++ b/src/main/java/buttondevteam/chat/components/towncolors/TownColorCommand.java @@ -1,11 +1,13 @@ -package buttondevteam.chat.commands.ucmds; +package buttondevteam.chat.components.towncolors; +import buttondevteam.chat.commands.ucmds.UCommandBase; import buttondevteam.chat.components.towny.TownyComponent; import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.chat.CommandClass; import buttondevteam.lib.chat.OptionallyPlayerCommandClass; import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; import com.palmergames.bukkit.towny.object.Resident; +import lombok.val; import org.bukkit.entity.Player; @CommandClass // TODO: /u u when annotation not present @@ -14,7 +16,8 @@ public class TownColorCommand extends UCommandBase { @Override public String GetHelpText(String alias)[] { StringBuilder cns = new StringBuilder(" "); - for (int i = 2; i <= ColorCount; i++) + val comp = (TownColorComponent) getComponent(); + for (int i = 2; i <= comp.colorCount().get(); i++) cns.append(" [colorname").append(i).append("]"); return new String[] { // "§6---- Town Color ----", // @@ -26,8 +29,6 @@ public class TownColorCommand extends UCommandBase { }; } - public static byte ColorCount; - @Override public boolean OnCommand(Player player, String alias, String[] args) { Resident res; @@ -36,8 +37,9 @@ public class TownColorCommand extends UCommandBase { player.sendMessage("§cYou need to be the mayor of a town to set it's colors."); return true; } - if (args.length > ColorCount) { - player.sendMessage("You can only use " + ColorCount + " color" + (ColorCount > 1 ? "s" : "") + "."); + val comp = (TownColorComponent) getComponent(); + if (args.length > comp.colorCount().get()) { + player.sendMessage("You can only use " + comp.colorCount().get() + " color" + (comp.colorCount().get() > 1 ? "s" : "") + "."); return true; } String[] a = new String[args.length + 1]; @@ -49,6 +51,6 @@ public class TownColorCommand extends UCommandBase { player.sendMessage("§cCouldn't find your town... Error reported."); return true; } - return buttondevteam.chat.commands.ucmds.admin.TownColorCommand.SetTownColor(player, alias, a); + return buttondevteam.chat.components.towncolors.admin.TownColorCommand.SetTownColor(player, alias, a); } } diff --git a/src/main/java/buttondevteam/chat/components/towncolors/TownColorComponent.java b/src/main/java/buttondevteam/chat/components/towncolors/TownColorComponent.java new file mode 100644 index 0000000..a44bdbe --- /dev/null +++ b/src/main/java/buttondevteam/chat/components/towncolors/TownColorComponent.java @@ -0,0 +1,201 @@ +package buttondevteam.chat.components.towncolors; + +import buttondevteam.chat.ChatPlayer; +import buttondevteam.chat.PluginMain; +import buttondevteam.chat.components.towncolors.admin.TCCount; +import buttondevteam.chat.components.towny.TownyComponent; +import buttondevteam.core.ComponentManager; +import buttondevteam.lib.TBMCCoreAPI; +import buttondevteam.lib.architecture.Component; +import buttondevteam.lib.architecture.ComponentMetadata; +import buttondevteam.lib.architecture.ConfigData; +import buttondevteam.lib.chat.Color; +import com.earth2me.essentials.User; +import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; +import lombok.experimental.var; +import lombok.val; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; +import org.dynmap.towny.DTBridge; +import org.dynmap.towny.DynmapTownyPlugin; + +import java.io.File; +import java.util.*; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.BiFunction; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.stream.Collectors; + +@ComponentMetadata(depends = TownyComponent.class) +public class TownColorComponent extends Component { + /** + * Names lowercased + */ + public static Map TownColors = new HashMap<>(); + /** + * Names lowercased - nation color gets added to town colors when needed + */ + public static Map NationColor = new HashMap<>(); + + public ConfigData colorCount() { + return getConfig().getData("colorCount", (byte) 1, cc -> (byte) cc, cc -> (int) cc); + } + + public ConfigData useNationColors() { //TODO + return getConfig().getData("useNationColors", true); + } + + @SuppressWarnings("unchecked") + @Override + protected void enable() { + //TODO: Don't register all commands automatically (welp) + Consumer loadTC = cs -> TownColorComponent.TownColors.putAll(cs.getValues(true).entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, v -> ((List) v.getValue()).stream() + .map(Color::valueOf).toArray(Color[]::new)))); + Consumer loadNC = ncs -> + TownColorComponent.NationColor.putAll(ncs.getValues(true).entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, v -> Color.valueOf((String) v.getValue())))); + var cs = getConfig().getConfig().getConfigurationSection("towncolors"); + if (cs != null) + loadTC.accept(cs); + else + load_old(loadTC, null); //Load old data + var ncs = getConfig().getConfig().getConfigurationSection("nationcolors"); + if (ncs != null) + loadNC.accept(ncs); + else + load_old(null, loadNC); //Why not choose by making different args null + + TownColors.keySet().removeIf(t -> !TownyComponent.TU.getTownsMap().containsKey(t)); // Removes town colors for deleted/renamed towns + NationColor.keySet().removeIf(n -> !TownyComponent.TU.getNationsMap().containsKey(n)); // Removes nation colors for deleted/renamed nations + + Bukkit.getScheduler().runTask(getPlugin(), () -> { + val dtp = (DynmapTownyPlugin) Bukkit.getPluginManager().getPlugin("Dynmap-Towny"); + if (dtp == null) + return; + for (val entry : TownColors.entrySet()) + setTownColor(dtp, buttondevteam.chat.components.towncolors.admin.TownColorCommand.getTownNameCased(entry.getKey()), entry.getValue()); + }); + + registerCommand(new TownColorCommand()); + registerCommand(new NationColorCommand()); + registerCommand(new buttondevteam.chat.components.towncolors.admin.TownColorCommand()); + registerCommand(new buttondevteam.chat.components.towncolors.admin.NationColorCommand()); + registerCommand(new TCCount()); + } + + @Override + protected void disable() { + getConfig().getConfig().createSection("towncolors", TownColors.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, + v -> Arrays.stream(v.getValue()).map(Enum::toString).toArray(String[]::new)))); + getConfig().getConfig().createSection("nationcolors", NationColor.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, + v -> v.getValue().toString()))); + } + + /** + * Sets a town's color on Dynmap. + * + * @param dtp A reference for the Dynmap-Towny plugin + * @param town The town's name using the correct casing + * @param colors The town's colors + */ + + public static void setTownColor(DynmapTownyPlugin dtp, String town, Color[] colors) { + Function c2i = c -> c.getRed() << 16 | c.getGreen() << 8 | c.getBlue(); + try { + DTBridge.setTownColor(dtp, town, c2i.apply(colors[0]), + c2i.apply(colors.length > 1 ? colors[1] : colors[0])); + } catch (Exception e) { + TBMCCoreAPI.SendException("Failed to set town color for town " + town + "!", e); + } + } + + private static String getPlayerNickname(Player player, User user, ChatPlayer cp) { + String nickname = user.getNick(true); + if (nickname.contains("~")) //StartsWith doesn't work because of color codes + nickname = nickname.replace("~", ""); //It gets stacked otherwise + String name = ChatColor.stripColor(nickname); //Enforce "town colors" on non-members + val res = TownyComponent.TU.getResidentMap().get(player.getName().toLowerCase()); + if (res == null || !res.hasTown()) + return name; + try { + Color[] clrs = Optional.ofNullable( + TownColors.get(res.getTown().getName().toLowerCase()) + ).orElse(new Color[]{Color.White}); //Use white as default town color + StringBuilder ret = new StringBuilder(); + AtomicInteger prevlen = new AtomicInteger(); + BiFunction anyColoredNamePart = (c, len) -> "§" //Len==0 if last part + + Integer.toHexString(c.ordinal()) // 'Odds' are the last character is chopped off so we make sure to include all chars at the end + + (len == 0 ? name.substring(prevlen.get()) + : name.substring(prevlen.get(), prevlen.addAndGet(len))); + BiFunction coloredNamePart = (len, i) + -> anyColoredNamePart.apply(clrs[i], i + 1 == clrs.length ? 0 : len); + final int len = name.length() / (clrs.length + 1); //The above param is needed because this isn't always passed + Color nc; + /*if(res.getTown().hasNation() + &&(nc=PluginMain.NationColor.get(res.getTown().getNation().getName().toLowerCase()))!=null) + len = name.length() / (clrs.length+1); + else + len = name.length() / clrs.length;*/ + val nclar = cp.NameColorLocations().get(); + int[] ncl = nclar == null ? null : nclar.stream().mapToInt(Integer::intValue).toArray(); + if (ncl != null && (Arrays.stream(ncl).sum() != name.length() || ncl.length != clrs.length + 1)) //+1: Nation color + ncl = null; // Reset if name length changed + //System.out.println("ncl: "+Arrays.toString(ncl)+" - sum: "+Arrays.stream(ncl).sum()+" - name len: "+name.length()); + if (!res.getTown().hasNation() + || (nc = NationColor.get(res.getTown().getNation().getName().toLowerCase())) == null) + nc = Color.White; + ret.append(anyColoredNamePart.apply(nc, ncl == null ? len : ncl[0])); //Make first color the nation color + for (int i = 0; i < clrs.length; i++) + //ret.append(coloredNamePart.apply(ncl == null ? len : (nc==null?ncl[i]:ncl[i+1]), i)); + ret.append(coloredNamePart.apply(ncl == null ? len : ncl[i + 1], i)); + return ret.toString(); + } catch (NotRegisteredException e) { + return nickname; + } + } + + /** + * Checks if the component is enabled + */ + public static void updatePlayerColors(Player player) { //Probably while ingame (/u ncolor) + updatePlayerColors(player, ChatPlayer.getPlayer(player.getUniqueId(), ChatPlayer.class)); + } + + /** + * Checks if the component is enabled + */ + public static void updatePlayerColors(Player player, ChatPlayer cp) { //Probably at join - nop, nicknames + if (!ComponentManager.isEnabled(TownColorComponent.class)) + return; + User user = PluginMain.essentials.getUser(player); + user.setNickname(getPlayerNickname(player, user, cp)); + user.setDisplayNick(); //These won't fire the nick change event + cp.FlairUpdate(); //Update in list + } + + private static void load_old(Consumer loadTC, + Consumer loadNC) { + PluginMain.Instance.getLogger().info("Loading files..."); + try { + File file = new File("TBMC/chatsettings.yml"); + if (file.exists()) { + YamlConfiguration yc = new YamlConfiguration(); + yc.load(file); + ConfigurationSection cs; + if (loadTC != null && (cs = yc.getConfigurationSection("towncolors")) != null) + loadTC.accept(cs); + if (loadNC != null && (cs = yc.getConfigurationSection("nationcolors")) != null) + loadNC.accept(cs); + PluginMain.Instance.getLogger().info("Loaded files!"); + } else + PluginMain.Instance.getLogger().info("No files to load, first run probably."); + } catch (Exception e) { + TBMCCoreAPI.SendException("Error while loading chat files!", e); + } + } +} diff --git a/src/main/java/buttondevteam/chat/listener/TownyListener.java b/src/main/java/buttondevteam/chat/components/towncolors/TownyListener.java similarity index 77% rename from src/main/java/buttondevteam/chat/listener/TownyListener.java rename to src/main/java/buttondevteam/chat/components/towncolors/TownyListener.java index a3ecc3f..6a9c28b 100644 --- a/src/main/java/buttondevteam/chat/listener/TownyListener.java +++ b/src/main/java/buttondevteam/chat/components/towncolors/TownyListener.java @@ -1,4 +1,4 @@ -package buttondevteam.chat.listener; +package buttondevteam.chat.components.towncolors; import buttondevteam.chat.PluginMain; import com.earth2me.essentials.User; @@ -16,21 +16,21 @@ import java.util.Objects; public class TownyListener implements Listener { @EventHandler public void onTownRename(RenameTownEvent event) { - val clrs = PluginMain.TownColors.remove(event.getOldName().toLowerCase()); + val clrs = TownColorComponent.TownColors.remove(event.getOldName().toLowerCase()); if (clrs != null) - PluginMain.TownColors.put(event.getTown().getName().toLowerCase(), clrs); + TownColorComponent.TownColors.put(event.getTown().getName().toLowerCase(), clrs); } @EventHandler //Gets called on town load as well public void onTownJoin(TownAddResidentEvent event) { Player p = Bukkit.getPlayer(event.getResident().getName()); if (p != null) - PlayerJoinLeaveListener.updatePlayerColors(p); + TownColorComponent.updatePlayerColors(p); } public static void updateTownMembers(Town town) { town.getResidents().stream().map(r -> Bukkit.getPlayer(r.getName())) - .filter(Objects::nonNull).forEach(PlayerJoinLeaveListener::updatePlayerColors); + .filter(Objects::nonNull).forEach(TownColorComponent::updatePlayerColors); } @EventHandler @@ -47,7 +47,7 @@ public class TownyListener implements Listener { @EventHandler public void onTownDelete(DeleteTownEvent event) { - PluginMain.TownColors.remove(event.getTownName().toLowerCase()); + TownColorComponent.TownColors.remove(event.getTownName().toLowerCase()); } @EventHandler @@ -61,9 +61,9 @@ public class TownyListener implements Listener { @EventHandler public void onNationRename(RenameNationEvent event) { - val clrs = PluginMain.NationColor.remove(event.getOldName().toLowerCase()); + val clrs = TownColorComponent.NationColor.remove(event.getOldName().toLowerCase()); if (clrs != null) - PluginMain.NationColor.put(event.getNation().getName().toLowerCase(), clrs); + TownColorComponent.NationColor.put(event.getNation().getName().toLowerCase(), clrs); } @EventHandler //Gets called on town load as well @@ -78,7 +78,7 @@ public class TownyListener implements Listener { @EventHandler public void onNationDelete(DeleteNationEvent event) { - PluginMain.NationColor.remove(event.getNationName().toLowerCase()); + TownColorComponent.NationColor.remove(event.getNationName().toLowerCase()); } @EventHandler diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/admin/NationColorCommand.java b/src/main/java/buttondevteam/chat/components/towncolors/admin/NationColorCommand.java similarity index 82% rename from src/main/java/buttondevteam/chat/commands/ucmds/admin/NationColorCommand.java rename to src/main/java/buttondevteam/chat/components/towncolors/admin/NationColorCommand.java index 9c9d4b1..3d7c216 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/admin/NationColorCommand.java +++ b/src/main/java/buttondevteam/chat/components/towncolors/admin/NationColorCommand.java @@ -1,8 +1,10 @@ -package buttondevteam.chat.commands.ucmds.admin; +package buttondevteam.chat.components.towncolors.admin; import buttondevteam.chat.PluginMain; +import buttondevteam.chat.commands.ucmds.admin.AdminCommandBase; +import buttondevteam.chat.components.towncolors.TownColorComponent; +import buttondevteam.chat.components.towncolors.TownyListener; import buttondevteam.chat.components.towny.TownyComponent; -import buttondevteam.chat.listener.TownyListener; import buttondevteam.lib.chat.Color; import com.palmergames.bukkit.towny.object.Nation; import com.palmergames.bukkit.towny.object.Town; @@ -40,14 +42,14 @@ public class NationColorCommand extends AdminCommandBase { val c = TownColorCommand.getColorOrSendError(args[1], sender); if (!c.isPresent()) return true; if (!c.get().getName().equals(Color.White.getName())) { //Default nation color - for (val nc : PluginMain.NationColor.values()) { + for (val nc : TownColorComponent.NationColor.values()) { if (nc.getName().equals(c.get().getName())) { sender.sendMessage("§cAnother nation already uses this color!"); return true; } } } - PluginMain.NationColor.put(args[0].toLowerCase(), c.get()); + TownColorComponent.NationColor.put(args[0].toLowerCase(), c.get()); Bukkit.getScheduler().runTaskAsynchronously(PluginMain.Instance, () -> { for (Town t : nation.getTowns()) TownyListener.updateTownMembers(t); diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/admin/TCCount.java b/src/main/java/buttondevteam/chat/components/towncolors/admin/TCCount.java similarity index 67% rename from src/main/java/buttondevteam/chat/commands/ucmds/admin/TCCount.java rename to src/main/java/buttondevteam/chat/components/towncolors/admin/TCCount.java index 277dff6..a20b26a 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/admin/TCCount.java +++ b/src/main/java/buttondevteam/chat/components/towncolors/admin/TCCount.java @@ -1,5 +1,8 @@ -package buttondevteam.chat.commands.ucmds.admin; +package buttondevteam.chat.components.towncolors.admin; +import buttondevteam.chat.commands.ucmds.admin.AdminCommandBase; +import buttondevteam.chat.components.towncolors.TownColorComponent; +import lombok.val; import org.bukkit.command.CommandSender; public class TCCount extends AdminCommandBase { @@ -20,7 +23,8 @@ public class TCCount extends AdminCommandBase { } catch (NumberFormatException e) { return false; } - buttondevteam.chat.commands.ucmds.TownColorCommand.ColorCount = count; + val comp = (TownColorComponent) getComponent(); + comp.colorCount().set(count); sender.sendMessage("Color count set to " + count); return true; } diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/admin/TownColorCommand.java b/src/main/java/buttondevteam/chat/components/towncolors/admin/TownColorCommand.java similarity index 85% rename from src/main/java/buttondevteam/chat/commands/ucmds/admin/TownColorCommand.java rename to src/main/java/buttondevteam/chat/components/towncolors/admin/TownColorCommand.java index 21b7659..8905d9d 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/admin/TownColorCommand.java +++ b/src/main/java/buttondevteam/chat/components/towncolors/admin/TownColorCommand.java @@ -1,8 +1,10 @@ -package buttondevteam.chat.commands.ucmds.admin; +package buttondevteam.chat.components.towncolors.admin; import buttondevteam.chat.PluginMain; +import buttondevteam.chat.commands.ucmds.admin.AdminCommandBase; +import buttondevteam.chat.components.towncolors.TownColorComponent; +import buttondevteam.chat.components.towncolors.TownyListener; import buttondevteam.chat.components.towny.TownyComponent; -import buttondevteam.chat.listener.TownyListener; import buttondevteam.lib.chat.Color; import com.palmergames.bukkit.towny.object.Town; import lombok.val; @@ -48,16 +50,16 @@ public class TownColorCommand extends AdminCommandBase { return true; clrs[i - 1] = c.get(); } - for (Map.Entry other : PluginMain.TownColors.entrySet()) { + for (Map.Entry other : TownColorComponent.TownColors.entrySet()) { Color nc, tnc; try { - nc = PluginMain.NationColor.get(TownyComponent.TU.getTownsMap().get(other.getKey()).getNation().getName().toLowerCase()); + nc = TownColorComponent.NationColor.get(TownyComponent.TU.getTownsMap().get(other.getKey()).getNation().getName().toLowerCase()); } catch (Exception e) { //Too lazy for lots of null-checks and it may throw exceptions anyways nc = null; } if (nc == null) nc = Color.White; //Default nation color try { - tnc = PluginMain.NationColor.get(targetTown.getNation().getName().toLowerCase()); + tnc = TownColorComponent.NationColor.get(targetTown.getNation().getName().toLowerCase()); } catch (Exception e) { tnc = null; } @@ -75,7 +77,7 @@ public class TownColorCommand extends AdminCommandBase { } } } - PluginMain.TownColors.put(args[0].toLowerCase(), clrs); + TownColorComponent.TownColors.put(args[0].toLowerCase(), clrs); TownyListener.updateTownMembers(targetTown); val dtp = (DynmapTownyPlugin) Bukkit.getPluginManager().getPlugin("Dynmap-Towny"); @@ -84,7 +86,7 @@ public class TownColorCommand extends AdminCommandBase { PluginMain.Instance.getLogger().warning("Dynmap-Towny not found for setting town color!"); return true; } - PluginMain.setTownColor(dtp, targetTown.getName(), clrs); + TownColorComponent.setTownColor(dtp, targetTown.getName(), clrs); sender.sendMessage("§bColor(s) set."); return true; } diff --git a/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java b/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java index 196a8d0..25158c5 100644 --- a/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java +++ b/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java @@ -7,28 +7,19 @@ import buttondevteam.chat.commands.UnlolCommand; import buttondevteam.chat.commands.ucmds.HistoryCommand; import buttondevteam.chat.components.flair.FlairComponent; import buttondevteam.chat.components.flair.FlairStates; -import buttondevteam.chat.components.towny.TownyComponent; +import buttondevteam.chat.components.towncolors.TownColorComponent; import buttondevteam.core.ComponentManager; -import buttondevteam.lib.chat.Color; import buttondevteam.lib.player.TBMCPlayerJoinEvent; import buttondevteam.lib.player.TBMCPlayerLoadEvent; import buttondevteam.lib.player.TBMCPlayerSaveEvent; -import com.earth2me.essentials.User; -import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; -import lombok.val; import org.bukkit.Bukkit; -import org.bukkit.ChatColor; import org.bukkit.GameMode; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerQuitEvent; -import java.util.Arrays; -import java.util.Optional; import java.util.Timer; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.function.BiFunction; public class PlayerJoinLeaveListener implements Listener { @@ -70,7 +61,7 @@ public class PlayerJoinLeaveListener implements Listener { nwithoutformatting = p.getName(); PlayerListener.nicknames.forcePut(nwithoutformatting.toLowerCase(), p.getUniqueId()); - updatePlayerColors(p, cp); //TO!DO: Doesn't have effect - It can help to register the listener + TownColorComponent.updatePlayerColors(p, cp); //TO!DO: Doesn't have effect - It can help to register the listener if (cp.ChatOnly || p.getGameMode().equals(GameMode.SPECTATOR)) { cp.ChatOnly = false; @@ -90,60 +81,4 @@ public class PlayerJoinLeaveListener implements Listener { UnlolCommand.Lastlol.values().removeIf(lld -> lld.getLolowner().equals(event.getPlayer())); } - private static String getPlayerNickname(Player player, User user, ChatPlayer cp) { - String nickname = user.getNick(true); - if (nickname.contains("~")) //StartsWith doesn't work because of color codes - nickname = nickname.replace("~", ""); //It gets stacked otherwise - String name = ChatColor.stripColor(nickname); //Enforce "town colors" on non-members - val res = TownyComponent.TU.getResidentMap().get(player.getName().toLowerCase()); - if (res == null || !res.hasTown()) - return name; - try { - Color[] clrs = Optional.ofNullable( - PluginMain.TownColors.get(res.getTown().getName().toLowerCase()) - ).orElse(new Color[]{Color.White}); //Use white as default town color - StringBuilder ret = new StringBuilder(); - AtomicInteger prevlen = new AtomicInteger(); - BiFunction anyColoredNamePart = (c, len) -> "§" //Len==0 if last part - + Integer.toHexString(c.ordinal()) // 'Odds' are the last character is chopped off so we make sure to include all chars at the end - + (len == 0 ? name.substring(prevlen.get()) - : name.substring(prevlen.get(), prevlen.addAndGet(len))); - BiFunction coloredNamePart = (len, i) - -> anyColoredNamePart.apply(clrs[i], i + 1 == clrs.length ? 0 : len); - final int len = name.length() / (clrs.length + 1); //The above param is needed because this isn't always passed - Color nc; - /*if(res.getTown().hasNation() - &&(nc=PluginMain.NationColor.get(res.getTown().getNation().getName().toLowerCase()))!=null) - len = name.length() / (clrs.length+1); - else - len = name.length() / clrs.length;*/ - val nclar = cp.NameColorLocations().get(); - int[] ncl = nclar == null ? null : nclar.stream().mapToInt(Integer::intValue).toArray(); - if (ncl != null && (Arrays.stream(ncl).sum() != name.length() || ncl.length != clrs.length + 1)) //+1: Nation color - ncl = null; // Reset if name length changed - //System.out.println("ncl: "+Arrays.toString(ncl)+" - sum: "+Arrays.stream(ncl).sum()+" - name len: "+name.length()); - if (!res.getTown().hasNation() - || (nc = PluginMain.NationColor.get(res.getTown().getNation().getName().toLowerCase())) == null) - nc = Color.White; - ret.append(anyColoredNamePart.apply(nc, ncl == null ? len : ncl[0])); //Make first color the nation color - for (int i = 0; i < clrs.length; i++) - //ret.append(coloredNamePart.apply(ncl == null ? len : (nc==null?ncl[i]:ncl[i+1]), i)); - ret.append(coloredNamePart.apply(ncl == null ? len : ncl[i + 1], i)); - return ret.toString(); - } catch (NotRegisteredException e) { - return nickname; - } - } - - public static void updatePlayerColors(Player player) { //Probably while ingame (/u ncolor) - updatePlayerColors(player, ChatPlayer.getPlayer(player.getUniqueId(), ChatPlayer.class)); - } - - @SuppressWarnings("WeakerAccess") - public static void updatePlayerColors(Player player, ChatPlayer cp) { //Probably at join - nop, nicknames - User user = PluginMain.essentials.getUser(player); - user.setNickname(getPlayerNickname(player, user, cp)); - user.setDisplayNick(); //These won't fire the nick change event - cp.FlairUpdate(); //Update in list - } } diff --git a/src/main/java/buttondevteam/chat/listener/PlayerListener.java b/src/main/java/buttondevteam/chat/listener/PlayerListener.java index 42dd4ef..43309f7 100644 --- a/src/main/java/buttondevteam/chat/listener/PlayerListener.java +++ b/src/main/java/buttondevteam/chat/listener/PlayerListener.java @@ -5,6 +5,7 @@ import buttondevteam.chat.ChatProcessing; import buttondevteam.chat.PluginMain; import buttondevteam.chat.commands.ucmds.HistoryCommand; import buttondevteam.chat.components.flair.FlairComponent; +import buttondevteam.chat.components.towncolors.TownColorComponent; import buttondevteam.component.channel.Channel; import buttondevteam.component.channel.ChatChannelRegisterEvent; import buttondevteam.component.channel.ChatRoom; @@ -52,9 +53,6 @@ public class PlayerListener implements Listener { */ public static BiMap nicknames = HashBiMap.create(); - public static String NotificationSound; - public static double NotificationPitch; - public final static String[] LaughStrings = new String[]{"xd", "lel", "lawl", "kek", "lmao", "hue", "hah", "rofl"}; @EventHandler(priority = EventPriority.HIGHEST) @@ -286,7 +284,7 @@ public class PlayerListener implements Listener { nicknames.inverse().forcePut(e.getAffected().getBase().getUniqueId(), ChatColor.stripColor(nick).toLowerCase()); Bukkit.getScheduler().runTaskLater(PluginMain.Instance, () -> { - PlayerJoinLeaveListener.updatePlayerColors(e.getAffected().getBase()); //Won't fire this event again + TownColorComponent.updatePlayerColors(e.getAffected().getBase()); //Won't fire this event again }, 1); } }