From a998fa8321114126c83b82e2f3ac176732269bba Mon Sep 17 00:00:00 2001 From: mathiascode Date: Mon, 2 Dec 2019 19:07:05 +0200 Subject: [PATCH] Reduce code duplication --- .../kaboom/extras/commands/CommandSkin.java | 34 ++--------- .../extras/commands/CommandUsername.java | 44 ++------------ .../kaboom/extras/helpers/SkinDownloader.java | 58 ++++++++++++++++--- .../modules/player/PlayerConnection.java | 31 ++-------- 4 files changed, 65 insertions(+), 102 deletions(-) diff --git a/src/main/java/pw/kaboom/extras/commands/CommandSkin.java b/src/main/java/pw/kaboom/extras/commands/CommandSkin.java index b612882..6bcfa05 100644 --- a/src/main/java/pw/kaboom/extras/commands/CommandSkin.java +++ b/src/main/java/pw/kaboom/extras/commands/CommandSkin.java @@ -9,13 +9,6 @@ import org.bukkit.command.ConsoleCommandSender; 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; - class CommandSkin implements CommandExecutor { public boolean onCommand(CommandSender sender, Command command, String label, final String[] args) { if (sender instanceof ConsoleCommandSender) { @@ -26,29 +19,12 @@ class CommandSkin implements CommandExecutor { if (args.length == 0) { player.sendMessage(ChatColor.RED + "Usage: /" + label + " "); } else if (!Main.skinInProgress.contains(player.getUniqueId())) { - Main.skinInProgress.add(player.getUniqueId()); + final String name = args[0]; + final boolean shouldChangeUsername = false; + final boolean shouldSendMessage = true; - new BukkitRunnable() { - public void run() { - SkinDownloader skinDownloader = new SkinDownloader(); - if (skinDownloader.fetchSkinData(args[0])) { - final PlayerProfile profile = player.getPlayerProfile(); - final String texture = skinDownloader.getTexture(); - final String signature = skinDownloader.getSignature(); - - profile.setProperty(new ProfileProperty("textures", texture, signature)); - - player.sendMessage("Successfully set your skin to " + args[0] + "'s"); - - new BukkitRunnable() { - public void run() { - player.setPlayerProfile(profile); - Main.skinInProgress.remove(player.getUniqueId()); - } - }.runTask(JavaPlugin.getPlugin(Main.class)); - } - } - }.runTaskAsynchronously(JavaPlugin.getPlugin(Main.class)); + SkinDownloader skinDownloader = new SkinDownloader(); + skinDownloader.applySkin(player, name, shouldChangeUsername, shouldSendMessage); } else { player.sendMessage("You are already applying a skin. Please wait a few seconds."); } diff --git a/src/main/java/pw/kaboom/extras/commands/CommandUsername.java b/src/main/java/pw/kaboom/extras/commands/CommandUsername.java index 82b55d4..6ffbfc4 100644 --- a/src/main/java/pw/kaboom/extras/commands/CommandUsername.java +++ b/src/main/java/pw/kaboom/extras/commands/CommandUsername.java @@ -9,16 +9,6 @@ import org.bukkit.command.ConsoleCommandSender; 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 com.google.gson.JsonObject; -import com.google.gson.JsonParser; - class CommandUsername implements CommandExecutor { public boolean onCommand(CommandSender sender, Command command, String label, final String[] args) { if (sender instanceof ConsoleCommandSender) { @@ -29,35 +19,13 @@ class CommandUsername implements CommandExecutor { if (args.length == 0) { player.sendMessage(ChatColor.RED + "Usage: /" + label + " "); } else if (!Main.usernameInProgress.contains(player.getUniqueId())) { - Main.usernameInProgress.add(player.getUniqueId()); - final String nameColor = ChatColor.translateAlternateColorCodes('&', String.join(" ", args)); - final String nameShort = nameColor.substring(0, Math.min(16, nameColor.length())); - - new BukkitRunnable() { - public void run() { - final PlayerProfile profile = player.getPlayerProfile(); - profile.setName(nameShort); - - SkinDownloader skinDownloader = new SkinDownloader(); - - if (skinDownloader.fetchSkinData(args[0])) { - final String texture = skinDownloader.getTexture(); - final String signature = skinDownloader.getSignature(); - - profile.setProperty(new ProfileProperty("textures", texture, signature)); - } - - player.sendMessage("Successfully set your username to \"" + nameShort + "\""); - - new BukkitRunnable() { - public void run() { - player.setPlayerProfile(profile); - Main.usernameInProgress.remove(player.getUniqueId()); - } - }.runTask(JavaPlugin.getPlugin(Main.class)); - } - }.runTaskAsynchronously(JavaPlugin.getPlugin(Main.class)); + final String name = nameColor.substring(0, Math.min(16, nameColor.length())); + final boolean shouldChangeUsername = true; + final boolean shouldSendMessage = true; + + SkinDownloader skinDownloader = new SkinDownloader(); + skinDownloader.applySkin(player, name, shouldChangeUsername, shouldSendMessage); } else { player.sendMessage("Your username is already being changed. Please wait a few seconds."); } diff --git a/src/main/java/pw/kaboom/extras/helpers/SkinDownloader.java b/src/main/java/pw/kaboom/extras/helpers/SkinDownloader.java index 61c8412..0167151 100644 --- a/src/main/java/pw/kaboom/extras/helpers/SkinDownloader.java +++ b/src/main/java/pw/kaboom/extras/helpers/SkinDownloader.java @@ -4,6 +4,15 @@ import java.io.InputStreamReader; import java.net.URL; 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 com.google.gson.JsonObject; import com.google.gson.JsonParser; @@ -11,7 +20,46 @@ class SkinDownloader { private String texture; private String signature; - public boolean fetchSkinData(String playerName) { + public void applySkin(Player player, String name, boolean shouldChangeName, boolean shouldSendMessage) { + Main.usernameInProgress.add(player.getUniqueId()); + + new BukkitRunnable() { + public void run() { + final PlayerProfile profile = player.getPlayerProfile(); + + if (shouldChangeName && shouldSendMessage) { + profile.setName(name); + player.sendMessage("Changing your username. Please wait..."); + } + + if (fetchSkinData(name)) { + profile.setProperty(new ProfileProperty("textures", texture, signature)); + if (!shouldChangeName && shouldSendMessage) { + player.sendMessage("Successfully set your skin to " + name + "'s"); + } + } else if (!shouldChangeName && shouldSendMessage) { + player.sendMessage("A player with that username doesn't exist"); + Main.usernameInProgress.remove(player.getUniqueId()); + return; + } + + new BukkitRunnable() { + public void run() { + if (player.isOnline()) { + player.setPlayerProfile(profile); + + if (shouldChangeName && shouldSendMessage) { + player.sendMessage("Successfully set your username to \"" + name + "\""); + } + } + Main.usernameInProgress.remove(player.getUniqueId()); + } + }.runTask(JavaPlugin.getPlugin(Main.class)); + } + }.runTaskAsynchronously(JavaPlugin.getPlugin(Main.class)); + } + + private boolean fetchSkinData(String playerName) { try { final URL skinUrl = new URL("https://api.ashcon.app/mojang/v2/user/" + playerName); final HttpsURLConnection skinConnection = (HttpsURLConnection) skinUrl.openConnection(); @@ -36,12 +84,4 @@ class SkinDownloader { } return false; } - - public String getSignature() { - return signature; - } - - public String getTexture() { - return texture; - } } \ No newline at end of file diff --git a/src/main/java/pw/kaboom/extras/modules/player/PlayerConnection.java b/src/main/java/pw/kaboom/extras/modules/player/PlayerConnection.java index 77c5149..1aa0eeb 100644 --- a/src/main/java/pw/kaboom/extras/modules/player/PlayerConnection.java +++ b/src/main/java/pw/kaboom/extras/modules/player/PlayerConnection.java @@ -27,15 +27,8 @@ import org.bukkit.inventory.meta.BannerMeta; import org.bukkit.plugin.java.JavaPlugin; -import org.bukkit.scheduler.BukkitRunnable; - -import pw.kaboom.extras.SkinDownloader; - import com.destroystokyo.paper.event.player.PlayerConnectionCloseEvent; -import com.destroystokyo.paper.profile.PlayerProfile; -import com.destroystokyo.paper.profile.ProfileProperty; - class PlayerConnection implements Listener { @EventHandler void onAsyncPlayerPreLogin(AsyncPlayerPreLoginEvent event) { @@ -119,25 +112,11 @@ class PlayerConnection implements Listener { player.setOp(true); } - new BukkitRunnable() { - public void run() { - SkinDownloader skinDownloader = new SkinDownloader(); - if (skinDownloader.fetchSkinData(player.getName())) { - final PlayerProfile profile = player.getPlayerProfile(); - final String texture = skinDownloader.getTexture(); - final String signature = skinDownloader.getSignature(); - profile.setProperty(new ProfileProperty("textures", texture, signature)); - - new BukkitRunnable() { - public void run() { - if (player.isOnline()) { - player.setPlayerProfile(profile); - } - } - }.runTask(JavaPlugin.getPlugin(Main.class)); - } - } - }.runTaskAsynchronously(JavaPlugin.getPlugin(Main.class)); + final boolean shouldChangeUsername = false; + final boolean shouldSendMessage = false; + + SkinDownloader skinDownloader = new SkinDownloader(); + skinDownloader.applySkin(player, player.getName(), shouldChangeUsername, shouldSendMessage); } @EventHandler