From 68de2f36cfa9040d7c9a4fa7235eb20d9c74dfba Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 21 May 2018 03:55:41 +0200 Subject: [PATCH] Fixed respect, town colors etc. Fixed respect display to 2 decimals (Fixed a "critical" bug in... 25 days) #73 Fixed town colors hopefully (#74) Fixed /u ncolor requiring the ~ Added support for colons for ncolor Yesterday I already woke up by this time and I'm still not sleeping now --- .../java/buttondevteam/chat/PluginMain.java | 32 ++++++++++++------- .../chat/commands/FTopCommand.java | 12 +++++-- .../chat/commands/ucmds/NColorCommand.java | 15 +++++---- .../ucmds/admin/TownColorCommand.java | 6 +++- .../chat/listener/PlayerListener.java | 4 +-- src/main/java/org/dynmap/towny/DTBridge.java | 17 +++++----- 6 files changed, 54 insertions(+), 32 deletions(-) diff --git a/src/main/java/buttondevteam/chat/PluginMain.java b/src/main/java/buttondevteam/chat/PluginMain.java index cb9315c..0d4a4e3 100644 --- a/src/main/java/buttondevteam/chat/PluginMain.java +++ b/src/main/java/buttondevteam/chat/PluginMain.java @@ -106,7 +106,7 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. if (dtp == null) return; for (val entry : TownColors.entrySet()) - setTownColor(dtp, entry.getKey(), entry.getValue()); + setTownColor(dtp, buttondevteam.chat.commands.ucmds.admin.TownColorCommand.getTownNameCased(entry.getKey()), entry.getValue()); }); if (!setupEconomy() || !setupPermissions()) @@ -116,15 +116,22 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. new Thread(new AnnouncerThread()).start(); } - 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); - } - } + /** + * 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 Boolean stop = false; public static Essentials essentials = null; @@ -272,7 +279,10 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. public static ArrayList AnnounceMessages = new ArrayList<>(); public static int AnnounceTime = 15 * 60 * 1000; - public static Map TownColors = new HashMap<>(); + /** + * Names lowercased + */ + public static Map TownColors = new HashMap<>(); @SuppressWarnings("unchecked") public static void LoadFiles() { diff --git a/src/main/java/buttondevteam/chat/commands/FTopCommand.java b/src/main/java/buttondevteam/chat/commands/FTopCommand.java index e3685a8..8c10178 100644 --- a/src/main/java/buttondevteam/chat/commands/FTopCommand.java +++ b/src/main/java/buttondevteam/chat/commands/FTopCommand.java @@ -36,8 +36,16 @@ public class FTopCommand extends TBMCCommandBase { Bukkit.getScheduler().runTaskAsynchronously(PluginMain.Instance, () -> { if (cached == null || lastcache < System.nanoTime() - 60000000000L) { // 1m - (no guarantees of nanoTime's relation to 0, so we need the null check too) cached = Arrays.stream(Objects.requireNonNull(playerdir.listFiles())).sequential() - .map(f -> TBMCPlayerBase.getPlayer( - UUID.fromString(f.getName().substring(0, f.getName().length() - 4)), ChatPlayer.class)) + .filter(f -> f.getName().length() > 4) + .map(f -> { + try { + return TBMCPlayerBase.getPlayer( + UUID.fromString(f.getName().substring(0, f.getName().length() - 4)), ChatPlayer.class); + } catch (Exception e) { + return null; + } + }) + .filter(Objects::nonNull) .sorted((cp1, cp2) -> Double.compare(cp2.getF(), cp1.getF())) .toArray(ChatPlayer[]::new); // TODO: Properly implement getting all players lastcache = System.nanoTime(); diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/NColorCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/NColorCommand.java index 79432c6..1fa5a1d 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/NColorCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/NColorCommand.java @@ -45,28 +45,29 @@ public class NColorCommand extends UCommandBase { } if (args.length == 0) return false; - String arg = player.getDisplayName().startsWith("~") ? "~" + args[0] : args[0]; //Add ~ for nicknames - if (!args[0].replace("|", "").equalsIgnoreCase(ChatColor.stripColor(player.getDisplayName()))) { + final String name = ChatColor.stripColor(player.getDisplayName()); + String arg = name.startsWith("~") ? "~" + args[0] : args[0]; //Add ~ for nicknames + if (!arg.replace("|", "").replace(":", "").equalsIgnoreCase(name)) { player.sendMessage("§cThe name you gave doesn't match your name. Make sure to use " - + ChatColor.stripColor(player.getDisplayName()) + "§c with added vertical lines (|)."); + + name + "§c with added vertical lines (|) or colons (:)."); return true; } - String[] nameparts = args[0].split("\\|"); + String[] nameparts = arg.split("\\|"); Color[] towncolors = PluginMain.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; } if (nameparts.length < towncolors.length) { - player.sendMessage("§cYou need more vertical lines (|) in your name. (Should have " + (towncolors.length - 1) + ")"); + player.sendMessage("§cYou need more vertical lines (|) or colons (:) in your name. (Should have " + (towncolors.length - 1) + ")"); return true; } if (nameparts.length > towncolors.length * 2) { - player.sendMessage("§cYou have waay too many vertical lines (|) in your name. (Should have " + (towncolors.length - 1) + ")"); + player.sendMessage("§cYou have waay too many vertical lines (|) or colons (:) in your name. (Should have " + (towncolors.length - 1) + ")"); return true; } if (nameparts.length > towncolors.length) { - player.sendMessage("§cYou have too many vertical lines (|) in your name. (Should have " + (towncolors.length - 1) + ")"); + player.sendMessage("§cYou have too many vertical lines (|) or colons (:) in your name. (Should have " + (towncolors.length - 1) + ")"); return true; } ChatPlayer.getPlayer(player.getUniqueId(), ChatPlayer.class).NameColorLocations() diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/admin/TownColorCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/admin/TownColorCommand.java index c45a95e..72f92dc 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/admin/TownColorCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/admin/TownColorCommand.java @@ -67,8 +67,12 @@ public class TownColorCommand extends AdminCommandBase { PluginMain.Instance.getLogger().warning("Dynmap-Towny not found for setting town color!"); return true; } - PluginMain.setTownColor(dtp, args[0].toLowerCase(), clrs); + PluginMain.setTownColor(dtp, getTownNameCased(args[0]), clrs); sender.sendMessage("§bColor(s) set."); return true; } + + public static String getTownNameCased(String name) { + return PluginMain.TU.getTownsMap().get(name.toLowerCase()).getName(); + } } diff --git a/src/main/java/buttondevteam/chat/listener/PlayerListener.java b/src/main/java/buttondevteam/chat/listener/PlayerListener.java index db2c10f..7b4c66e 100644 --- a/src/main/java/buttondevteam/chat/listener/PlayerListener.java +++ b/src/main/java/buttondevteam/chat/listener/PlayerListener.java @@ -53,7 +53,7 @@ public class PlayerListener implements Listener { public static boolean ShowRPTag = false; - public final static String[] LaughStrings = new String[] { "xd", "lel", "lawl", "kek", "lmao", "hue", "hah" }; + public final static String[] LaughStrings = new String[]{"xd", "lel", "lawl", "kek", "lmao", "hue", "hah", "rofl"}; @EventHandler(priority = EventPriority.HIGHEST) public void onPlayerChat(AsyncPlayerChatEvent event) { @@ -262,7 +262,7 @@ public class PlayerListener implements Listener { final String flair = cp.GetFormattedFlair(e.getTarget() != InfoTarget.MCCommand); if (flair.length() > 0) e.addInfo("/r/TheButton flair: " + flair); - e.addInfo("Respect: " + cp.getF()); + e.addInfo(String.format("Respect: %.2f", cp.getF())); } catch (Exception ex) { TBMCCoreAPI.SendException("Error while providing chat info for player " + e.getPlayer().getFileName(), ex); } diff --git a/src/main/java/org/dynmap/towny/DTBridge.java b/src/main/java/org/dynmap/towny/DTBridge.java index a78c39e..ee427b0 100644 --- a/src/main/java/org/dynmap/towny/DTBridge.java +++ b/src/main/java/org/dynmap/towny/DTBridge.java @@ -1,16 +1,15 @@ package org.dynmap.towny; -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.util.Map; - +import lombok.val; import org.bukkit.Bukkit; import org.bukkit.configuration.file.FileConfiguration; import org.dynmap.bukkit.DynmapPlugin; import org.dynmap.markers.MarkerAPI; -import lombok.val; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.util.Map; public class DTBridge { /** @@ -19,7 +18,7 @@ public class DTBridge { * @param dtp * The Dynmap-Towny plugin * @param townname - * The name of the town + * The name of the town, using correct casing * @param strokecolor * The stroke color in RGB format * @param fillcolor @@ -32,14 +31,14 @@ public class DTBridge { IllegalAccessException, NoSuchMethodException, InstantiationException, InvocationTargetException { Class cl = Class.forName(DynmapTownyPlugin.class.getName() + "$AreaStyle"); Field field = DynmapTownyPlugin.class.getDeclaredField("cusstyle"); - field.setAccessible(true); // DOesn't allow accessing it from the same package, if it's from a different plugin + field.setAccessible(true); // Doesn't allow accessing it from the same package, if it's from a different plugin @SuppressWarnings("unchecked") val map = (Map) field.get(dtp); Object style = map.get(townname); if (style == null) { Constructor c = cl.getDeclaredConstructor(FileConfiguration.class, String.class, MarkerAPI.class); c.setAccessible(true); - style = c.newInstance(dtp.getConfig(), "custstyle" + townname, + style = c.newInstance(dtp.getConfig(), "custstyle." + townname, ((DynmapPlugin) Bukkit.getPluginManager().getPlugin("dynmap")).getMarkerAPI()); map.put(townname, style); }