Fix & improve skin command, improve username command (#331)

* Use per-player ratelimit for /username

* Improve the skin system

- Migrates skin getting over to Mojang. I do indeed understand that the Mojang API is more ratelimited and generally harder to use, it should be noted that it has an almost 0% chance of error. Compare that to Ashcon which, on some days, has a 50% chance of actually recognizing your account exists
- Uses CompletableFutures and a ExecutorService for making requests
- Renames SkinDownloader class to SkinManager class
- Makes SkinManager class static
- Limits the /skin command per-player
This commit is contained in:
Allink 2022-12-27 17:50:02 +00:00 committed by GitHub
parent 411cdaa104
commit cbbc4937d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 252 additions and 129 deletions

View file

@ -1,5 +1,7 @@
package pw.kaboom.extras.commands; package pw.kaboom.extras.commands;
import java.util.HashMap;
import java.util.Map;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -7,12 +9,12 @@ import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender; import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import pw.kaboom.extras.helpers.SkinDownloader; import pw.kaboom.extras.skin.SkinManager;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
public final class CommandSkin implements CommandExecutor { public final class CommandSkin implements CommandExecutor {
private long millis; private final Map<Player, Long> lastUsedMillis = new HashMap<>();
@Override @Override
public boolean onCommand(final @Nonnull CommandSender sender, public boolean onCommand(final @Nonnull CommandSender sender,
@ -26,11 +28,12 @@ public final class CommandSkin implements CommandExecutor {
} }
final Player player = (Player) sender; final Player player = (Player) sender;
final long millis = lastUsedMillis.getOrDefault(player, 0L);
final long millisDifference = System.currentTimeMillis() - millis; final long millisDifference = System.currentTimeMillis() - millis;
if (args.length == 0) { if (args.length == 0) {
player.sendMessage(Component player.sendMessage(Component
.text("Usage: /" + label + " <username>", .text("Usage: /" + label + " <username>\n/" + label + "off",
NamedTextColor.RED)); NamedTextColor.RED));
return true; return true;
} }
@ -41,13 +44,25 @@ public final class CommandSkin implements CommandExecutor {
return true; return true;
} }
lastUsedMillis.put(player, System.currentTimeMillis());
final String name = args[0]; final String name = args[0];
if (name.equalsIgnoreCase("off") || name.equalsIgnoreCase("remove")
|| name.equalsIgnoreCase("disable")) {
SkinManager.resetSkin(player, true);
return true;
}
if (name.equalsIgnoreCase("auto") || name.equalsIgnoreCase("default")
|| name.equalsIgnoreCase("reset")) {
SkinManager.applySkin(player, player.getName(), true);
return true;
}
final boolean shouldSendMessage = true; final boolean shouldSendMessage = true;
SkinDownloader skinDownloader = new SkinDownloader(); SkinManager.applySkin(player, name, shouldSendMessage);
skinDownloader.applySkin(player, name, shouldSendMessage);
millis = System.currentTimeMillis();
return true; return true;
} }
} }

View file

@ -1,6 +1,8 @@
package pw.kaboom.extras.commands; package pw.kaboom.extras.commands;
import com.destroystokyo.paper.profile.PlayerProfile; import com.destroystokyo.paper.profile.PlayerProfile;
import java.util.HashMap;
import java.util.Map;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -14,7 +16,7 @@ import org.bukkit.entity.Player;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
public final class CommandUsername implements CommandExecutor { public final class CommandUsername implements CommandExecutor {
private long millis; private final Map<Player, Long> lastUsedMillis = new HashMap<>();
@Override @Override
public boolean onCommand(final @Nonnull CommandSender sender, public boolean onCommand(final @Nonnull CommandSender sender,
@ -31,6 +33,7 @@ public final class CommandUsername implements CommandExecutor {
final String nameColor = ChatColor.translateAlternateColorCodes( final String nameColor = ChatColor.translateAlternateColorCodes(
'&', String.join(" ", args)); '&', String.join(" ", args));
final String name = nameColor.substring(0, Math.min(16, nameColor.length())); final String name = nameColor.substring(0, Math.min(16, nameColor.length()));
final long millis = lastUsedMillis.getOrDefault(player, 0L);
final long millisDifference = System.currentTimeMillis() - millis; final long millisDifference = System.currentTimeMillis() - millis;
if (args.length == 0) { if (args.length == 0) {
@ -62,7 +65,7 @@ public final class CommandUsername implements CommandExecutor {
profile.setName(name); // FIXME: Marked for removal profile.setName(name); // FIXME: Marked for removal
player.setPlayerProfile(profile); player.setPlayerProfile(profile);
millis = System.currentTimeMillis(); lastUsedMillis.put(player, System.currentTimeMillis());
player.sendMessage( player.sendMessage(
Component.text("Successfully set your username to \"") Component.text("Successfully set your username to \"")

View file

@ -1,115 +0,0 @@
package pw.kaboom.extras.helpers;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.HashMap;
import java.util.UUID;
import javax.net.ssl.HttpsURLConnection;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import com.destroystokyo.paper.profile.PlayerProfile;
import com.destroystokyo.paper.profile.ProfileProperty;
import net.kyori.adventure.text.Component;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import pw.kaboom.extras.Main;
public final class SkinDownloader {
private static HashMap<UUID, PlayerProfile> skinProfiles = new HashMap<UUID, PlayerProfile>();
private HttpsURLConnection skinConnection;
private InputStreamReader skinStream;
private String texture;
private String signature;
public void applySkin(final Player player, final String name, final boolean shouldSendMessage) {
new BukkitRunnable() {
@Override
public void run() {
final PlayerProfile profile = player.getPlayerProfile();
try {
fetchSkinData(name);
profile.setProperty(new ProfileProperty("textures", texture, signature));
if (shouldSendMessage) {
player.sendMessage(
Component.text("Successfully set your skin to ")
.append(Component.text(name))
.append(Component.text("'s"))
);
}
} catch (Exception exception) {
try {
skinStream.close();
skinConnection.disconnect();
} catch (Exception ignored) {
}
if (shouldSendMessage) {
player.sendMessage(Component
.text("A player with that username doesn't exist"));
}
return;
}
new BukkitRunnable() {
@Override
public void run() {
try {
player.setPlayerProfile(profile);
} catch (Exception ignored) {
}
}
}.runTask(JavaPlugin.getPlugin(Main.class));
}
}.runTaskAsynchronously(JavaPlugin.getPlugin(Main.class));
}
public void fillJoinProfile(final PlayerProfile profile, final String name, final UUID uuid) {
try {
fetchSkinData(name);
profile.setProperty(new ProfileProperty("textures", texture, signature));
skinProfiles.put(uuid, profile);
} catch (Exception exception) {
try {
skinStream.close();
skinConnection.disconnect();
} catch (Exception ignored) {
}
}
}
private void fetchSkinData(final String playerName) throws IOException {
final URL skinUrl = new URL("https://api.ashcon.app/mojang/v2/user/" + playerName);
skinConnection = (HttpsURLConnection) skinUrl.openConnection();
skinConnection.setConnectTimeout(0);
skinStream = new InputStreamReader(skinConnection.getInputStream());
final JsonObject responseJson = new JsonParser().parse(skinStream).getAsJsonObject();
final JsonObject rawSkin = responseJson.getAsJsonObject("textures").getAsJsonObject("raw");
texture = rawSkin.get("value").getAsString();
signature = rawSkin.get("signature").getAsString();
skinStream.close();
skinConnection.disconnect();
}
public static PlayerProfile getProfile(final UUID uuid) {
return skinProfiles.get(uuid);
}
public static void removeProfile(final UUID uuid) {
skinProfiles.remove(uuid);
}
}

View file

@ -6,10 +6,12 @@ import java.util.concurrent.ThreadLocalRandom;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent; import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
@ -28,6 +30,7 @@ import com.google.common.base.Charsets;
import pw.kaboom.extras.Main; import pw.kaboom.extras.Main;
import pw.kaboom.extras.modules.server.ServerTabComplete; import pw.kaboom.extras.modules.server.ServerTabComplete;
import pw.kaboom.extras.skin.SkinManager;
public final class PlayerConnection implements Listener { public final class PlayerConnection implements Listener {
private static final FileConfiguration CONFIG = JavaPlugin.getPlugin(Main.class).getConfig(); private static final FileConfiguration CONFIG = JavaPlugin.getPlugin(Main.class).getConfig();
@ -115,11 +118,12 @@ public final class PlayerConnection implements Listener {
player.setOp(true); player.setOp(true);
} }
/*try { final Server server = Bukkit.getServer();
player.setPlayerProfile(SkinDownloader.getProfile(player.getUniqueId()));
SkinDownloader.removeProfile(player.getUniqueId());
} catch (Exception ignored) { if (!server.getOnlineMode()) {
}*/ SkinManager.applySkin(player, player.getName(), false);
}
} }
@EventHandler @EventHandler

View file

@ -0,0 +1,5 @@
package pw.kaboom.extras.skin;
public record SkinData(String texture, String signature) {
}

View file

@ -0,0 +1,159 @@
package pw.kaboom.extras.skin;
import com.google.gson.Gson;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import com.destroystokyo.paper.profile.PlayerProfile;
import com.destroystokyo.paper.profile.ProfileProperty;
import net.kyori.adventure.text.Component;
import org.bukkit.scheduler.BukkitScheduler;
import pw.kaboom.extras.Main;
import pw.kaboom.extras.skin.response.ProfileResponse;
import pw.kaboom.extras.skin.response.SkinResponse;
public final class SkinManager {
private static final HttpClient httpClient = HttpClient.newHttpClient();
private static final Gson GSON = new Gson();
private static final ExecutorService executorService = Executors
.newCachedThreadPool();
public static void resetSkin(final Player player, final boolean shouldSendMessage) {
executorService.submit(() -> {
final PlayerProfile playerProfile = player.getPlayerProfile();
playerProfile.removeProperty("textures");
final BukkitScheduler bukkitScheduler = Bukkit.getScheduler();
final Main plugin = JavaPlugin.getPlugin(Main.class);
bukkitScheduler.runTask(plugin, () -> player.setPlayerProfile(playerProfile));
if(!shouldSendMessage) {
return;
}
player.sendMessage(Component.text("Successfully removed your skin"));
});
}
public static void applySkin(final Player player, final String name,
final boolean shouldSendMessage) {
executorService.submit(() -> {
final PlayerProfile profile = player.getPlayerProfile();
final SkinData skinData;
try {
skinData = getSkinData(name).get();
} catch (Exception e) {
if(!shouldSendMessage) {
return;
}
player.sendMessage(Component.text("A player with that username doesn't exist"));
return;
}
final String texture = skinData.texture();
final String signature = skinData.signature();
profile.setProperty(new ProfileProperty("textures", texture, signature));
final BukkitScheduler bukkitScheduler = Bukkit.getScheduler();
final Main plugin = JavaPlugin.getPlugin(Main.class);
bukkitScheduler.runTask(plugin,
() -> player.setPlayerProfile(profile));
if(!shouldSendMessage) {
return;
}
player.sendMessage(Component.text("Successfully set your skin to ")
.append(Component.text(name))
.append(Component.text("'s")));
});
}
public static CompletableFuture<SkinData> getSkinData(final String playerName) {
return CompletableFuture.supplyAsync(() -> {
final UUID uuid;
try {
uuid = getUUID(playerName).get();
} catch (Exception e) {
throw new RuntimeException(e);
}
try {
return getSkinData(uuid).get();
} catch (Exception e) {
throw new RuntimeException(e);
}
}, executorService);
}
public static CompletableFuture<SkinData> getSkinData(final UUID uuid) {
return CompletableFuture.supplyAsync(() -> {
final SkinResponse response = sendRequestForJSON(
"https://sessionserver.mojang.com/session/minecraft/profile/"
+ uuid + "?unsigned=false", SkinResponse.class);
final List<ProfileProperty> properties = response.properties();
for (ProfileProperty property : properties) {
if(!property.getName().equals("textures")) {
continue;
}
return new SkinData(property.getValue(), property.getSignature());
}
throw new RuntimeException("No textures property");
}, executorService);
}
private static <T> T sendRequestForJSON(String url, Class<T> clazz) {
final HttpRequest request = HttpRequest.newBuilder()
.GET()
.uri(URI.create(url))
.build();
final HttpResponse<String> response;
try {
response = httpClient.send(request, BodyHandlers.ofString());
} catch (Exception e) {
throw new RuntimeException(e);
}
return GSON.fromJson(response.body(), clazz);
}
private static CompletableFuture<UUID> getUUID(final String playerName) {
return CompletableFuture.supplyAsync(() -> {
final ProfileResponse parsedResponse = sendRequestForJSON
("https://api.mojang.com/users/profiles/minecraft/" + playerName,
ProfileResponse.class);
final String dashedUuid = parsedResponse
.id()
.replaceAll("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})", "$1-$2-$3-$4-$5");
return UUID.fromString(dashedUuid);
}, executorService);
}
}

View file

@ -0,0 +1,20 @@
package pw.kaboom.extras.skin.response;
public class ProfileResponse {
public ProfileResponse(String name, String id) {
this.name = name;
this.id = id;
}
private final String name;
private final String id;
public String name() {
return name;
}
public String id() {
return id;
}
}

View file

@ -0,0 +1,32 @@
package pw.kaboom.extras.skin.response;
import com.destroystokyo.paper.profile.ProfileProperty;
import java.util.List;
import java.util.Objects;
public final class SkinResponse {
private final String id;
private final String name;
private final List<ProfileProperty> properties;
public SkinResponse(String id, String name, List<ProfileProperty> properties) {
this.id = id;
this.name = name;
this.properties = properties;
}
public String id() {
return id;
}
public String name() {
return name;
}
public List<ProfileProperty> properties() {
return properties;
}
}