From 0ac78c9cb27da25719cf6cc67de7e687c8655804 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sun, 21 Oct 2018 19:45:11 +0200 Subject: [PATCH] Some work on nation colors --- .../commands/ucmds/NationColorCommand.java | 4 + .../ucmds/admin/NationColorCommand.java | 4 + .../ucmds/admin/TownColorCommand.java | 37 +++--- .../chat/listener/TownyListener.java | 113 +++++++++++++----- 4 files changed, 107 insertions(+), 51 deletions(-) create mode 100644 src/main/java/buttondevteam/chat/commands/ucmds/NationColorCommand.java create mode 100644 src/main/java/buttondevteam/chat/commands/ucmds/admin/NationColorCommand.java diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/NationColorCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/NationColorCommand.java new file mode 100644 index 0000000..cf22e9a --- /dev/null +++ b/src/main/java/buttondevteam/chat/commands/ucmds/NationColorCommand.java @@ -0,0 +1,4 @@ +package buttondevteam.chat.commands.ucmds; + +public class NationColorCommand { +} diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/admin/NationColorCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/admin/NationColorCommand.java new file mode 100644 index 0000000..19d0129 --- /dev/null +++ b/src/main/java/buttondevteam/chat/commands/ucmds/admin/NationColorCommand.java @@ -0,0 +1,4 @@ +package buttondevteam.chat.commands.ucmds.admin; + +public class NationColorCommand { +} 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 38c56d8..3fee0f7 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/admin/TownColorCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/admin/TownColorCommand.java @@ -1,7 +1,7 @@ package buttondevteam.chat.commands.ucmds.admin; import buttondevteam.chat.PluginMain; -import buttondevteam.chat.listener.PlayerJoinLeaveListener; +import buttondevteam.chat.listener.TownyListener; import buttondevteam.lib.chat.Color; import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; import com.palmergames.bukkit.towny.object.Town; @@ -40,6 +40,7 @@ public class TownColorCommand extends AdminCommandBase { return true; } Color[] clrs = null; //Add nation color as well + boolean hasNationColor = false; Town targetTown = PluginMain.TU.getTownsMap().get(args[0].toLowerCase()); try { Color c; //TODO: Add command for nation color @@ -47,30 +48,20 @@ public class TownColorCommand extends AdminCommandBase { && (c = PluginMain.NationColor.get(targetTown.getNation().getName().toLowerCase())) != null) { clrs = new Color[args.length]; clrs[0] = c; + hasNationColor = true; } } catch (NotRegisteredException ignored) { } - if (clrs == null) + if (!hasNationColor) clrs = new Color[args.length - 1]; for (int i = 1; i < args.length; i++) { - val c = getColor(args[i]); - if (!c.isPresent()) { //^^ Skip black - sender.sendMessage("§cThe color '" + args[i] + "' cannot be found."); //ˇˇ Skip black - sender.sendMessage("§cAvailable colors: " + Arrays.stream(Color.values()).skip(1).map(col -> String.format("§%x%s§r", col.ordinal(), col.getName())).collect(Collectors.joining(", "))); - sender.sendMessage("§cMake sure to type them exactly as shown above."); + val c = getColorOrSendError(args[i], sender); + if (!c.isPresent()) return true; - } - clrs[i - 1] = c.get(); + clrs[hasNationColor ? i : i - 1] = c.get(); } PluginMain.TownColors.put(args[0].toLowerCase(), clrs); - Bukkit.getOnlinePlayers().forEach(p -> { - try { - Town t = PluginMain.TU.getResidentMap().get(p.getName().toLowerCase()).getTown(); - if (t != null && t.getName().equalsIgnoreCase(args[0])) - PlayerJoinLeaveListener.updatePlayerColors(p); - } catch (NotRegisteredException ignored) { - } - }); + TownyListener.updateTownMembers(targetTown); val dtp = (DynmapTownyPlugin) Bukkit.getPluginManager().getPlugin("Dynmap-Towny"); if (dtp == null) { @@ -78,13 +69,19 @@ public class TownColorCommand extends AdminCommandBase { PluginMain.Instance.getLogger().warning("Dynmap-Towny not found for setting town color!"); return true; } - PluginMain.setTownColor(dtp, getTownNameCased(args[0]), clrs); + PluginMain.setTownColor(dtp, targetTown.getName(), clrs); sender.sendMessage("§bColor(s) set."); return true; } - private static Optional getColor(String name) { - return Arrays.stream(Color.values()).skip(1).filter(cc -> cc.getName().equalsIgnoreCase(name)).findAny(); + private static Optional getColorOrSendError(String name, CommandSender sender) { + val c = Arrays.stream(Color.values()).skip(1).filter(cc -> cc.getName().equalsIgnoreCase(name)).findAny(); + if (!c.isPresent()) { //^^ Skip black + sender.sendMessage("§cThe color '" + name + "' cannot be found."); //ˇˇ Skip black + sender.sendMessage("§cAvailable colors: " + Arrays.stream(Color.values()).skip(1).map(col -> String.format("§%x%s§r", col.ordinal(), col.getName())).collect(Collectors.joining(", "))); + sender.sendMessage("§cMake sure to type them exactly as shown above."); + } + return c; } public static String getTownNameCased(String name) { diff --git a/src/main/java/buttondevteam/chat/listener/TownyListener.java b/src/main/java/buttondevteam/chat/listener/TownyListener.java index 54a3dbf..2e83c68 100644 --- a/src/main/java/buttondevteam/chat/listener/TownyListener.java +++ b/src/main/java/buttondevteam/chat/listener/TownyListener.java @@ -1,6 +1,7 @@ package buttondevteam.chat.listener; import buttondevteam.chat.PluginMain; +import buttondevteam.chat.commands.ucmds.admin.TownColorCommand; import com.earth2me.essentials.User; import com.palmergames.bukkit.towny.event.*; import com.palmergames.bukkit.towny.object.Town; @@ -14,19 +15,19 @@ import org.bukkit.event.Listener; import java.util.Objects; public class TownyListener implements Listener { - @EventHandler - public void onTownRename(RenameTownEvent event) { - val clrs = PluginMain.TownColors.remove(event.getOldName().toLowerCase()); - if (clrs != null) - PluginMain.TownColors.put(event.getTown().getName().toLowerCase(), clrs); - } + @EventHandler + public void onTownRename(RenameTownEvent event) { + val clrs = PluginMain.TownColors.remove(event.getOldName().toLowerCase()); + if (clrs != null) + PluginMain.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); - } + @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); + } @EventHandler //Gets called on town load as well public void onNationJoin(NationAddTownEvent event) { @@ -38,29 +39,79 @@ public class TownyListener implements Listener { updateTownMembers(event.getTown()); } - private void updateTownMembers(Town town) { //TODO: Update (or remove) nation color from town color + public static void updateTownMembers(Town town) { //TODO: Update (or remove) nation color from town color town.getResidents().stream().map(r -> Bukkit.getPlayer(r.getName())) .filter(Objects::nonNull).forEach(PlayerJoinLeaveListener::updatePlayerColors); } - @EventHandler - public void onTownLeave(TownRemoveResidentEvent event) { - Player p = Bukkit.getPlayer(event.getResident().getName()); - if (p != null) { - User user = PluginMain.essentials.getUser(p); - user.setNickname(ChatColor.stripColor(user.getNick(true).replace("~", ""))); - } - } + @EventHandler + public void onTownLeave(TownRemoveResidentEvent event) { + Player p = Bukkit.getPlayer(event.getResident().getName()); + if (p != null) { + User user = PluginMain.essentials.getUser(p); + user.setNickname(ChatColor.stripColor(user.getNick(true).replace("~", ""))); + } + } - @EventHandler - public void onTownDelete(DeleteTownEvent event) { - PluginMain.TownColors.remove(event.getTownName().toLowerCase()); - } + @EventHandler + public void onTownDelete(DeleteTownEvent event) { + PluginMain.TownColors.remove(event.getTownName().toLowerCase()); + } - @EventHandler - public void onTownCreate(NewTownEvent event) { - Player p = Bukkit.getPlayer(event.getTown().getMayor().getName()); - if (p != null) - p.sendMessage("§6Use /u towncolor to set a color for the town."); - } + @EventHandler + public void onTownCreate(NewTownEvent event) { + Player p = Bukkit.getPlayer(event.getTown().getMayor().getName()); + if (p != null) + p.sendMessage("§6Use /u towncolor to set a color for the town."); + } + + //----------------------------------------------------------------------------- + + @EventHandler + public void onNationRename(RenameNationEvent event) { + val clrs = PluginMain.NationColor.remove(event.getOldName().toLowerCase()); + if (clrs != null) + PluginMain.NationColor.put(event.getNation().getName().toLowerCase(), clrs); + } + + @EventHandler //Gets called on town load as well + public void onNationJoin(NationAddTownEvent event) { + TownColorCommand.SetTownColor() + } + + @EventHandler //Gets called on town load as well + public void onNationJoin(NationAddTownEvent event) { + updateTownMembers(event.getTown()); + } + + @EventHandler //Gets called on town load as well + public void onNationLeave(NationRemoveTownEvent event) { + updateTownMembers(event.getTown()); + } + + private void updateTownMembers(Town town) { + town.getResidents().stream().map(r -> Bukkit.getPlayer(r.getName())) + .filter(Objects::nonNull).forEach(PlayerJoinLeaveListener::updatePlayerColors); + } + + @EventHandler + public void onTownLeave(TownRemoveResidentEvent event) { + Player p = Bukkit.getPlayer(event.getResident().getName()); + if (p != null) { + User user = PluginMain.essentials.getUser(p); + user.setNickname(ChatColor.stripColor(user.getNick(true).replace("~", ""))); + } + } + + @EventHandler + public void onTownDelete(DeleteTownEvent event) { + PluginMain.TownColors.remove(event.getTownName().toLowerCase()); + } + + @EventHandler + public void onTownCreate(NewTownEvent event) { + Player p = Bukkit.getPlayer(event.getTown().getMayor().getName()); + if (p != null) + p.sendMessage("§6Use /u towncolor to set a color for the town."); + } }