From d66630460e24337d177683a99002a4167f1fac96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Pani=C4=87?= Date: Sun, 7 Apr 2019 23:33:20 +0200 Subject: Added UUID support in commands --- src/main/java/com/redstoner/bungeeBans/Util.java | 66 +++++++++++++++++++++- .../redstoner/bungeeBans/commands/BanCommand.java | 39 ++++++++++--- .../bungeeBans/commands/GetBanCommand.java | 37 +++++++++--- .../bungeeBans/commands/UnbanCommand.java | 37 +++++++++--- 4 files changed, 151 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/redstoner/bungeeBans/Util.java b/src/main/java/com/redstoner/bungeeBans/Util.java index b529794..001e180 100644 --- a/src/main/java/com/redstoner/bungeeBans/Util.java +++ b/src/main/java/com/redstoner/bungeeBans/Util.java @@ -1,19 +1,36 @@ package com.redstoner.bungeeBans; +import com.google.gson.Gson; import com.mojang.api.profiles.HttpProfileRepository; import com.mojang.api.profiles.Profile; +import java.io.IOException; +import java.net.URL; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; +import java.util.Date; +import java.util.Scanner; import java.util.regex.Pattern; public class Util { + private static final Gson gson = new Gson(); + private static final HttpProfileRepository profileRepo = new HttpProfileRepository("minecraft"); - private static final Pattern ipValidity = Pattern.compile( - "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$"); + + private static final String undashedUuidRegex = "([0-9a-fA-F]{8})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]+)"; + private static final String dashedUuidRegex = "([0-9a-fA-F]{8})-([0-9a-fA-F]{4})-([0-9a-fA-F]{4})-([0-9a-fA-F]{4})-([0-9a-fA-F]+)"; + + private static final String ipRegex = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$"; + + private static final Pattern uuidValidity = Pattern.compile(dashedUuidRegex); + private static final Pattern ipValidity = Pattern.compile(ipRegex); public static String dashUUID(String uuid) { - return uuid.replaceFirst("([0-9a-fA-F]{8})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]+)", "$1-$2-$3-$4-$5"); + return uuid.replaceFirst(undashedUuidRegex, "$1-$2-$3-$4-$5"); + } + + public static String trimUUID(String uuid) { + return uuid.replaceAll("-", ""); } public static String getNow() { @@ -24,7 +41,50 @@ public class Util { return profileRepo.findProfilesByNames(names); } + public static boolean validateUUID(String uuid) { + return uuidValidity.matcher(uuid).matches(); + } + public static boolean validateIP(String ip) { return ipValidity.matcher(ip).matches(); } + + public static NameChange[] findNameChangesByUUID(String uuid) throws MojangException { + NameChange[] names = null; + + try { + Scanner jsonScanner = new Scanner( + new URL("https://api.mojang.com/user/profiles/" + trimUUID(uuid) + "/names").openConnection().getInputStream(), + "UTF-8" + ); + + names = gson.fromJson(jsonScanner.next(), NameChange[].class); + jsonScanner.close(); + } catch (IOException e) { + if (e.getMessage().contains("HTTP response code: 429")) { + throw new MojangException("Mojang api request limit reached! Please try again later!"); + } else { + throw new MojangException(e.getMessage()); + } + } catch (Exception e) { + throw new MojangException("Invalid UUID!"); + } + + return names; + } + + public static class MojangException extends Exception { + MojangException(String message) { + super(message); + } + } + + public static class NameChange { + public String name; + public long changedToAt; + + public Date getChangeDate() { + return new Date(changedToAt); + } + } } diff --git a/src/main/java/com/redstoner/bungeeBans/commands/BanCommand.java b/src/main/java/com/redstoner/bungeeBans/commands/BanCommand.java index 0527893..d4f54ce 100644 --- a/src/main/java/com/redstoner/bungeeBans/commands/BanCommand.java +++ b/src/main/java/com/redstoner/bungeeBans/commands/BanCommand.java @@ -35,7 +35,7 @@ public class BanCommand extends Command { sender.sendMessage( new ComponentBuilder(ChatColor.RED + "Usage: ") .append(ChatColor.AQUA + "/ban ") - .append(ChatColor.GOLD + " ") + .append(ChatColor.GOLD + " ") .append(ChatColor.YELLOW + "[reason]") .create() ); @@ -46,16 +46,37 @@ public class BanCommand extends Command { break; } - Profile[] profiles = Util.findProfilesByNames(args[0]); + String uuid; + String name; + String expires = "forever"; //TODO: expiry option - if (profiles.length != 1) { - sender.sendMessage(new TextComponent(ChatColor.RED + "Invalid name!")); - return; - } + if (Util.validateUUID(args[0])) { + uuid = args[0]; - String uuid = Util.dashUUID(profiles[0].getId()); - String name = profiles[0].getName(); - String expires = "forever"; //TODO: expiry option + try { + Util.NameChange[] nameChanges = Util.findNameChangesByUUID(uuid); + + if (nameChanges.length == 0) { + sender.sendMessage(new TextComponent(ChatColor.RED + "Invalid UUID!")); + return; + } + + name = nameChanges[nameChanges.length - 1].name; + } catch (Util.MojangException e) { + sender.sendMessage(new TextComponent(ChatColor.RED + e.getMessage())); + return; + } + } else { + Profile[] profiles = Util.findProfilesByNames(args[0]); + + if (profiles.length != 1) { + sender.sendMessage(new TextComponent(ChatColor.RED + "Invalid name!")); + return; + } + + uuid = Util.dashUUID(profiles[0].getId()); + name = profiles[0].getName(); + } if (bm.getBan(uuid) != null) { sender.sendMessage(new TextComponent(ChatColor.RED + "That player is already banned!")); diff --git a/src/main/java/com/redstoner/bungeeBans/commands/GetBanCommand.java b/src/main/java/com/redstoner/bungeeBans/commands/GetBanCommand.java index 5ff213a..f63fd74 100644 --- a/src/main/java/com/redstoner/bungeeBans/commands/GetBanCommand.java +++ b/src/main/java/com/redstoner/bungeeBans/commands/GetBanCommand.java @@ -26,22 +26,43 @@ public class GetBanCommand extends Command { new ComponentBuilder(ChatColor.RED + "Invalid command! ") .append("Usage: ") .append(ChatColor.AQUA + "/getban ") - .append(ChatColor.GOLD + " ") + .append(ChatColor.GOLD + " ") .create() ); return; } - Profile[] profiles = Util.findProfilesByNames(args[0]); + String uuid; + String name; - if (profiles.length != 1) { - sender.sendMessage(new TextComponent(ChatColor.RED + "Invalid name!")); - return; - } + if (Util.validateUUID(args[0])) { + uuid = args[0]; + + try { + Util.NameChange[] nameChanges = Util.findNameChangesByUUID(uuid); + + if (nameChanges.length == 0) { + sender.sendMessage(new TextComponent(ChatColor.RED + "Invalid UUID!")); + return; + } - String uuid = Util.dashUUID(profiles[0].getId()); - String name = profiles[0].getName(); + name = nameChanges[nameChanges.length - 1].name; + } catch (Util.MojangException e) { + sender.sendMessage(new TextComponent(ChatColor.RED + e.getMessage())); + return; + } + } else { + Profile[] profiles = Util.findProfilesByNames(args[0]); + + if (profiles.length != 1) { + sender.sendMessage(new TextComponent(ChatColor.RED + "Invalid name!")); + return; + } + + uuid = Util.dashUUID(profiles[0].getId()); + name = profiles[0].getName(); + } PlayerBan ban = bm.getBan(uuid); diff --git a/src/main/java/com/redstoner/bungeeBans/commands/UnbanCommand.java b/src/main/java/com/redstoner/bungeeBans/commands/UnbanCommand.java index 809c7d2..b38469b 100644 --- a/src/main/java/com/redstoner/bungeeBans/commands/UnbanCommand.java +++ b/src/main/java/com/redstoner/bungeeBans/commands/UnbanCommand.java @@ -27,22 +27,43 @@ public class UnbanCommand extends Command { sender.sendMessage( new ComponentBuilder(ChatColor.RED + "Usage: ") .append(ChatColor.AQUA + "/unban ") - .append(ChatColor.GOLD + " ") + .append(ChatColor.GOLD + " ") .create() ); return; } - Profile[] profiles = Util.findProfilesByNames(args[0]); + String uuid; + String name; - if (profiles.length != 1) { - sender.sendMessage(new TextComponent(ChatColor.RED + "Invalid name!")); - return; - } + if (Util.validateUUID(args[0])) { + uuid = args[0]; + + try { + Util.NameChange[] nameChanges = Util.findNameChangesByUUID(uuid); + + if (nameChanges.length == 0) { + sender.sendMessage(new TextComponent(ChatColor.RED + "Invalid UUID!")); + return; + } - String uuid = Util.dashUUID(profiles[0].getId()); - String name = profiles[0].getName(); + name = nameChanges[nameChanges.length - 1].name; + } catch (Util.MojangException e) { + sender.sendMessage(new TextComponent(ChatColor.RED + e.getMessage())); + return; + } + } else { + Profile[] profiles = Util.findProfilesByNames(args[0]); + + if (profiles.length != 1) { + sender.sendMessage(new TextComponent(ChatColor.RED + "Invalid name!")); + return; + } + + uuid = Util.dashUUID(profiles[0].getId()); + name = profiles[0].getName(); + } PlayerBan ban = bm.getBan(uuid); -- cgit v1.2.3