From 5ec209c3507545f5adf23d2e1c1e1d2038094f95 Mon Sep 17 00:00:00 2001 From: James Mortemore Date: Sat, 7 Apr 2018 11:07:08 +0100 Subject: [PATCH] JSONified /alts and possible alts in /bminfo Clicking player name will execute /bminfo Player names are now colour coded in this command Green = never banned Yellow = Banned previously Gold = Temporarily banned Red = Perm banned Fixed #759 --- pom.xml | 13 +++++ .../banmanager/commands/FindAltsCommand.java | 34 +++++++++---- .../banmanager/commands/InfoCommand.java | 32 ++++++++---- .../banmanager/util/JSONCommandUtils.java | 51 +++++++++++++++++++ 4 files changed, 110 insertions(+), 20 deletions(-) create mode 100644 src/main/java/me/confuser/banmanager/util/JSONCommandUtils.java diff --git a/pom.xml b/pom.xml index b8e10158..093ba2b6 100644 --- a/pom.xml +++ b/pom.xml @@ -101,6 +101,10 @@ javax.annotation me.confuser.banmanager.internal.jaxax.annotation + + me.rayzr522.jsonmessage + me.confuser.banmanager.internal.jsonmessage + @@ -125,6 +129,10 @@ Central http://repo1.maven.org/maven2 + + rayzr-repo + https://cdn.rawgit.com/Rayzr522/maven-repo/master/ + @@ -199,6 +207,11 @@ geoip2 2.2.0 + + me.rayzr522 + jsonmessage + 1.0.0 + junit junit diff --git a/src/main/java/me/confuser/banmanager/commands/FindAltsCommand.java b/src/main/java/me/confuser/banmanager/commands/FindAltsCommand.java index 62a1f2c1..517a3762 100644 --- a/src/main/java/me/confuser/banmanager/commands/FindAltsCommand.java +++ b/src/main/java/me/confuser/banmanager/commands/FindAltsCommand.java @@ -5,11 +5,13 @@ import me.confuser.banmanager.data.PlayerData; import me.confuser.banmanager.util.CommandUtils; import me.confuser.banmanager.util.IPUtils; +import me.confuser.banmanager.util.JSONCommandUtils; import me.confuser.bukkitutil.Message; import org.apache.commons.lang.StringUtils; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; import java.util.ArrayList; import java.util.List; @@ -61,20 +63,32 @@ public void run() { } List players = plugin.getPlayerStorage().getDuplicates(ip); - ArrayList names = new ArrayList<>(players.size()); - for (PlayerData player : players) { - names.add(player.getName()); - } + if (sender instanceof Player) { + sender.sendMessage(Message.get("alts.header").set("ip", ipStr).toString()); + + if (players.isEmpty()) { + sender.sendMessage(Message.get("none").toString()); + return; + } - sender.sendMessage(Message.get("alts.header").set("ip", ipStr).toString()); + JSONCommandUtils.alts(players).send((Player) sender); + } else { + ArrayList names = new ArrayList<>(players.size()); - if (names.isEmpty()) { - sender.sendMessage(Message.get("none").toString()); - return; - } + for (PlayerData player : players) { + names.add(player.getName()); + } + + sender.sendMessage(Message.get("alts.header").set("ip", ipStr).toString()); + + if (names.isEmpty()) { + sender.sendMessage(Message.get("none").toString()); + return; + } - sender.sendMessage(ChatColor.GOLD + StringUtils.join(names, ", ")); + sender.sendMessage(ChatColor.GOLD + StringUtils.join(names, ", ")); + } } }); diff --git a/src/main/java/me/confuser/banmanager/commands/InfoCommand.java b/src/main/java/me/confuser/banmanager/commands/InfoCommand.java index 262d3976..02bdf353 100644 --- a/src/main/java/me/confuser/banmanager/commands/InfoCommand.java +++ b/src/main/java/me/confuser/banmanager/commands/InfoCommand.java @@ -9,8 +9,10 @@ import me.confuser.banmanager.data.*; import me.confuser.banmanager.util.DateUtils; import me.confuser.banmanager.util.IPUtils; +import me.confuser.banmanager.util.JSONCommandUtils; import me.confuser.banmanager.util.parsers.InfoCommandParser; import me.confuser.bukkitutil.Message; +import me.rayzr522.jsonmessage.JSONMessage; import org.apache.commons.lang.time.FastDateFormat; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -130,7 +132,7 @@ public void playerInfo(CommandSender sender, String name, Integer index, InfoCom PlayerData player = players.get(index - 1); - ArrayList messages = new ArrayList<>(); + ArrayList messages = new ArrayList<>(); boolean hasFlags = parser.isBans() || parser.isKicks() || parser.isMutes() || parser.isNotes() || parser .isWarnings() || parser.getIps() != null; @@ -266,15 +268,21 @@ public void playerInfo(CommandSender sender, String name, Integer index, InfoCom if (sender.hasPermission("bm.command.bminfo.alts")) { messages.add(Message.getString("alts.header")); - StringBuilder duplicates = new StringBuilder(); + List duplicatePlayers = plugin.getPlayerStorage().getDuplicates(player.getIp()); - for (PlayerData duplicatePlayer : plugin.getPlayerStorage().getDuplicates(player.getIp())) { - duplicates.append(duplicatePlayer.getName()).append(", "); - } + if (sender instanceof Player) { + messages.add(JSONCommandUtils.alts(duplicatePlayers)); + } else { + StringBuilder duplicates = new StringBuilder(); + + for (PlayerData duplicatePlayer : duplicatePlayers) { + duplicates.append(duplicatePlayer.getName()).append(", "); + } - if (duplicates.length() >= 2) duplicates.setLength(duplicates.length() - 2); + if (duplicates.length() >= 2) duplicates.setLength(duplicates.length() - 2); - messages.add(duplicates.toString()); + messages.add(duplicates.toString()); + } } if (sender.hasPermission("bm.command.bminfo.ipstats")) { @@ -364,12 +372,16 @@ public void playerInfo(CommandSender sender, String name, Integer index, InfoCom } // TODO Show last warning - for (String message : messages) { - sender.sendMessage(message); + for (Object message : messages) { + if (message instanceof String) { + sender.sendMessage((String) message); + } else if (message instanceof JSONMessage){ + ((JSONMessage) message).send((Player) sender); + } } } - private void handleIpHistory(ArrayList messages, PlayerData player, long since, int page) { + private void handleIpHistory(ArrayList messages, PlayerData player, long since, int page) { CloseableIterator iterator = null; try { iterator = plugin.getPlayerHistoryStorage().getSince(player, since, page); diff --git a/src/main/java/me/confuser/banmanager/util/JSONCommandUtils.java b/src/main/java/me/confuser/banmanager/util/JSONCommandUtils.java new file mode 100644 index 00000000..7cc775a0 --- /dev/null +++ b/src/main/java/me/confuser/banmanager/util/JSONCommandUtils.java @@ -0,0 +1,51 @@ +package me.confuser.banmanager.util; + +import me.confuser.banmanager.BanManager; +import me.confuser.banmanager.data.PlayerBanData; +import me.confuser.banmanager.data.PlayerData; +import me.rayzr522.jsonmessage.JSONMessage; +import org.bukkit.ChatColor; + +import java.sql.SQLException; +import java.util.List; + +public class JSONCommandUtils { + private static BanManager plugin = BanManager.getPlugin(); + + public static JSONMessage alts(List players) { + JSONMessage message = JSONMessage.create(); + int index = 0; + + for (PlayerData player : players) { + ChatColor colour = ChatColor.GREEN; + + if (plugin.getPlayerBanStorage().isBanned(player.getUUID())) { + PlayerBanData ban = plugin.getPlayerBanStorage().getBan(player.getUUID()); + + if (ban.getExpires() == 0) { + colour = ChatColor.RED; + } else { + colour = ChatColor.GOLD; + } + } else { + try { + if (plugin.getPlayerBanRecordStorage().getCount(player) != 0) { + colour = ChatColor.YELLOW; + } + } catch (SQLException e) { + e.printStackTrace(); + } + } + + message.then(player.getName()).color(colour).runCommand("/bminfo " + player.getName()); + + if (index != players.size() - 1) { + message.then(", "); + } + + index++; + } + + return message; + } +}