From cee69dc55b6a579ca0d15cc539bbe34721089495 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Wed, 1 Nov 2017 22:55:34 +0100 Subject: [PATCH 1/3] Made /u ncolor, needs further testing --- .travis.yml | 4 +- .../java/buttondevteam/chat/ChatPlayer.java | 5 ++ .../buttondevteam/chat/ChatProcessing.java | 16 +++- .../chat/commands/ucmds/NColorCommand.java | 76 +++++++++++++++++++ .../chat/commands/ucmds/TownColorCommand.java | 9 --- 5 files changed, 95 insertions(+), 15 deletions(-) create mode 100644 src/main/java/buttondevteam/chat/commands/ucmds/NColorCommand.java diff --git a/.travis.yml b/.travis.yml index dfd0224..808d9b0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,10 +2,10 @@ cache: directories: - $HOME/.m2/repository/org/ before_install: | # Wget BuildTools and run if cached folder not found - if [ ! -d "$HOME/.m2/repository/org/spigotmc/spigot/1.12.1-R0.1-SNAPSHOT" ]; then + if [ ! -d "$HOME/.m2/repository/org/spigotmc/spigot/1.12.2-R0.1-SNAPSHOT" ]; then wget -O BuildTools.jar https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar # grep so that download counts don't appear in log files - java -jar BuildTools.jar --rev 1.12.1 | grep -vE "[^/ ]*/[^/ ]*\s*KB\s*$" | grep -v "^\s*$" + java -jar BuildTools.jar --rev 1.12.2 | grep -vE "[^/ ]*/[^/ ]*\s*KB\s*$" | grep -v "^\s*$" fi language: java jdk: diff --git a/src/main/java/buttondevteam/chat/ChatPlayer.java b/src/main/java/buttondevteam/chat/ChatPlayer.java index 5b6c424..71e7b73 100644 --- a/src/main/java/buttondevteam/chat/ChatPlayer.java +++ b/src/main/java/buttondevteam/chat/ChatPlayer.java @@ -11,6 +11,7 @@ import buttondevteam.lib.player.EnumPlayerData; import buttondevteam.lib.player.PlayerClass; import buttondevteam.lib.player.PlayerData; import buttondevteam.lib.player.TBMCPlayerBase; +import gnu.trove.list.array.TIntArrayList; @PlayerClass(pluginname = "Button1Chat") public class ChatPlayer extends TBMCPlayerBase { @@ -42,6 +43,10 @@ public class ChatPlayer extends TBMCPlayerBase { return data(false); } + public PlayerData NameColorLocations() { // No byte[] + return data(null); + } + public Location SavedLocation; public boolean Working; // public int Tables = 10; diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index 062c596..e861d4a 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -1,9 +1,11 @@ package buttondevteam.chat; 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; import org.bukkit.Bukkit; @@ -259,12 +261,18 @@ public class ChatProcessing { return player.getDisplayName(); String ret = ""; String name = ChatColor.stripColor(player.getDisplayName()); + 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(len * i) : name.substring(len * i, len * i + len)); int len = name.length() / clrs.length; - // val bounds = new int[clrs.length - 1]; - // for (int i = 0; i < clrs.length; i++) + int[] ncl = ChatPlayer.getPlayer(player.getUniqueId(), ChatPlayer.class).NameColorLocations().get() + .toArray(); + if (Arrays.stream(ncl).sum() != name.length() || ncl.length != clrs.length) { + System.out.println("Name length changed: " + Arrays.stream(ncl).sum() + " -> " + name.length()); + ncl = null; // Reset if name length changed + } for (int i = 0; i < clrs.length; i++) - ret += "§" + 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(len * i) : name.substring(len * i, len * i + len)); + ret += coloredNamePart.apply(ncl == null ? len : ncl[i], i); return ret; } catch (NotRegisteredException e) { return player.getDisplayName(); diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/NColorCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/NColorCommand.java new file mode 100644 index 0000000..852ecfd --- /dev/null +++ b/src/main/java/buttondevteam/chat/commands/ucmds/NColorCommand.java @@ -0,0 +1,76 @@ +package buttondevteam.chat.commands.ucmds; + +import java.util.Arrays; + +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; +import gnu.trove.list.array.TIntArrayList; + +@OptionallyPlayerCommandClass(playerOnly = true) +@CommandClass +public class NColorCommand extends UCommandBase { + @Override + public String[] GetHelpText(String alias) { + return new String[] { // + "§6---- Name color ----", // + "This command allows you to set how the town colors look on your name.", // + "To use this command, you need to be in a town which has town colors set.", // + "Use a vertical line as a separator between the colors.", // + "Example: /u ncolor Norbi|Peti --> §6Norbi§ePeti" // + }; + } + + @Override + public boolean OnCommand(Player player, String alias, String[] args) { + Resident res; + Town town; + try { + if ((res = PluginMain.TU.getResidentMap().get(player.getName().toLowerCase())) == null || !res.hasTown() + || (town = res.getTown()) == null) { + player.sendMessage("§cYou need to be in a town."); + return true; + } + } catch (Exception e) { + player.sendMessage("§cYou need to be in a town. (" + e + ")"); + return true; + } + if (args.length == 0) + return false; + 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 (|)."); + return true; + } + String[] nameparts = args[0].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."); + return true; + } + if (nameparts.length > towncolors.length * 2) { + player.sendMessage("§cYou have waay too many vertical lines (|) in your name."); + return true; + } + if (nameparts.length > towncolors.length) { + player.sendMessage("§cYou have too many vertical lines (|) in your name."); + return true; + } + ChatPlayer.getPlayer(player.getUniqueId(), ChatPlayer.class).NameColorLocations() + .set(TIntArrayList.wrap(Arrays.stream(nameparts).mapToInt(np -> np.length()).toArray())); // No byte[] + player.sendMessage("§bName colors set."); // TODO: ArrayList is what it becomes I think + return true; + } +} diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/TownColorCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/TownColorCommand.java index b667ea7..63c7623 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/TownColorCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/TownColorCommand.java @@ -1,22 +1,13 @@ package buttondevteam.chat.commands.ucmds; -import java.util.Arrays; - -import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import org.dynmap.towny.DynmapTownyPlugin; - -import com.mysql.fabric.xmlrpc.base.Array; 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.Color; import buttondevteam.lib.chat.CommandClass; import buttondevteam.lib.chat.OptionallyPlayerCommandClass; -import lombok.val; @CommandClass // TODO: /u u when annotation not present @OptionallyPlayerCommandClass(playerOnly = true) From 9057ab9f1516ed2ddfcaeae1dc3f19bc90b10ab6 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 4 Nov 2017 02:04:34 +0100 Subject: [PATCH 2/3] Fix --- src/main/java/buttondevteam/chat/ChatPlayer.java | 3 +-- src/main/java/buttondevteam/chat/ChatProcessing.java | 4 ++-- .../buttondevteam/chat/commands/ucmds/NColorCommand.java | 7 ++++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/buttondevteam/chat/ChatPlayer.java b/src/main/java/buttondevteam/chat/ChatPlayer.java index 71e7b73..b54f75a 100644 --- a/src/main/java/buttondevteam/chat/ChatPlayer.java +++ b/src/main/java/buttondevteam/chat/ChatPlayer.java @@ -11,7 +11,6 @@ import buttondevteam.lib.player.EnumPlayerData; import buttondevteam.lib.player.PlayerClass; import buttondevteam.lib.player.PlayerData; import buttondevteam.lib.player.TBMCPlayerBase; -import gnu.trove.list.array.TIntArrayList; @PlayerClass(pluginname = "Button1Chat") public class ChatPlayer extends TBMCPlayerBase { @@ -43,7 +42,7 @@ public class ChatPlayer extends TBMCPlayerBase { return data(false); } - public PlayerData NameColorLocations() { // No byte[] + public PlayerData> NameColorLocations() { // No byte[], no TIntArrayList return data(null); } diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index e861d4a..bb7c977 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -265,8 +265,8 @@ public class ChatProcessing { + 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(len * i) : name.substring(len * i, len * i + len)); int len = name.length() / clrs.length; - int[] ncl = ChatPlayer.getPlayer(player.getUniqueId(), ChatPlayer.class).NameColorLocations().get() - .toArray(); + int[] ncl = ChatPlayer.getPlayer(player.getUniqueId(), ChatPlayer.class).NameColorLocations().get().stream() + .mapToInt(Integer::intValue).toArray(); if (Arrays.stream(ncl).sum() != name.length() || ncl.length != clrs.length) { System.out.println("Name length changed: " + Arrays.stream(ncl).sum() + " -> " + name.length()); ncl = null; // Reset if name length changed diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/NColorCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/NColorCommand.java index 852ecfd..cc6dca4 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/NColorCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/NColorCommand.java @@ -1,6 +1,8 @@ package buttondevteam.chat.commands.ucmds; +import java.util.ArrayList; import java.util.Arrays; +import java.util.stream.Collectors; import org.bukkit.ChatColor; import org.bukkit.entity.Player; @@ -13,7 +15,6 @@ import buttondevteam.chat.PluginMain; import buttondevteam.lib.chat.Color; import buttondevteam.lib.chat.CommandClass; import buttondevteam.lib.chat.OptionallyPlayerCommandClass; -import gnu.trove.list.array.TIntArrayList; @OptionallyPlayerCommandClass(playerOnly = true) @CommandClass @@ -69,8 +70,8 @@ public class NColorCommand extends UCommandBase { return true; } ChatPlayer.getPlayer(player.getUniqueId(), ChatPlayer.class).NameColorLocations() - .set(TIntArrayList.wrap(Arrays.stream(nameparts).mapToInt(np -> np.length()).toArray())); // No byte[] - player.sendMessage("§bName colors set."); // TODO: ArrayList is what it becomes I think + .set(new ArrayList<>(Arrays.stream(nameparts).map(np -> np.length()).collect(Collectors.toList()))); // No byte[], no TIntArrayList + player.sendMessage("§bName colors set."); return true; } } From ae3c05c14aeed18d934217a6c15fd27c0c7afaa7 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 4 Nov 2017 16:35:37 +0100 Subject: [PATCH 3/3] /u ncolor works! --- .../buttondevteam/chat/ChatProcessing.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index bb7c977..2022a09 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -259,21 +259,25 @@ public class ChatProcessing { val clrs = PluginMain.TownColors.get(res.getTown().getName().toLowerCase()); if (clrs == null) return player.getDisplayName(); - String ret = ""; + 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(len * i) : name.substring(len * i, len * i + len)); + + (i + 1 == clrs.length ? name.substring(prevlen.get()) + : name.substring(prevlen.get(), prevlen.addAndGet(len))); int len = name.length() / clrs.length; - int[] ncl = ChatPlayer.getPlayer(player.getUniqueId(), ChatPlayer.class).NameColorLocations().get().stream() - .mapToInt(Integer::intValue).toArray(); - if (Arrays.stream(ncl).sum() != name.length() || ncl.length != clrs.length) { - System.out.println("Name length changed: " + Arrays.stream(ncl).sum() + " -> " + name.length()); + val nclar = ChatPlayer.getPlayer(player.getUniqueId(), ChatPlayer.class).NameColorLocations().get(); + int[] ncl = nclar == null ? null : nclar.stream().mapToInt(Integer::intValue).toArray(); + if (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 += coloredNamePart.apply(ncl == null ? len : ncl[i], i); - return ret; + ret.append(coloredNamePart.apply(ncl == null ? len : ncl[i], i)); + return ret.toString(); } catch (NotRegisteredException e) { return player.getDisplayName(); }