From 4006cdad586ecd40275a606ae6b850486196777d Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 16 Apr 2018 21:51:19 +0200 Subject: [PATCH] Buunch of fixes (TownColor) - Banned the color black - Made the color list colored (when using an invalid color) - Finished displayname-refactor (hopefully) - /u ncolor will now show how your name looks - Using the display name in the Tab list - Nickname handling fixed in NColor - Smaller fixes Testing needed --- .../java/buttondevteam/chat/ChatPlayer.java | 17 +++---- .../buttondevteam/chat/ChatProcessing.java | 34 +------------- .../chat/commands/ucmds/NColorCommand.java | 27 ++++++------ .../ucmds/admin/TownColorCommand.java | 24 +++++++--- .../listener/PlayerJoinLeaveListener.java | 44 ++++++++++++++++++- .../chat/listener/PlayerListener.java | 41 ++++++++++------- 6 files changed, 111 insertions(+), 76 deletions(-) diff --git a/src/main/java/buttondevteam/chat/ChatPlayer.java b/src/main/java/buttondevteam/chat/ChatPlayer.java index b54f75a..592d3de 100644 --- a/src/main/java/buttondevteam/chat/ChatPlayer.java +++ b/src/main/java/buttondevteam/chat/ChatPlayer.java @@ -1,16 +1,17 @@ package buttondevteam.chat; -import java.util.ArrayList; -import java.util.List; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.entity.Player; - -import buttondevteam.lib.chat.*; +import buttondevteam.lib.chat.Channel; +import buttondevteam.lib.chat.Color; import buttondevteam.lib.player.EnumPlayerData; import buttondevteam.lib.player.PlayerClass; import buttondevteam.lib.player.PlayerData; import buttondevteam.lib.player.TBMCPlayerBase; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.entity.Player; + +import java.util.ArrayList; +import java.util.List; @PlayerClass(pluginname = "Button1Chat") public class ChatPlayer extends TBMCPlayerBase { @@ -104,7 +105,7 @@ public class ChatPlayer extends TBMCPlayerBase { // PluginMain.Instance.getServer().getScoreboardManager().getMainScoreboard().getTeams().add() Player p = Bukkit.getPlayer(uuid); if (p != null) - p.setPlayerListName(String.format("%s%s", p.getName(), GetFormattedFlair())); + p.setPlayerListName(String.format("%s%s", p.getDisplayName(), GetFormattedFlair())); } public short GetFlairColor() { diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index 419f89d..e725ed0 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -18,21 +18,17 @@ import com.earth2me.essentials.Essentials; import com.google.common.collect.Lists; import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; import lombok.val; import org.bukkit.Bukkit; -import org.bukkit.ChatColor; import org.bukkit.Sound; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.scoreboard.Objective; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; -import java.util.function.BiFunction; import java.util.regex.Pattern; public class ChatProcessing { @@ -214,35 +210,7 @@ public class ChatProcessing { private static String getSenderName(CommandSender sender, Player player) { if (player == null) return sender.getName(); - val res = PluginMain.TU.getResidentMap().get(player.getName().toLowerCase()); - if (res == null || !res.hasTown()) - return player.getDisplayName(); - try { - val clrs = PluginMain.TownColors.get(res.getTown().getName().toLowerCase()); - if (clrs == null) - return player.getDisplayName(); - StringBuilder ret = new StringBuilder(); - String name = ChatColor.stripColor(player.getDisplayName()); - 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; - val nclar = ChatPlayer.getPlayer(player.getUniqueId(), ChatPlayer.class).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 - if (name.charAt(0) == '~') { // Ignore ~ in nicknames - prevlen.incrementAndGet(); - ret.append("~"); - } - for (int i = 0; i < clrs.length; i++) - ret.append(coloredNamePart.apply(ncl == null ? len : ncl[i], i)); - return ret.toString(); - } catch (NotRegisteredException e) { - return player.getDisplayName(); - } + return player.getDisplayName(); } static String getChannelID(Channel channel, CommandSender sender) { diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/NColorCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/NColorCommand.java index cc6dca4..71e2ec6 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/NColorCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/NColorCommand.java @@ -1,21 +1,20 @@ package buttondevteam.chat.commands.ucmds; +import buttondevteam.chat.ChatPlayer; +import buttondevteam.chat.PluginMain; +import buttondevteam.chat.listener.PlayerJoinLeaveListener; +import buttondevteam.lib.chat.Color; +import buttondevteam.lib.chat.CommandClass; +import buttondevteam.lib.chat.OptionallyPlayerCommandClass; +import com.palmergames.bukkit.towny.object.Resident; +import com.palmergames.bukkit.towny.object.Town; +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; + import java.util.ArrayList; import java.util.Arrays; import java.util.stream.Collectors; -import org.bukkit.ChatColor; -import org.bukkit.entity.Player; - -import com.palmergames.bukkit.towny.object.Resident; -import com.palmergames.bukkit.towny.object.Town; - -import buttondevteam.chat.ChatPlayer; -import buttondevteam.chat.PluginMain; -import buttondevteam.lib.chat.Color; -import buttondevteam.lib.chat.CommandClass; -import buttondevteam.lib.chat.OptionallyPlayerCommandClass; - @OptionallyPlayerCommandClass(playerOnly = true) @CommandClass public class NColorCommand extends UCommandBase { @@ -46,6 +45,7 @@ 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()))) { player.sendMessage("§cThe name you gave doesn't match your name. Make sure to use " + ChatColor.stripColor(player.getDisplayName()) + "§c with added vertical lines (|)."); @@ -71,7 +71,8 @@ public class NColorCommand extends UCommandBase { } ChatPlayer.getPlayer(player.getUniqueId(), ChatPlayer.class).NameColorLocations() .set(new ArrayList<>(Arrays.stream(nameparts).map(np -> np.length()).collect(Collectors.toList()))); // No byte[], no TIntArrayList - player.sendMessage("§bName colors set."); + PlayerJoinLeaveListener.updatePlayerColors(player); + player.sendMessage("§bName colors set: " + player.getDisplayName()); 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 6541e1a..8b4a44f 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,10 @@ package buttondevteam.chat.commands.ucmds.admin; import buttondevteam.chat.PluginMain; +import buttondevteam.chat.listener.PlayerJoinLeaveListener; 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; import org.bukkit.command.CommandSender; @@ -38,18 +41,29 @@ public class TownColorCommand extends AdminCommandBase { val clrs = new Color[args.length - 1]; for (int i = 1; i < args.length; i++) { val ii = i; - val c = Arrays.stream(Color.values()).filter(cc -> cc.getName().equalsIgnoreCase(args[ii])).findAny(); - if (!c.isPresent()) { - sender.sendMessage("§cThe color '" + args[i] + "' cannot be found."); - sender.sendMessage("§cAvailable colors: " + Arrays.stream(Color.values()).map(Color::getName).collect(Collectors.joining(", "))); + val c = Arrays.stream(Color.values()).skip(1).filter(cc -> cc.getName().equalsIgnoreCase(args[ii])).findAny(); + 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(", "))); return true; } 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) { + } + }); + val dtp = (DynmapTownyPlugin) Bukkit.getPluginManager().getPlugin("Dynmap-Towny"); if (dtp == null) { - sender.sendMessage("§cDynmap-Towny couldn'5 be found to set town color."); + sender.sendMessage("§cDynmap-Towny couldn't be found §6but otherwise §btown color set."); + PluginMain.Instance.getLogger().warning("Dynmap-Towny not found for setting town color!"); return true; } PluginMain.setTownColor(dtp, args[0].toLowerCase(), clrs); diff --git a/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java b/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java index 33bce55..396a668 100644 --- a/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java +++ b/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java @@ -9,14 +9,20 @@ import buttondevteam.lib.player.TBMCPlayerJoinEvent; import buttondevteam.lib.player.TBMCPlayerLoadEvent; import buttondevteam.lib.player.TBMCPlayerSaveEvent; import com.earth2me.essentials.Essentials; +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.Timer; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.BiFunction; public class PlayerJoinLeaveListener implements Listener { @@ -72,7 +78,7 @@ public class PlayerJoinLeaveListener implements Listener { String nwithoutformatting = PluginMain.essentials.getUser(p).getNickname(); - p.setDisplayName(); + updatePlayerColors(p); int index; if (nwithoutformatting != null) { @@ -102,4 +108,40 @@ public class PlayerJoinLeaveListener implements Listener { UnlolCommand.Lastlol.values().removeIf(lld -> lld.getLolowner().equals(event.getPlayer())); } + private static String getPlayerDisplayName(Player player) { + String nickname = PluginMain.essentials.getUser(player).getNick(true); + val res = PluginMain.TU.getResidentMap().get(player.getName().toLowerCase()); + if (res == null || !res.hasTown()) + return nickname; + try { + val clrs = PluginMain.TownColors.get(res.getTown().getName().toLowerCase()); + if (clrs == null) + return nickname; + 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; + val nclar = ChatPlayer.getPlayer(player.getUniqueId(), ChatPlayer.class).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 + if (name.charAt(0) == '~') { // Ignore ~ in nicknames + prevlen.incrementAndGet(); + ret.append("~"); + } + for (int i = 0; i < clrs.length; i++) + ret.append(coloredNamePart.apply(ncl == null ? len : ncl[i], i)); + return ret.toString(); + } catch (NotRegisteredException e) { + return nickname; + } + } + + public static void updatePlayerColors(Player player) { + player.setDisplayName(getPlayerDisplayName(player)); + } } diff --git a/src/main/java/buttondevteam/chat/listener/PlayerListener.java b/src/main/java/buttondevteam/chat/listener/PlayerListener.java index 0ec0db2..3f1b772 100644 --- a/src/main/java/buttondevteam/chat/listener/PlayerListener.java +++ b/src/main/java/buttondevteam/chat/listener/PlayerListener.java @@ -114,22 +114,31 @@ public class PlayerListener implements Listener { if (player != null && sender instanceof Player) player.sendMessage("§b" + ((Player) sender).getDisplayName() + " §bis in this world: " + ((Player) sender).getWorld().getName()); - } else if (cmd.equalsIgnoreCase("minecraft:me")) { - if (!(sender instanceof Player) || !PluginMain.essentials.getUser((Player) sender).isMuted()) { - String msg = message.substring(index + 1); - Bukkit.broadcastMessage(String.format("* %s %s", ((Player) sender).getDisplayName(), msg)); - return true; - } else { - sender.sendMessage("§cCan't use /minecraft:me while muted."); - return true; - } - } else - for (Channel channel : Channel.getChannels()) { - if (cmd.equalsIgnoreCase(channel.ID)) { - TBMCChatAPI.SendChatMessage(channel, sender, message.substring(index + 1)); - return true; - } - } + } else if (cmd.equalsIgnoreCase("minecraft:me")) { + if (!(sender instanceof Player) || !PluginMain.essentials.getUser((Player) sender).isMuted()) { + String msg = message.substring(index + 1); + Bukkit.broadcastMessage(String.format("* %s %s", sender instanceof Player ? ((Player) sender).getDisplayName() : sender.getName(), msg)); + return true; + } else { + sender.sendMessage("§cCan't use /minecraft:me while muted."); + return true; + } + } else if (cmd.equalsIgnoreCase("me")) { //Take over for Discord broadcast + if (!(sender instanceof Player) || !PluginMain.essentials.getUser((Player) sender).isMuted()) { + String msg = message.substring(index + 1); + Bukkit.broadcastMessage(String.format("§5* %s %s", sender instanceof Player ? ((Player) sender).getDisplayName() : sender.getName(), msg)); + return true; + } else { + sender.sendMessage("§cCan't use /me while muted."); + return true; + } + } else + for (Channel channel : Channel.getChannels()) { + if (cmd.equalsIgnoreCase(channel.ID)) { + TBMCChatAPI.SendChatMessage(channel, sender, message.substring(index + 1)); + return true; + } + } // TODO: Target selectors } // We don't care if we have arguments