name history

i still need to test bcuz i dont have an alt
This commit is contained in:
Super_ 2020-11-05 08:40:48 -05:00
parent 52be58872e
commit 8428e2a6d3
7 changed files with 117 additions and 6 deletions

View File

@ -17,6 +17,8 @@ import me.totalfreedom.plex.util.PlexUtils;
import me.totalfreedom.plex.world.CustomWorld; import me.totalfreedom.plex.world.CustomWorld;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
@Getter @Getter
@Setter @Setter
public class Plex extends JavaPlugin public class Plex extends JavaPlugin

View File

@ -12,6 +12,7 @@ import me.totalfreedom.plex.command.source.CommandSource;
import me.totalfreedom.plex.command.source.RequiredCommandSource; import me.totalfreedom.plex.command.source.RequiredCommandSource;
import me.totalfreedom.plex.player.PlexPlayer; import me.totalfreedom.plex.player.PlexPlayer;
import me.totalfreedom.plex.rank.enums.Rank; import me.totalfreedom.plex.rank.enums.Rank;
import me.totalfreedom.plex.util.PlexLog;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.World; import org.bukkit.World;
@ -69,6 +70,7 @@ public abstract class PlexCommand extends Command implements TabExecutor, IPlexC
if (!matches(label)) return false; if (!matches(label)) return false;
if (this.sender == null) if (this.sender == null)
this.sender = new CommandSource(sender); this.sender = new CommandSource(sender);
PlexLog.log(this.sender.getSender().getName());
if (commandSource == RequiredCommandSource.CONSOLE && sender instanceof Player) if (commandSource == RequiredCommandSource.CONSOLE && sender instanceof Player)
{ {
send(tl("noPermissionInGame")); send(tl("noPermissionInGame"));

View File

@ -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 = "/<command> <player>", 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<String> onTabComplete(CommandSource sender, String[] args)
{
return args.length == 1 ? PlexUtils.getPlayerNameList() : null;
}
}

View File

@ -21,6 +21,7 @@ public class CommandHandler
commands.add(new OpAllCMD()); commands.add(new OpAllCMD());
commands.add(new OpCMD()); commands.add(new OpCMD());
commands.add(new FreezeCMD()); commands.add(new FreezeCMD());
commands.add(new NameHistoryCMD());
PlexLog.log(String.format("Registered %s commands!", commands.size())); PlexLog.log(String.format("Registered %s commands!", commands.size()));
} }

View File

@ -1,9 +1,13 @@
package me.totalfreedom.plex.util; 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.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import me.totalfreedom.plex.Plex; import me.totalfreedom.plex.Plex;
@ -14,6 +18,8 @@ import org.bukkit.command.Command;
import org.bukkit.command.PluginCommandYamlParser; import org.bukkit.command.PluginCommandYamlParser;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class PlexUtils public class PlexUtils
{ {
@ -78,7 +84,7 @@ public class PlexUtils
if (f == null) if (f == null)
return ChatColor.RED + "No message"; return ChatColor.RED + "No message";
for (Object object : objects) for (Object object : objects)
f = f.replace("<v>", String.valueOf(object)); f = f.replaceFirst("<v>", String.valueOf(object));
ChatColor base = getChatColorFromConfig(plugin.messages, ChatColor.GRAY, "baseColor"); ChatColor base = getChatColorFromConfig(plugin.messages, ChatColor.GRAY, "baseColor");
ChatColor broadcast = getChatColorFromConfig(plugin.messages, ChatColor.AQUA, "broadcastColor"); ChatColor broadcast = getChatColorFromConfig(plugin.messages, ChatColor.AQUA, "broadcastColor");
ChatColor error = getChatColorFromConfig(plugin.messages, ChatColor.RED, "errorColor"); ChatColor error = getChatColorFromConfig(plugin.messages, ChatColor.RED, "errorColor");
@ -136,4 +142,19 @@ public class PlexUtils
{ {
Bukkit.broadcastMessage(s); 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());
}
} }

View File

@ -1,7 +1,6 @@
package me.totalfreedom.plex.world; package me.totalfreedom.plex.world;
import me.totalfreedom.plex.Plex; import me.totalfreedom.plex.Plex;
import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.WorldCreator; import org.bukkit.WorldCreator;
@ -10,6 +9,7 @@ import org.bukkit.block.BlockState;
import org.bukkit.block.Sign; import org.bukkit.block.Sign;
import org.bukkit.generator.ChunkGenerator; import org.bukkit.generator.ChunkGenerator;
import java.io.File;
import java.util.Objects; import java.util.Objects;
public class CustomWorld extends WorldCreator public class CustomWorld extends WorldCreator
@ -45,9 +45,9 @@ public class CustomWorld extends WorldCreator
@Override @Override
public World generate() public World generate()
{ {
boolean addFeatures = Bukkit.getWorld(name) == null; boolean existed = new File(name).exists();
World world = super.generate(); World world = super.generate();
if (addFeatures) if (!existed)
{ {
Block block = world.getBlockAt(0, world.getHighestBlockYAt(0, 0) + 1, 0); Block block = world.getBlockAt(0, world.getHighestBlockYAt(0, 0) + 1, 0);
block.setType(Material.OAK_SIGN); block.setType(Material.OAK_SIGN);

View File

@ -43,4 +43,11 @@ noPermission: <e>You cannot use this command!
# 1: the login message (uncolored) of the rank required to use the command # 1: the login message (uncolored) of the rank required to use the command
noPermissionRank: <e>You must be at least <v> rank to use this command! noPermissionRank: <e>You must be at least <v> rank to use this command!
noPermissionInGame: <e>You must be in console to use this command! noPermissionInGame: <e>You must be in console to use this command!
noPermissionConsole: <e>You must be in-game to use this command! noPermissionConsole: <e>You must be in-game to use this command!
# 1: the username of the name history
nameHistoryTitle: Name History of <v>
# 1: a username of the found user
# 2: when the user changed to that username
nameHistoryBody: " - <v> (<v>)"
# 1: the username that failed
nameHistoryFail: <e>Something went wrong while trying to retrieve name history of <v>! Try again later!