0

Added UUID support in commands

This commit is contained in:
David Panić
2019-04-07 23:33:20 +02:00
parent aa6c22df1c
commit d66630460e
4 changed files with 152 additions and 29 deletions

View File

@@ -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);
}
}
}

View File

@@ -35,7 +35,7 @@ public class BanCommand extends Command {
sender.sendMessage(
new ComponentBuilder(ChatColor.RED + "Usage: ")
.append(ChatColor.AQUA + "/ban ")
.append(ChatColor.GOLD + "<username> ")
.append(ChatColor.GOLD + "<username/uuid> ")
.append(ChatColor.YELLOW + "[reason]")
.create()
);
@@ -46,17 +46,38 @@ public class BanCommand extends Command {
break;
}
Profile[] profiles = Util.findProfilesByNames(args[0]);
if (profiles.length != 1) {
sender.sendMessage(new TextComponent(ChatColor.RED + "Invalid name!"));
return;
}
String uuid = Util.dashUUID(profiles[0].getId());
String name = profiles[0].getName();
String uuid;
String name;
String expires = "forever"; //TODO: expiry option
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;
}
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!"));
return;

View File

@@ -26,23 +26,44 @@ public class GetBanCommand extends Command {
new ComponentBuilder(ChatColor.RED + "Invalid command! ")
.append("Usage: ")
.append(ChatColor.AQUA + "/getban ")
.append(ChatColor.GOLD + "<username> ")
.append(ChatColor.GOLD + "<username/uuid> ")
.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;
}
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();
}
String uuid = Util.dashUUID(profiles[0].getId());
String name = profiles[0].getName();
PlayerBan ban = bm.getBan(uuid);
if (ban == null) {

View File

@@ -27,23 +27,44 @@ public class UnbanCommand extends Command {
sender.sendMessage(
new ComponentBuilder(ChatColor.RED + "Usage: ")
.append(ChatColor.AQUA + "/unban ")
.append(ChatColor.GOLD + "<username> ")
.append(ChatColor.GOLD + "<username/uuid> ")
.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;
}
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();
}
String uuid = Util.dashUUID(profiles[0].getId());
String name = profiles[0].getName();
PlayerBan ban = bm.getBan(uuid);
if (ban == null) {