From 8428e2a6d35faec796546518b3c074f7e1b4a95d Mon Sep 17 00:00:00 2001 From: Super_ Date: Thu, 5 Nov 2020 08:40:48 -0500 Subject: [PATCH] name history i still need to test bcuz i dont have an alt --- src/main/java/me/totalfreedom/plex/Plex.java | 2 + .../plex/command/PlexCommand.java | 2 + .../plex/command/impl/NameHistoryCMD.java | 78 +++++++++++++++++++ .../plex/handlers/CommandHandler.java | 1 + .../me/totalfreedom/plex/util/PlexUtils.java | 25 +++++- .../totalfreedom/plex/world/CustomWorld.java | 6 +- src/main/resources/messages.yml | 9 ++- 7 files changed, 117 insertions(+), 6 deletions(-) create mode 100644 src/main/java/me/totalfreedom/plex/command/impl/NameHistoryCMD.java diff --git a/src/main/java/me/totalfreedom/plex/Plex.java b/src/main/java/me/totalfreedom/plex/Plex.java index f15fc6c..cf642e2 100644 --- a/src/main/java/me/totalfreedom/plex/Plex.java +++ b/src/main/java/me/totalfreedom/plex/Plex.java @@ -17,6 +17,8 @@ import me.totalfreedom.plex.util.PlexUtils; import me.totalfreedom.plex.world.CustomWorld; import org.bukkit.plugin.java.JavaPlugin; +import java.io.File; + @Getter @Setter public class Plex extends JavaPlugin diff --git a/src/main/java/me/totalfreedom/plex/command/PlexCommand.java b/src/main/java/me/totalfreedom/plex/command/PlexCommand.java index 473c9e0..db2a657 100644 --- a/src/main/java/me/totalfreedom/plex/command/PlexCommand.java +++ b/src/main/java/me/totalfreedom/plex/command/PlexCommand.java @@ -12,6 +12,7 @@ import me.totalfreedom.plex.command.source.CommandSource; import me.totalfreedom.plex.command.source.RequiredCommandSource; import me.totalfreedom.plex.player.PlexPlayer; import me.totalfreedom.plex.rank.enums.Rank; +import me.totalfreedom.plex.util.PlexLog; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.World; @@ -69,6 +70,7 @@ public abstract class PlexCommand extends Command implements TabExecutor, IPlexC if (!matches(label)) return false; if (this.sender == null) this.sender = new CommandSource(sender); + PlexLog.log(this.sender.getSender().getName()); if (commandSource == RequiredCommandSource.CONSOLE && sender instanceof Player) { send(tl("noPermissionInGame")); diff --git a/src/main/java/me/totalfreedom/plex/command/impl/NameHistoryCMD.java b/src/main/java/me/totalfreedom/plex/command/impl/NameHistoryCMD.java new file mode 100644 index 0000000..c886e65 --- /dev/null +++ b/src/main/java/me/totalfreedom/plex/command/impl/NameHistoryCMD.java @@ -0,0 +1,78 @@ +package me.totalfreedom.plex.command.impl; + +import me.totalfreedom.plex.command.PlexCommand; +import me.totalfreedom.plex.command.annotation.CommandParameters; +import me.totalfreedom.plex.command.annotation.CommandPermissions; +import me.totalfreedom.plex.command.exception.CommandArgumentException; +import me.totalfreedom.plex.command.source.CommandSource; +import me.totalfreedom.plex.rank.enums.Rank; +import me.totalfreedom.plex.util.PlexUtils; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.parser.ParseException; + +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.Comparator; +import java.util.List; + +import static me.totalfreedom.plex.util.PlexUtils.tl; + +@CommandParameters(description = "Get the name history of a player", usage = "/ ", aliases = "nh") +@CommandPermissions(level = Rank.OP) +public class NameHistoryCMD extends PlexCommand +{ + private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MM/dd/yyyy 'at' HH:mm:ss"); + + public NameHistoryCMD() + { + super("namehistory"); + } + + @Override + public void execute(CommandSource sender, String[] args) + { + if (args.length != 1) + throw new CommandArgumentException(); + String username = args[0]; + JSONArray array; + try + { + JSONObject profile = (JSONObject) PlexUtils.simpleGET("https://api.mojang.com/users/profiles/minecraft/" + username); + String uuid = (String) profile.get("id"); + array = (JSONArray) PlexUtils.simpleGET("https://api.mojang.com/user/profiles/" + uuid + "/names"); + } + catch (ParseException | IOException ex) + { + send(tl("nameHistoryFail", username)); + return; + } + + array.sort(Comparator.reverseOrder()); + + StringBuilder result = new StringBuilder() + .append(tl("nameHistoryTitle", username)) + .append("\n"); + for (Object o : array) + { + JSONObject object = (JSONObject) o; + Object changedToAt = object.get("changedToAt"); + if (changedToAt == null) + changedToAt = "O"; + else + changedToAt = DATE_FORMAT.format(changedToAt); + result.append(tl("nameHistoryBody", object.get("name"), changedToAt)) + .append("\n"); + } + send(result.toString()); + } + + @Override + public List onTabComplete(CommandSource sender, String[] args) + { + return args.length == 1 ? PlexUtils.getPlayerNameList() : null; + } +} \ No newline at end of file diff --git a/src/main/java/me/totalfreedom/plex/handlers/CommandHandler.java b/src/main/java/me/totalfreedom/plex/handlers/CommandHandler.java index c0f7a56..e86eb55 100644 --- a/src/main/java/me/totalfreedom/plex/handlers/CommandHandler.java +++ b/src/main/java/me/totalfreedom/plex/handlers/CommandHandler.java @@ -21,6 +21,7 @@ public class CommandHandler commands.add(new OpAllCMD()); commands.add(new OpCMD()); commands.add(new FreezeCMD()); + commands.add(new NameHistoryCMD()); PlexLog.log(String.format("Registered %s commands!", commands.size())); } diff --git a/src/main/java/me/totalfreedom/plex/util/PlexUtils.java b/src/main/java/me/totalfreedom/plex/util/PlexUtils.java index 585b23e..12916ba 100644 --- a/src/main/java/me/totalfreedom/plex/util/PlexUtils.java +++ b/src/main/java/me/totalfreedom/plex/util/PlexUtils.java @@ -1,9 +1,13 @@ package me.totalfreedom.plex.util; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; -import java.util.Objects; import java.util.stream.Collectors; import me.totalfreedom.plex.Plex; @@ -14,6 +18,8 @@ import org.bukkit.command.Command; import org.bukkit.command.PluginCommandYamlParser; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; public class PlexUtils { @@ -78,7 +84,7 @@ public class PlexUtils if (f == null) return ChatColor.RED + "No message"; for (Object object : objects) - f = f.replace("", String.valueOf(object)); + f = f.replaceFirst("", String.valueOf(object)); ChatColor base = getChatColorFromConfig(plugin.messages, ChatColor.GRAY, "baseColor"); ChatColor broadcast = getChatColorFromConfig(plugin.messages, ChatColor.AQUA, "broadcastColor"); ChatColor error = getChatColorFromConfig(plugin.messages, ChatColor.RED, "errorColor"); @@ -136,4 +142,19 @@ public class PlexUtils { Bukkit.broadcastMessage(s); } + + public static Object simpleGET(String url) throws IOException, ParseException + { + URL u = new URL(url); + HttpURLConnection connection = (HttpURLConnection) u.openConnection(); + connection.setRequestMethod("GET"); + BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); + String line; + StringBuilder content = new StringBuilder(); + while ((line = in.readLine()) != null) + content.append(line); + in.close(); + connection.disconnect(); + return new JSONParser().parse(content.toString()); + } } diff --git a/src/main/java/me/totalfreedom/plex/world/CustomWorld.java b/src/main/java/me/totalfreedom/plex/world/CustomWorld.java index 8f7c45c..732560e 100644 --- a/src/main/java/me/totalfreedom/plex/world/CustomWorld.java +++ b/src/main/java/me/totalfreedom/plex/world/CustomWorld.java @@ -1,7 +1,6 @@ package me.totalfreedom.plex.world; import me.totalfreedom.plex.Plex; -import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.WorldCreator; @@ -10,6 +9,7 @@ import org.bukkit.block.BlockState; import org.bukkit.block.Sign; import org.bukkit.generator.ChunkGenerator; +import java.io.File; import java.util.Objects; public class CustomWorld extends WorldCreator @@ -45,9 +45,9 @@ public class CustomWorld extends WorldCreator @Override public World generate() { - boolean addFeatures = Bukkit.getWorld(name) == null; + boolean existed = new File(name).exists(); World world = super.generate(); - if (addFeatures) + if (!existed) { Block block = world.getBlockAt(0, world.getHighestBlockYAt(0, 0) + 1, 0); block.setType(Material.OAK_SIGN); diff --git a/src/main/resources/messages.yml b/src/main/resources/messages.yml index f67ef76..c254fbb 100644 --- a/src/main/resources/messages.yml +++ b/src/main/resources/messages.yml @@ -43,4 +43,11 @@ noPermission: You cannot use this command! # 1: the login message (uncolored) of the rank required to use the command noPermissionRank: You must be at least rank to use this command! noPermissionInGame: You must be in console to use this command! -noPermissionConsole: You must be in-game to use this command! \ No newline at end of file +noPermissionConsole: You must be in-game to use this command! +# 1: the username of the name history +nameHistoryTitle: Name History of +# 1: a username of the found user +# 2: when the user changed to that username +nameHistoryBody: " - ()" +# 1: the username that failed +nameHistoryFail: Something went wrong while trying to retrieve name history of ! Try again later! \ No newline at end of file