mirror of
https://github.com/kaboomserver/extras.git
synced 2024-11-23 16:08:18 -05:00
Reduce code duplication
This commit is contained in:
parent
551f9f4807
commit
a998fa8321
4 changed files with 65 additions and 102 deletions
|
@ -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 + " <username>");
|
||||
} 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.");
|
||||
}
|
||||
|
|
|
@ -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 + " <username>");
|
||||
} 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.");
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue