From 83c99df895c8e94a732b05be5e156c22bafbff90 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Thu, 11 Oct 2018 13:15:03 +0200 Subject: [PATCH 1/8] Adding support for nation color (WIP) Also various fixes Got rid of a bunch of warnings --- pom.xml | 2 +- .../java/buttondevteam/chat/PluginMain.java | 44 ++++++++++++------- .../chat/commands/ucmds/TownColorCommand.java | 13 +++--- .../ucmds/admin/TownColorCommand.java | 29 +++++++++--- .../listener/PlayerJoinLeaveListener.java | 43 +++++------------- .../chat/listener/TownyListener.java | 20 ++++++++- 6 files changed, 85 insertions(+), 66 deletions(-) diff --git a/pom.xml b/pom.xml index 024cb81..8056f5b 100644 --- a/pom.xml +++ b/pom.xml @@ -163,7 +163,7 @@ com.github.TBMCPlugins.ButtonCore Towny - master-SNAPSHOT + 8d3b6b6 com.vexsoftware diff --git a/src/main/java/buttondevteam/chat/PluginMain.java b/src/main/java/buttondevteam/chat/PluginMain.java index b082706..d7b7ad1 100644 --- a/src/main/java/buttondevteam/chat/PluginMain.java +++ b/src/main/java/buttondevteam/chat/PluginMain.java @@ -40,7 +40,6 @@ import org.htmlcleaner.TagNode; import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.net.UnknownHostException; @@ -54,16 +53,16 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. // https://www.reddit.com/r/thebutton/comments/31c32v/i_pressed_the_button_without_really_thinking/ public static PluginMain Instance; public static ConsoleCommandSender Console; - public final static String FlairThreadURL = "https://www.reddit.com/r/Chromagamers/comments/51ys94/flair_thread_for_the_mc_server/"; + private final static String FlairThreadURL = "https://www.reddit.com/r/Chromagamers/comments/51ys94/flair_thread_for_the_mc_server/"; public static Scoreboard SB; public static TownyUniverse TU; - public static ArrayList Towns; - public static ArrayList Nations; + private static ArrayList Towns; + private static ArrayList Nations; public static Channel TownChat; public static Channel NationChat; - public static Channel RPChannel; + private static Channel RPChannel; //TODO: Move to ButtonCore - or use the ch filter in the Discord plugin /** *

@@ -88,10 +87,11 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. SB = getServer().getScoreboardManager().getMainScoreboard(); // Main can be detected with @a[score_...] TU = ((Towny) Bukkit.getPluginManager().getPlugin("Towny")).getTownyUniverse(); - Towns = new ArrayList(TU.getTownsMap().values()); // Creates a snapshot of towns, new towns will be added when needed - Nations = new ArrayList(TU.getNationsMap().values()); // Same here but with nations + Towns = new ArrayList<>(TU.getTownsMap().values()); // Creates a snapshot of towns, new towns will be added when needed + Nations = new ArrayList<>(TU.getNationsMap().values()); // Same here but with nations - TownColors.keySet().removeIf(t -> !TU.getTownsMap().containsKey(t.toLowerCase())); // Removes town colors for deleted/renamed towns + TownColors.keySet().removeIf(t -> !TU.getTownsMap().containsKey(t)); // Removes town colors for deleted/renamed towns + NationColor.keySet().removeIf(n -> !TU.getNationsMap().containsKey(n)); // Removes nation colors for deleted/renamed nations TBMCChatAPI.RegisterChatChannel( TownChat = new Channel("§3TC§f", Color.DarkAqua, "tc", s -> checkTownNationChat(s, false))); @@ -201,7 +201,7 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. } } - public void DownloadFlair(ChatPlayer mp) throws MalformedURLException, IOException { + public void DownloadFlair(ChatPlayer mp) throws IOException { String[] flairdata = TBMCCoreAPI .DownloadString("http://karmadecay.com/thebutton-data.php?users=" + mp.UserName().get()) .replace("\"", "").split(":"); @@ -247,11 +247,11 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. p.SetFlair(Short.parseShort(text)); } - public static boolean CheckForJoinDate(ChatPlayer mp) throws Exception { + private static boolean CheckForJoinDate(ChatPlayer mp) throws Exception { return JoinedBefore(mp, 2015, 4, 1); } - public static boolean JoinedBefore(ChatPlayer mp, int year, int month, int day) throws Exception { + private static boolean JoinedBefore(ChatPlayer mp, int year, int month, int day) throws Exception { URL url = new URL("https://www.reddit.com/u/" + mp.UserName()); URLConnection con = url.openConnection(); con.setRequestProperty("User-Agent", "TheButtonAutoFlair"); @@ -281,13 +281,17 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. public static ArrayList AnnounceMessages = new ArrayList<>(); public static int AnnounceTime = 15 * 60 * 1000; - /** - * Names lowercased - */ - public static Map TownColors = new HashMap<>(); + /** + * 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") - public static void LoadFiles() { + private static void LoadFiles() { PluginMain.Instance.getLogger().info("Loading files..."); try { File file = new File("TBMC/chatsettings.yml"); @@ -305,6 +309,10 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. .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."); @@ -323,9 +331,11 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. yc.set("announcetime", AnnounceTime); yc.set("announcements", AnnounceMessages); yc.set("alphadeaths", PlayerListener.AlphaDeaths); - yc.createSection("towncolors", TownColors.entrySet().stream().collect(Collectors.toMap(k -> k.getKey(), + 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) { diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/TownColorCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/TownColorCommand.java index 7d0d9d8..197a574 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/TownColorCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/TownColorCommand.java @@ -1,22 +1,21 @@ package buttondevteam.chat.commands.ucmds; -import org.bukkit.entity.Player; -import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; -import com.palmergames.bukkit.towny.object.Resident; - import buttondevteam.chat.PluginMain; 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 org.bukkit.entity.Player; @CommandClass // TODO: /u u when annotation not present @OptionallyPlayerCommandClass(playerOnly = true) public class TownColorCommand extends UCommandBase { @Override public String GetHelpText(String alias)[] { - String cns = " "; + StringBuilder cns = new StringBuilder(" "); for (int i = 2; i <= ColorCount; i++) - cns += " [colorname" + i + "]"; + cns.append(" [colorname").append(i).append("]"); return new String[] { // "§6---- Town Color ----", // "This command allows setting a color for a town.", // @@ -47,7 +46,7 @@ public class TownColorCommand extends UCommandBase { String[] a = new String[args.length + 1]; System.arraycopy(args, 0, a, 1, args.length); try { - a[0] = res.getTown().getName().toLowerCase(); + a[0] = res.getTown().getName(); } catch (NotRegisteredException e) { TBMCCoreAPI.SendException("Failed to set town color for player " + player + "!", e); player.sendMessage("§cCouldn't find your town... Error reported."); 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 72f92dc..38c56d8 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/admin/TownColorCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/admin/TownColorCommand.java @@ -11,6 +11,7 @@ import org.bukkit.command.CommandSender; import org.dynmap.towny.DynmapTownyPlugin; import java.util.Arrays; +import java.util.Optional; import java.util.stream.Collectors; public class TownColorCommand extends AdminCommandBase { @@ -38,10 +39,21 @@ public class TownColorCommand extends AdminCommandBase { sender.sendMessage("§cThe town '" + args[0] + "' cannot be found."); return true; } - val clrs = new Color[args.length - 1]; - for (int i = 1; i < args.length; i++) { - val ii = i; - val c = Arrays.stream(Color.values()).skip(1).filter(cc -> cc.getName().equalsIgnoreCase(args[ii])).findAny(); + Color[] clrs = null; //Add nation color as well + Town targetTown = PluginMain.TU.getTownsMap().get(args[0].toLowerCase()); + try { + Color c; //TODO: Add command for nation color + if (targetTown.getNation() != null + && (c = PluginMain.NationColor.get(targetTown.getNation().getName().toLowerCase())) != null) { + clrs = new Color[args.length]; + clrs[0] = c; + } + } catch (NotRegisteredException ignored) { + } + if (clrs == null) + 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(", "))); @@ -51,13 +63,12 @@ public class TownColorCommand extends AdminCommandBase { clrs[i - 1] = c.get(); } PluginMain.TownColors.put(args[0].toLowerCase(), clrs); - //PluginMain.TU.getTownsMap().get(args[0].toLowerCase()).getResidents().forEach(r->{ 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 e) { + } catch (NotRegisteredException ignored) { } }); @@ -72,7 +83,11 @@ public class TownColorCommand extends AdminCommandBase { return true; } - public static String getTownNameCased(String name) { + private static Optional getColor(String name) { + return Arrays.stream(Color.values()).skip(1).filter(cc -> cc.getName().equalsIgnoreCase(name)).findAny(); + } + + public static String getTownNameCased(String name) { return PluginMain.TU.getTownsMap().get(name.toLowerCase()).getName(); } } diff --git a/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java b/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java index 4d5d940..91f5fa2 100644 --- a/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java +++ b/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java @@ -51,31 +51,7 @@ public class PlayerJoinLeaveListener implements Listener { }; tt.mp = cp; timer.schedule(tt, 1000); - } else { - /*Timer timer = new Timer(); - PlayerJoinTimerTask tt = new PlayerJoinTimerTask() { - - @Override - public void run() { - Player player = Bukkit.getPlayer(mp.PlayerName().get()); - if (player == null) - return; - - if (mp.FlairState().get().equals(FlairStates.NoComment)) { - String json = String.format( - "[\"\",{\"text\":\"If you're from Reddit and you'd like your /r/TheButton flair displayed ingame, write your Minecraft name to \",\"color\":\"aqua\"},{\"text\":\"[this thread].\",\"color\":\"aqua\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"%s\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"Click here to go to the Reddit thread\",\"color\":\"aqua\"}]}}}]", - PluginMain.FlairThreadURL); - PluginMain.Instance.getServer().dispatchCommand(PluginMain.Console, - "tellraw " + mp.PlayerName() + " " + json); - json = "[\"\",{\"text\":\"If you aren't from Reddit or don't want the flair, type /u ignore to prevent this message after next login.\",\"color\":\"aqua\"}]"; - PluginMain.Instance.getServer().dispatchCommand(PluginMain.Console, - "tellraw " + mp.PlayerName() + " " + json); - } - } - }; - tt.mp = cp; - timer.schedule(tt, 15 * 1000);*/ //TODO: Better Reddit integration (OAuth) - } + } //TODO: Better Reddit integration (OAuth) String nwithoutformatting = PluginMain.essentials.getUser(p).getNickname(); @@ -109,7 +85,7 @@ public class PlayerJoinLeaveListener implements Listener { UnlolCommand.Lastlol.values().removeIf(lld -> lld.getLolowner().equals(event.getPlayer())); } - private static String getPlayerNickname(Player player, User user) { + 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 @@ -128,7 +104,7 @@ public class PlayerJoinLeaveListener implements Listener { + (i + 1 == clrs.length ? name.substring(prevlen.get()) : name.substring(prevlen.get(), prevlen.addAndGet(len))); int len = name.length() / clrs.length; - val nclar = ChatPlayer.getPlayer(player.getUniqueId(), ChatPlayer.class).NameColorLocations().get(); + 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)) ncl = null; // Reset if name length changed @@ -145,10 +121,11 @@ public class PlayerJoinLeaveListener implements Listener { updatePlayerColors(player, ChatPlayer.getPlayer(player.getUniqueId(), ChatPlayer.class)); } - public static void updatePlayerColors(Player player, ChatPlayer cp) { //Probably at join - nop, nicknames - User user = PluginMain.essentials.getUser(player); - user.setNickname(getPlayerNickname(player, user)); - user.setDisplayNick(); //These won't fire the nick change event - cp.FlairUpdate(); //Update in list - } + @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/TownyListener.java b/src/main/java/buttondevteam/chat/listener/TownyListener.java index 56309f4..54a3dbf 100644 --- a/src/main/java/buttondevteam/chat/listener/TownyListener.java +++ b/src/main/java/buttondevteam/chat/listener/TownyListener.java @@ -3,6 +3,7 @@ package buttondevteam.chat.listener; import buttondevteam.chat.PluginMain; import com.earth2me.essentials.User; import com.palmergames.bukkit.towny.event.*; +import com.palmergames.bukkit.towny.object.Town; import lombok.val; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -10,6 +11,8 @@ import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; +import java.util.Objects; + public class TownyListener implements Listener { @EventHandler public void onTownRename(RenameTownEvent event) { @@ -25,6 +28,21 @@ public class TownyListener implements Listener { PlayerJoinLeaveListener.updatePlayerColors(p); } + @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) { //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()); @@ -43,6 +61,6 @@ public class TownyListener implements Listener { public void onTownCreate(NewTownEvent event) { Player p = Bukkit.getPlayer(event.getTown().getMayor().getName()); if (p != null) - p.sendMessage("§6Use /u towncolor [color2] to set a color for the town."); + p.sendMessage("§6Use /u towncolor to set a color for the town."); } } -- 2.30.2 From 0ac78c9cb27da25719cf6cc67de7e687c8655804 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sun, 21 Oct 2018 19:45:11 +0200 Subject: [PATCH 2/8] 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."); + } } -- 2.30.2 From 775789a45129e13299c2b13f064bafeab8fa269a Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 22 Oct 2018 00:53:52 +0200 Subject: [PATCH 3/8] More work on nation colors Needs testing --- .../ucmds/admin/TownColorCommand.java | 18 +----- .../listener/PlayerJoinLeaveListener.java | 32 ++++++++--- .../chat/listener/TownyListener.java | 57 +++++-------------- 3 files changed, 40 insertions(+), 67 deletions(-) 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 3fee0f7..e718b6f 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/admin/TownColorCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/admin/TownColorCommand.java @@ -3,7 +3,6 @@ package buttondevteam.chat.commands.ucmds.admin; import buttondevteam.chat.PluginMain; import buttondevteam.chat.listener.TownyListener; import buttondevteam.lib.chat.Color; -import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; import com.palmergames.bukkit.towny.object.Town; import lombok.val; import org.bukkit.Bukkit; @@ -39,26 +38,13 @@ public class TownColorCommand extends AdminCommandBase { sender.sendMessage("§cThe town '" + args[0] + "' cannot be found."); return true; } - Color[] clrs = null; //Add nation color as well - boolean hasNationColor = false; + Color[] clrs = new Color[args.length - 1]; Town targetTown = PluginMain.TU.getTownsMap().get(args[0].toLowerCase()); - try { - Color c; //TODO: Add command for nation color - if (targetTown.getNation() != null - && (c = PluginMain.NationColor.get(targetTown.getNation().getName().toLowerCase())) != null) { - clrs = new Color[args.length]; - clrs[0] = c; - hasNationColor = true; - } - } catch (NotRegisteredException ignored) { - } - if (!hasNationColor) - clrs = new Color[args.length - 1]; for (int i = 1; i < args.length; i++) { val c = getColorOrSendError(args[i], sender); if (!c.isPresent()) return true; - clrs[hasNationColor ? i : i - 1] = c.get(); + clrs[i - 1] = c.get(); } PluginMain.TownColors.put(args[0].toLowerCase(), clrs); TownyListener.updateTownMembers(targetTown); diff --git a/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java b/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java index 91f5fa2..0038a98 100644 --- a/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java +++ b/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java @@ -5,6 +5,7 @@ import buttondevteam.chat.FlairStates; import buttondevteam.chat.PlayerJoinTimerTask; import buttondevteam.chat.PluginMain; import buttondevteam.chat.commands.UnlolCommand; +import buttondevteam.lib.chat.Color; import buttondevteam.lib.player.TBMCPlayerJoinEvent; import buttondevteam.lib.player.TBMCPlayerLoadEvent; import buttondevteam.lib.player.TBMCPlayerSaveEvent; @@ -89,28 +90,41 @@ public class PlayerJoinLeaveListener implements Listener { 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 = PluginMain.TU.getResidentMap().get(player.getName().toLowerCase()); if (res == null || !res.hasTown()) - return nickname; + return name; try { val clrs = PluginMain.TownColors.get(res.getTown().getName().toLowerCase()); if (clrs == null) - return nickname; + return name; StringBuilder ret = new StringBuilder(); - String name = ChatColor.stripColor(nickname); AtomicInteger prevlen = new AtomicInteger(); - BiFunction coloredNamePart = (len, i) -> "§" - + Integer.toHexString(clrs[i].ordinal()) // 'Odds' are the last character is chopped off so we make sure to include all chars at the end - + (i + 1 == clrs.length ? name.substring(prevlen.get()) - : name.substring(prevlen.get(), prevlen.addAndGet(len))); - int len = name.length() / clrs.length; + 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)) 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 : ncl[i], 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; diff --git a/src/main/java/buttondevteam/chat/listener/TownyListener.java b/src/main/java/buttondevteam/chat/listener/TownyListener.java index 2e83c68..a3ecc3f 100644 --- a/src/main/java/buttondevteam/chat/listener/TownyListener.java +++ b/src/main/java/buttondevteam/chat/listener/TownyListener.java @@ -1,7 +1,6 @@ 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; @@ -29,17 +28,7 @@ public class TownyListener implements Listener { PlayerJoinLeaveListener.updatePlayerColors(p); } - @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()); - } - - public static void updateTownMembers(Town town) { //TODO: Update (or remove) nation color from town color + public static void updateTownMembers(Town town) { town.getResidents().stream().map(r -> Bukkit.getPlayer(r.getName())) .filter(Objects::nonNull).forEach(PlayerJoinLeaveListener::updatePlayerColors); } @@ -47,10 +36,13 @@ public class TownyListener implements Listener { @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("~", ""))); - } + if (p != null) + resetNameColor(p); + } + + private void resetNameColor(Player p) { + User user = PluginMain.essentials.getUser(p); + user.setNickname(ChatColor.stripColor(user.getNick(true).replace("~", ""))); } @EventHandler @@ -74,44 +66,25 @@ public class TownyListener implements Listener { 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 + @EventHandler 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); + updateTownMembers(event.getTown()); //The town still has it's colours } @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("~", ""))); - } + public void onNationDelete(DeleteNationEvent event) { + PluginMain.NationColor.remove(event.getNationName().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()); + public void onNationCreate(NewNationEvent event) { + Player p = Bukkit.getPlayer(event.getNation().getCapital().getMayor().getName()); if (p != null) - p.sendMessage("§6Use /u towncolor to set a color for the town."); + p.sendMessage("§6Use /u nationcolor to set a color for the nation."); } } -- 2.30.2 From e6ada0b2d05371ee6528e5d509a3bbc87d1b65ad Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Wed, 24 Oct 2018 00:08:03 +0200 Subject: [PATCH 4/8] Probably fixed a Discord chat issue DiscordPlugin#61 --- .../chat/commands/appendtext/AppendTextCommandBase.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/buttondevteam/chat/commands/appendtext/AppendTextCommandBase.java b/src/main/java/buttondevteam/chat/commands/appendtext/AppendTextCommandBase.java index 01255df..129adef 100644 --- a/src/main/java/buttondevteam/chat/commands/appendtext/AppendTextCommandBase.java +++ b/src/main/java/buttondevteam/chat/commands/appendtext/AppendTextCommandBase.java @@ -28,10 +28,10 @@ public abstract class AppendTextCommandBase extends TBMCCommandBase { cp, msg).fromCommand(true).build()); else if (sender.isOp()) TBMCChatAPI.SendChatMessage(ChatMessage.builder(PlayerListener.ConsoleChannel, sender, - (cp = TBMCPlayer.getPlayer(new UUID(0, 0), ChatPlayer.class)), msg).build()); + (cp = TBMCPlayer.getPlayer(new UUID(0, 0), ChatPlayer.class)), msg).fromCommand(true).build()); else TBMCChatAPI.SendChatMessage(ChatMessage.builder(Channel.GlobalChat, sender, - (cp = TBMCPlayer.getPlayer(new UUID(0, 0), ChatPlayer.class)), msg).build()); //TODO + (cp = TBMCPlayer.getPlayer(new UUID(0, 0), ChatPlayer.class)), msg).fromCommand(true).build()); //TODO return true; } } -- 2.30.2 From 3124d41f54a3009e808af611111c7afa7e5a240d Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Thu, 25 Oct 2018 14:13:56 +0200 Subject: [PATCH 5/8] AppendText, F and other user fixes #88 TBMCPlugins/DiscordPlugin#61 --- .../java/buttondevteam/chat/ChatPlayer.java | 2 - .../appendtext/AppendTextCommandBase.java | 31 +++----- .../ucmds/admin/NationColorCommand.java | 2 +- .../chat/listener/PlayerListener.java | 71 ++++++++----------- 4 files changed, 42 insertions(+), 64 deletions(-) diff --git a/src/main/java/buttondevteam/chat/ChatPlayer.java b/src/main/java/buttondevteam/chat/ChatPlayer.java index d4b3db5..2ffa48d 100644 --- a/src/main/java/buttondevteam/chat/ChatPlayer.java +++ b/src/main/java/buttondevteam/chat/ChatPlayer.java @@ -1,6 +1,5 @@ package buttondevteam.chat; -import buttondevteam.lib.chat.Channel; import buttondevteam.lib.chat.Color; import buttondevteam.lib.player.EnumPlayerData; import buttondevteam.lib.player.PlayerClass; @@ -50,7 +49,6 @@ public class ChatPlayer extends TBMCPlayerBase { public Location SavedLocation; public boolean Working; // public int Tables = 10; - public Channel CurrentChannel = Channel.GlobalChat; public boolean SendingLink = false; public boolean RainbowPresserColorMode = false; public Color OtherColorMode = null; diff --git a/src/main/java/buttondevteam/chat/commands/appendtext/AppendTextCommandBase.java b/src/main/java/buttondevteam/chat/commands/appendtext/AppendTextCommandBase.java index 129adef..74350d6 100644 --- a/src/main/java/buttondevteam/chat/commands/appendtext/AppendTextCommandBase.java +++ b/src/main/java/buttondevteam/chat/commands/appendtext/AppendTextCommandBase.java @@ -1,13 +1,12 @@ package buttondevteam.chat.commands.appendtext; -import buttondevteam.chat.ChatPlayer; -import buttondevteam.chat.listener.PlayerListener; -import buttondevteam.lib.chat.*; +import buttondevteam.lib.chat.ChatMessage; +import buttondevteam.lib.chat.CommandClass; +import buttondevteam.lib.chat.TBMCChatAPI; +import buttondevteam.lib.chat.TBMCCommandBase; +import buttondevteam.lib.player.ChromaGamerBase; import buttondevteam.lib.player.TBMCPlayer; import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -import java.util.UUID; @CommandClass(modOnly = false, excludeFromPath = true) public abstract class AppendTextCommandBase extends TBMCCommandBase { @@ -18,20 +17,12 @@ public abstract class AppendTextCommandBase extends TBMCCommandBase { @Override public boolean OnCommand(CommandSender sender, String alias, String[] args) { - String msg = GetAppendedText(); - for (int i = args.length - 1; i >= 0; i--) - msg = args[i] + " " + msg; - ChatPlayer cp; - if (sender instanceof Player) - TBMCChatAPI.SendChatMessage(ChatMessage.builder( - (cp = TBMCPlayer.getPlayer(((Player) sender).getUniqueId(), ChatPlayer.class)).CurrentChannel, sender, - cp, msg).fromCommand(true).build()); - else if (sender.isOp()) - TBMCChatAPI.SendChatMessage(ChatMessage.builder(PlayerListener.ConsoleChannel, sender, - (cp = TBMCPlayer.getPlayer(new UUID(0, 0), ChatPlayer.class)), msg).fromCommand(true).build()); - else - TBMCChatAPI.SendChatMessage(ChatMessage.builder(Channel.GlobalChat, sender, - (cp = TBMCPlayer.getPlayer(new UUID(0, 0), ChatPlayer.class)), msg).fromCommand(true).build()); //TODO + StringBuilder msg = new StringBuilder(); + for (String arg : args) msg.append(arg).append(" "); + msg.append(GetAppendedText()); + TBMCChatAPI.SendChatMessage(ChatMessage.builder(sender, + ChromaGamerBase.getFromSender(sender, TBMCPlayer.class), msg.toString()) + .fromCommand(true).build()); return true; } } diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/admin/NationColorCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/admin/NationColorCommand.java index 19d0129..c2dd682 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/admin/NationColorCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/admin/NationColorCommand.java @@ -1,4 +1,4 @@ package buttondevteam.chat.commands.ucmds.admin; -public class NationColorCommand { +public class NationColorCommand { //TODO } diff --git a/src/main/java/buttondevteam/chat/listener/PlayerListener.java b/src/main/java/buttondevteam/chat/listener/PlayerListener.java index 1c379a8..aebd816 100644 --- a/src/main/java/buttondevteam/chat/listener/PlayerListener.java +++ b/src/main/java/buttondevteam/chat/listener/PlayerListener.java @@ -6,6 +6,7 @@ import buttondevteam.chat.PluginMain; import buttondevteam.lib.TBMCChatEvent; import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.chat.*; +import buttondevteam.lib.player.ChromaGamerBase; import buttondevteam.lib.player.ChromaGamerBase.InfoTarget; import buttondevteam.lib.player.TBMCPlayer; import buttondevteam.lib.player.TBMCPlayerGetInfoEvent; @@ -13,6 +14,7 @@ import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; import com.vexsoftware.votifier.model.Vote; import com.vexsoftware.votifier.model.VotifierEvent; +import lombok.val; import net.ess3.api.events.NickChangeEvent; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -28,12 +30,14 @@ import org.bukkit.event.server.ServerCommandEvent; import org.bukkit.help.HelpTopic; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; +import org.bukkit.scheduler.BukkitTask; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Map.Entry; +import java.util.Random; +import java.util.UUID; import java.util.function.BiPredicate; -import java.util.function.Consumer; -import java.util.function.Supplier; public class PlayerListener implements Listener { /** @@ -57,7 +61,7 @@ public class PlayerListener implements Listener { if (event.isCancelled()) return; ChatPlayer cp = TBMCPlayer.getPlayer(event.getPlayer().getUniqueId(), ChatPlayer.class); - TBMCChatAPI.SendChatMessage(ChatMessage.builder(cp.CurrentChannel, event.getPlayer(), cp, event.getMessage()).build()); + TBMCChatAPI.SendChatMessage(ChatMessage.builder(event.getPlayer(), cp, event.getMessage()).build()); event.setCancelled(true); // The custom event should only be cancelled when muted or similar } @@ -71,11 +75,7 @@ public class PlayerListener implements Listener { if (message.length() < 2) return false; int index = message.indexOf(" "); - ChatPlayer mp; - if (sender instanceof Player) - mp = TBMCPlayer.getPlayer(((Player) sender).getUniqueId(), ChatPlayer.class); - else - mp = TBMCPlayer.getPlayer(new UUID(0, 0), ChatPlayer.class); //Use 0, 0 for console and whatever - TODO: Refactor console stuff + val mp = ChromaGamerBase.getFromSender(sender, TBMCPlayer.class); String cmd; final BiPredicate checkchid = (chan, cmd1) -> cmd1.equalsIgnoreCase(chan.ID) || (chan.IDs != null && Arrays.stream(chan.IDs).anyMatch(cmd1::equalsIgnoreCase)); if (index == -1) { // Only the command is run @@ -85,24 +85,17 @@ public class PlayerListener implements Listener { cmd = sender instanceof ConsoleCommandSender ? message : message.substring(1); for (Channel channel : Channel.getChannels()) { if (checkchid.test(channel, cmd)) { - Supplier getch = () -> sender instanceof Player ? mp.CurrentChannel : ConsoleChannel; - Consumer setch = ch -> { - if (sender instanceof Player) - mp.CurrentChannel = ch; - else - ConsoleChannel = ch; - }; - Channel oldch = getch.get(); + Channel oldch = mp.channel().get(); if (oldch instanceof ChatRoom) ((ChatRoom) oldch).leaveRoom(sender); if (oldch.equals(channel)) - setch.accept(Channel.GlobalChat); + mp.channel().set(Channel.GlobalChat); else { - setch.accept(channel); + mp.channel().set(channel); if (channel instanceof ChatRoom) ((ChatRoom) channel).joinRoom(sender); } - sender.sendMessage("§6You are now talking in: §b" + getch.get().DisplayName); + sender.sendMessage("§6You are now talking in: §b" + mp.channel().get().DisplayName); return true; } } @@ -134,7 +127,7 @@ public class PlayerListener implements Listener { } else for (Channel channel : Channel.getChannels()) { if (checkchid.test(channel, cmd)) { //Apparently method references don't require final variables - TBMCChatAPI.SendChatMessage(ChatMessage.builder(channel, sender, mp, message.substring(index + 1)).build()); + TBMCChatAPI.SendChatMessage(ChatMessage.builder(sender, mp, message.substring(index + 1)).build(), channel); return true; } } @@ -174,7 +167,7 @@ public class PlayerListener implements Listener { public static boolean ActiveF = false; public static ChatPlayer FPlayer = null; - private Timer Ftimer; + public static BukkitTask Ftask = null; public static int AlphaDeaths; public static ArrayList Fs = new ArrayList<>(); @@ -184,28 +177,26 @@ public class PlayerListener implements Listener { AlphaDeaths++; // MinigamePlayer mgp = Minigames.plugin.pdata.getMinigamePlayer(e.getEntity()); if (/* (mgp != null && !mgp.isInMinigame()) && */ new Random().nextBoolean()) { // Don't store Fs for NPCs - if (Ftimer != null) - Ftimer.cancel(); + Runnable tt = () -> { + if (ActiveF) { + ActiveF = false; + if (FPlayer != null && FPlayer.FCount().get() < Integer.MAX_VALUE - 1) + FPlayer.FCount().set(FPlayer.FCount().get() + Fs.size()); + Bukkit.broadcastMessage("§b" + Fs.size() + " " + (Fs.size() == 1 ? "person" : "people") + + " paid their respects.§r"); + Fs.clear(); + } + }; + if (Ftask != null) { + Ftask.cancel(); + tt.run(); //Finish previous one + } ActiveF = true; Fs.clear(); FPlayer = TBMCPlayer.getPlayer(e.getEntity().getUniqueId(), ChatPlayer.class); FPlayer.FDeaths().set(FPlayer.FDeaths().get() + 1); Bukkit.broadcastMessage("§bPress F to pay respects.§r"); - Ftimer = new Timer(); - TimerTask tt = new TimerTask() { - @Override - public void run() { - if (ActiveF) { - ActiveF = false; - if (FPlayer != null && FPlayer.FCount().get() < Integer.MAX_VALUE - 1) - FPlayer.FCount().set(FPlayer.FCount().get() + Fs.size()); - Bukkit.broadcastMessage("§b" + Fs.size() + " " + (Fs.size() == 1 ? "person" : "people") - + " paid their respects.§r"); - Fs.clear(); - } - } - }; - Ftimer.schedule(tt, 15 * 1000); + Bukkit.getScheduler().runTaskLaterAsynchronously(PluginMain.Instance, tt, 15 * 20); } } @@ -239,8 +230,6 @@ public class PlayerListener implements Listener { } } - public static Channel ConsoleChannel = Channel.GlobalChat; - @EventHandler(priority = EventPriority.HIGHEST) public void onConsoleCommand(ServerCommandEvent event) { if (onCommandPreprocess(event.getSender(), event.getCommand())) -- 2.30.2 From 58d8d1276f099c3fd0463af0656b270b1cc175bf Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Thu, 25 Oct 2018 23:51:39 +0200 Subject: [PATCH 6/8] Yeehaw fix & applying changes --- .../java/buttondevteam/chat/commands/YeehawCommand.java | 9 ++++----- .../chat/commands/appendtext/AppendTextCommandBase.java | 3 +-- .../java/buttondevteam/chat/listener/PlayerListener.java | 2 +- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/main/java/buttondevteam/chat/commands/YeehawCommand.java b/src/main/java/buttondevteam/chat/commands/YeehawCommand.java index 6a4b563..ba42373 100644 --- a/src/main/java/buttondevteam/chat/commands/YeehawCommand.java +++ b/src/main/java/buttondevteam/chat/commands/YeehawCommand.java @@ -1,12 +1,11 @@ package buttondevteam.chat.commands; -import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - import buttondevteam.lib.chat.CommandClass; import buttondevteam.lib.chat.TBMCCommandBase; import buttondevteam.lib.player.TBMCYEEHAWEvent; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; @CommandClass(modOnly = false) public class YeehawCommand extends TBMCCommandBase { @@ -22,9 +21,9 @@ public class YeehawCommand extends TBMCCommandBase { + (sender instanceof Player ? ((Player) sender).getDisplayName() : sender.getName()) + " §bYEEHAWs."; for (Player p : Bukkit.getOnlinePlayers()) { p.playSound(p.getLocation(), "tbmc.yeehaw", 1f, 1f); + p.sendMessage(message); //Not broadcasting, so the Discord plugin can handle the event in a special way } // Even a cmdblock could yeehaw in theory // Or anyone from Discord - Bukkit.broadcastMessage(message); Bukkit.getPluginManager().callEvent(new TBMCYEEHAWEvent(sender)); return true; } diff --git a/src/main/java/buttondevteam/chat/commands/appendtext/AppendTextCommandBase.java b/src/main/java/buttondevteam/chat/commands/appendtext/AppendTextCommandBase.java index 74350d6..5e36f88 100644 --- a/src/main/java/buttondevteam/chat/commands/appendtext/AppendTextCommandBase.java +++ b/src/main/java/buttondevteam/chat/commands/appendtext/AppendTextCommandBase.java @@ -5,7 +5,6 @@ import buttondevteam.lib.chat.CommandClass; import buttondevteam.lib.chat.TBMCChatAPI; import buttondevteam.lib.chat.TBMCCommandBase; import buttondevteam.lib.player.ChromaGamerBase; -import buttondevteam.lib.player.TBMCPlayer; import org.bukkit.command.CommandSender; @CommandClass(modOnly = false, excludeFromPath = true) @@ -21,7 +20,7 @@ public abstract class AppendTextCommandBase extends TBMCCommandBase { for (String arg : args) msg.append(arg).append(" "); msg.append(GetAppendedText()); TBMCChatAPI.SendChatMessage(ChatMessage.builder(sender, - ChromaGamerBase.getFromSender(sender, TBMCPlayer.class), msg.toString()) + ChromaGamerBase.getFromSender(sender), msg.toString()) .fromCommand(true).build()); return true; } diff --git a/src/main/java/buttondevteam/chat/listener/PlayerListener.java b/src/main/java/buttondevteam/chat/listener/PlayerListener.java index aebd816..4997347 100644 --- a/src/main/java/buttondevteam/chat/listener/PlayerListener.java +++ b/src/main/java/buttondevteam/chat/listener/PlayerListener.java @@ -75,7 +75,7 @@ public class PlayerListener implements Listener { if (message.length() < 2) return false; int index = message.indexOf(" "); - val mp = ChromaGamerBase.getFromSender(sender, TBMCPlayer.class); + val mp = ChromaGamerBase.getFromSender(sender); String cmd; final BiPredicate checkchid = (chan, cmd1) -> cmd1.equalsIgnoreCase(chan.ID) || (chan.IDs != null && Arrays.stream(chan.IDs).anyMatch(cmd1::equalsIgnoreCase)); if (index == -1) { // Only the command is run -- 2.30.2 From 241992571e181ca4cf3d35c9abf35e446eb16012 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sun, 28 Oct 2018 23:35:20 +0100 Subject: [PATCH 7/8] Nation color works! Getting Essentials on enable Both nation and town colors are white by default --- .../buttondevteam/chat/ChatProcessing.java | 3 -- .../java/buttondevteam/chat/PluginMain.java | 1 + .../commands/ucmds/NationColorCommand.java | 47 ++++++++++++++++++- .../chat/commands/ucmds/TownColorCommand.java | 3 -- .../ucmds/admin/NationColorCommand.java | 46 +++++++++++++++++- .../ucmds/admin/TownColorCommand.java | 8 +++- .../listener/PlayerJoinLeaveListener.java | 10 ++-- 7 files changed, 102 insertions(+), 16 deletions(-) diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index 8f1b7a5..6591cd6 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -14,7 +14,6 @@ import buttondevteam.lib.chat.*; import buttondevteam.lib.player.ChromaGamerBase; import buttondevteam.lib.player.TBMCPlayer; import buttondevteam.lib.player.TBMCPlayerBase; -import com.earth2me.essentials.Essentials; import com.earth2me.essentials.User; import com.google.common.collect.Lists; import com.google.gson.Gson; @@ -94,8 +93,6 @@ public class ChatProcessing { CommandSender sender = e.getSender(); String message = e.getMessage(); long processstart = System.nanoTime(); - if (PluginMain.essentials == null) - PluginMain.essentials = (Essentials) (Bukkit.getPluginManager().getPlugin("Essentials")); Player player = (sender instanceof Player ? (Player) sender : null); User user = PluginMain.essentials.getUser(player); diff --git a/src/main/java/buttondevteam/chat/PluginMain.java b/src/main/java/buttondevteam/chat/PluginMain.java index d7b7ad1..8a79abb 100644 --- a/src/main/java/buttondevteam/chat/PluginMain.java +++ b/src/main/java/buttondevteam/chat/PluginMain.java @@ -78,6 +78,7 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. @Override public void onEnable() { Instance = this; + PluginMain.essentials = (Essentials) (Bukkit.getPluginManager().getPlugin("Essentials")); TBMCCoreAPI.RegisterEventsForExceptions(new PlayerListener(), this); TBMCCoreAPI.RegisterEventsForExceptions(new TownyListener(), this); diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/NationColorCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/NationColorCommand.java index cf22e9a..cbfb0de 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/NationColorCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/NationColorCommand.java @@ -1,4 +1,49 @@ package buttondevteam.chat.commands.ucmds; -public class NationColorCommand { +import buttondevteam.chat.PluginMain; +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 org.bukkit.entity.Player; + +@CommandClass +@OptionallyPlayerCommandClass(playerOnly = true) +public class NationColorCommand extends UCommandBase { + @Override + public String[] GetHelpText(String alias) { + return new String[]{ // + "§6---- Nation Color ----", // + "This command allows setting a color for a nation.", // + "Each town in the nation will have it's first color (border) set to this color.", // + "See the help text for /u towncolor for more details.", // + "Usage: /" + GetCommandPath() + " ", // + "Example: /" + GetCommandPath() + " blue" // + }; + } + + @Override + public boolean OnCommand(Player player, String alias, String[] args) { + Resident res; + if (!(PluginMain.TU.getResidentMap().containsKey(player.getName().toLowerCase()) + && (res = PluginMain.TU.getResidentMap().get(player.getName().toLowerCase())).isKing())) { + player.sendMessage("§cYou need to be the king of a nation to set it's colors."); + return true; + } + if (args.length > 1) { + player.sendMessage("You can only use one color."); + return true; + } + String[] a = new String[args.length + 1]; + System.arraycopy(args, 0, a, 1, args.length); + try { + a[0] = res.getTown().getNation().getName(); + } catch (NotRegisteredException e) { + TBMCCoreAPI.SendException("Failed to set nation color for player " + player + "!", e); + player.sendMessage("§cCouldn't find your town/nation... Error reported."); + return true; + } + return buttondevteam.chat.commands.ucmds.admin.NationColorCommand.SetNationColor(player, alias, a); + } } diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/TownColorCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/TownColorCommand.java index 197a574..648daf1 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/TownColorCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/TownColorCommand.java @@ -31,9 +31,6 @@ public class TownColorCommand extends UCommandBase { @Override public boolean OnCommand(Player player, String alias, String[] args) { Resident res; - // System.out.println("contains: " + PluginMain.TU.getResidentMap().contains(player.getName().toLowerCase())); - // System.out.println("res: " + PluginMain.TU.getResidentMap().get(player.getName().toLowerCase())); - // System.out.println("mayor: " + PluginMain.TU.getResidentMap().get(player.getName().toLowerCase()).isMayor()); if (!(PluginMain.TU.getResidentMap().containsKey(player.getName().toLowerCase()) && (res = PluginMain.TU.getResidentMap().get(player.getName().toLowerCase())).isMayor())) { player.sendMessage("§cYou need to be the mayor of a town to set it's colors."); diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/admin/NationColorCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/admin/NationColorCommand.java index c2dd682..60564c5 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/admin/NationColorCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/admin/NationColorCommand.java @@ -1,4 +1,48 @@ package buttondevteam.chat.commands.ucmds.admin; -public class NationColorCommand { //TODO +import buttondevteam.chat.PluginMain; +import buttondevteam.chat.listener.TownyListener; +import com.palmergames.bukkit.towny.object.Nation; +import com.palmergames.bukkit.towny.object.Town; +import lombok.val; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; + +public class NationColorCommand extends AdminCommandBase { + @Override + public String[] GetHelpText(String alias) { + return new String[]{ // + "§6---- Nation color ----", // + "Sets the color of the nation.", // + "Usage: /u admin nationcolor " // + }; + } + + @Override + public boolean OnCommand(CommandSender sender, String alias, String[] args) { + return SetNationColor(sender, alias, args); + } + + public static boolean SetNationColor(CommandSender sender, String alias, String[] args) { + if (args.length < 2) + return false; + if (args.length > 2) { + sender.sendMessage("§cYou can only use one color as a nation color."); + return true; + } + final Nation nation = PluginMain.TU.getNationsMap().get(args[0].toLowerCase()); + if (nation == null) { + sender.sendMessage("§cThe nation '" + args[0] + "' cannot be found."); + return true; + } + val c = TownColorCommand.getColorOrSendError(args[1], sender); + if (!c.isPresent()) return true; + PluginMain.NationColor.put(args[0].toLowerCase(), c.get()); + Bukkit.getScheduler().runTaskAsynchronously(PluginMain.Instance, () -> { + for (Town t : nation.getTowns()) + TownyListener.updateTownMembers(t); + }); + sender.sendMessage("§bNation color set to §" + TownColorCommand.getColorText(c.get())); + return true; + } } 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 e718b6f..e1cd357 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/admin/TownColorCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/admin/TownColorCommand.java @@ -60,16 +60,20 @@ public class TownColorCommand extends AdminCommandBase { return true; } - private static Optional getColorOrSendError(String name, CommandSender sender) { + public 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("§cAvailable colors: " + Arrays.stream(Color.values()).skip(1).map(TownColorCommand::getColorText).collect(Collectors.joining(", "))); sender.sendMessage("§cMake sure to type them exactly as shown above."); } return c; } + public static String getColorText(Color col) { + return String.format("§%x%s§r", col.ordinal(), col.getName()); + } + public static String getTownNameCased(String name) { return PluginMain.TU.getTownsMap().get(name.toLowerCase()).getName(); } diff --git a/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java b/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java index 0038a98..ec88341 100644 --- a/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java +++ b/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java @@ -9,7 +9,6 @@ import buttondevteam.lib.chat.Color; import buttondevteam.lib.player.TBMCPlayerJoinEvent; import buttondevteam.lib.player.TBMCPlayerLoadEvent; import buttondevteam.lib.player.TBMCPlayerSaveEvent; -import com.earth2me.essentials.Essentials; import com.earth2me.essentials.User; import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; import lombok.val; @@ -22,6 +21,7 @@ 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; @@ -36,8 +36,6 @@ public class PlayerJoinLeaveListener implements Listener { @EventHandler public void onPlayerTBMCJoin(TBMCPlayerJoinEvent e) { - if (PluginMain.essentials == null) - PluginMain.essentials = ((Essentials) Bukkit.getPluginManager().getPlugin("Essentials")); ChatPlayer cp = e.GetPlayer().asPluginPlayer(ChatPlayer.class); Player p = Bukkit.getPlayer(cp.getUUID()); @@ -95,9 +93,9 @@ public class PlayerJoinLeaveListener implements Listener { if (res == null || !res.hasTown()) return name; try { - val clrs = PluginMain.TownColors.get(res.getTown().getName().toLowerCase()); - if (clrs == null) - return name; + 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 -- 2.30.2 From df6e76086b0e2eecc346901b250cd123294c180c Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Thu, 1 Nov 2018 01:24:01 +0100 Subject: [PATCH 8/8] Treating the RP channel as global --- .gitignore | 4 ++-- pom.xml | 2 ++ .../java/buttondevteam/chat/PluginMain.java | 8 ++------ .../chat/commands/UnlolCommand.java | 20 +++++++++---------- 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index ea8759c..deefcdf 100644 --- a/.gitignore +++ b/.gitignore @@ -131,7 +131,7 @@ publish/ *.publishproj # NuGet Packages Directory -## TODO: If you have NuGet Package Restore enabled, uncomment the next line +## TO!DO: If you have NuGet Package Restore enabled, uncomment the next line #packages/ # Windows Azure Build Output @@ -221,4 +221,4 @@ TheButtonAutoFlair/out/artifacts/Autoflair/Autoflair.jar *.name .idea/compiler.xml *.xml -/.apt_generated/ +/.apt_generated/ diff --git a/pom.xml b/pom.xml index 8056f5b..f9ca404 100644 --- a/pom.xml +++ b/pom.xml @@ -92,6 +92,8 @@ true + false +