From c7402b34eef66e6586a1798eeb8e62609c2379b2 Mon Sep 17 00:00:00 2001 From: Zeanon Date: Mon, 26 Apr 2021 20:24:56 +0200 Subject: [PATCH] You can now ban and mute Users who never joined the server, HOOZAH --- .../bungeecore/commands/BanCommand.java | 2 +- .../bungeecore/commands/BasicCommand.java | 9 +++- .../bungeecore/commands/MuteCommand.java | 2 +- .../bungeecore/commands/UnbanCommand.java | 2 +- .../steamwar/bungeecore/sql/SteamwarUser.java | 46 ++++++++++++++++++- 5 files changed, 55 insertions(+), 6 deletions(-) diff --git a/src/de/steamwar/bungeecore/commands/BanCommand.java b/src/de/steamwar/bungeecore/commands/BanCommand.java index 293b3f1..6b3bcea 100644 --- a/src/de/steamwar/bungeecore/commands/BanCommand.java +++ b/src/de/steamwar/bungeecore/commands/BanCommand.java @@ -42,7 +42,7 @@ public class BanCommand extends BasicCommand { return; } - SteamwarUser target = user(sender, args[0]); + SteamwarUser target = unsafeUser(sender, args[0]); if(target == null) return; diff --git a/src/de/steamwar/bungeecore/commands/BasicCommand.java b/src/de/steamwar/bungeecore/commands/BasicCommand.java index 9cdf19a..d4b9c0a 100644 --- a/src/de/steamwar/bungeecore/commands/BasicCommand.java +++ b/src/de/steamwar/bungeecore/commands/BasicCommand.java @@ -53,10 +53,17 @@ abstract class BasicCommand extends Command implements TabExecutor { return new ArrayList<>(); } - protected SteamwarUser user(CommandSender sender, String arg){ + protected SteamwarUser existingUser(CommandSender sender, String arg){ SteamwarUser target = SteamwarUser.get(arg); if(target == null) Message.send("UNKNOWN_PLAYER", sender); return target; } + + protected SteamwarUser unsafeUser(CommandSender sender, String arg){ + SteamwarUser target = SteamwarUser.getOrCreateOfflinePlayer(arg); + if(target == null) + Message.send("UNKNOWN_PLAYER", sender); + return target; + } } diff --git a/src/de/steamwar/bungeecore/commands/MuteCommand.java b/src/de/steamwar/bungeecore/commands/MuteCommand.java index ea77888..9ecfde1 100644 --- a/src/de/steamwar/bungeecore/commands/MuteCommand.java +++ b/src/de/steamwar/bungeecore/commands/MuteCommand.java @@ -38,7 +38,7 @@ public class MuteCommand extends BasicCommand { return; } - SteamwarUser target = user(sender, args[0]); + SteamwarUser target = unsafeUser(sender, args[0]); if(target == null) return; diff --git a/src/de/steamwar/bungeecore/commands/UnbanCommand.java b/src/de/steamwar/bungeecore/commands/UnbanCommand.java index 58c0a6f..176029a 100644 --- a/src/de/steamwar/bungeecore/commands/UnbanCommand.java +++ b/src/de/steamwar/bungeecore/commands/UnbanCommand.java @@ -39,7 +39,7 @@ public class UnbanCommand extends BasicCommand { return; } - SteamwarUser target = user(sender, args[0]); + SteamwarUser target = existingUser(sender, args[0]); if(target == null) return; diff --git a/src/de/steamwar/bungeecore/sql/SteamwarUser.java b/src/de/steamwar/bungeecore/sql/SteamwarUser.java index bf11939..d221c8b 100644 --- a/src/de/steamwar/bungeecore/sql/SteamwarUser.java +++ b/src/de/steamwar/bungeecore/sql/SteamwarUser.java @@ -19,10 +19,15 @@ package de.steamwar.bungeecore.sql; +import com.google.gson.JsonParser; import de.steamwar.bungeecore.BungeeCore; import de.steamwar.bungeecore.Message; import de.steamwar.bungeecore.commands.WebregisterCommand; import de.steamwar.bungeecore.listeners.ConnectionListener; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Scanner; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.connection.PendingConnection; @@ -38,6 +43,7 @@ import java.util.HashMap; import java.util.Map; import java.util.UUID; + public class SteamwarUser { private final int id; private final UUID uuid; @@ -51,6 +57,8 @@ public class SteamwarUser { private static final Map usersByUUID = new HashMap<>(); private static final Map usersById = new HashMap<>(); private static final InetAddress LIXFEL_DE; + private static final String API_URL = "https://api.mojang.com/users/profiles/minecraft/"; + private static final JsonParser jsonParser = new JsonParser(); static { try { @@ -85,8 +93,7 @@ public class SteamwarUser { user.userName = userName; } }else{ - SQL.update("INSERT INTO UserData (UUID, UserName, UserGroup) VALUES (?, ?, 'Member')", connection.getUniqueId().toString(), connection.getName()); - user = dbInit(SQL.select("SELECT * FROM UserData WHERE UUID = ?", connection.getUniqueId().toString())); + user = SteamwarUser.createUserInDatabase(connection.getUniqueId(), connection.getName()); if(user == null) throw new SecurityException("user == null"); ConnectionListener.newPlayer(user.uuid); @@ -97,6 +104,25 @@ public class SteamwarUser { return user; } + public static SteamwarUser getOrCreateOfflinePlayer(String name){ + SteamwarUser user = SteamwarUser.get(name); + if (user != null) { + return user; + } + + UUID uuid = SteamwarUser.loadUUID(name); + if (uuid == null) { + return null; + } + + return SteamwarUser.createUserInDatabase(uuid, name); + } + + private static SteamwarUser createUserInDatabase(UUID uuid, String name) { + SQL.update("INSERT INTO UserData (UUID, UserName, UserGroup) VALUES (?, ?, 'Member')", uuid.toString(), name); + return dbInit(SQL.select("SELECT * FROM UserData WHERE UUID = ?", uuid.toString())); + } + public static SteamwarUser get(String userName){ userName = userName.toLowerCase(); if(usersByName.containsKey(userName)) @@ -126,6 +152,22 @@ public class SteamwarUser { usersByUUID.clear(); } + public static UUID loadUUID(String playerName) { + try { + final URL url = new URL(API_URL + playerName); + return getUniqueIdFromString(jsonParser.parse(new Scanner(url.openConnection().getInputStream()).nextLine()).getAsJsonObject().get("id").getAsString()); + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + private static UUID getUniqueIdFromString(String uuid) { + return UUID.fromString(uuid.replaceFirst("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})", "$1-$2-$3-$4-$5")); + } + public void setTeam(int team){ this.team = team; SQL.update("Update UserData SET Team = ? WHERE id = ?", team, id); -- 2.39.2