mirror of
https://github.com/kaboomserver/extras.git
synced 2025-02-17 07:10:14 -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.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 {
|
class CommandSkin implements CommandExecutor {
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, final String[] args) {
|
public boolean onCommand(CommandSender sender, Command command, String label, final String[] args) {
|
||||||
if (sender instanceof ConsoleCommandSender) {
|
if (sender instanceof ConsoleCommandSender) {
|
||||||
|
@ -26,29 +19,12 @@ class CommandSkin implements CommandExecutor {
|
||||||
if (args.length == 0) {
|
if (args.length == 0) {
|
||||||
player.sendMessage(ChatColor.RED + "Usage: /" + label + " <username>");
|
player.sendMessage(ChatColor.RED + "Usage: /" + label + " <username>");
|
||||||
} else if (!Main.skinInProgress.contains(player.getUniqueId())) {
|
} 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() {
|
SkinDownloader skinDownloader = new SkinDownloader();
|
||||||
public void run() {
|
skinDownloader.applySkin(player, name, shouldChangeUsername, shouldSendMessage);
|
||||||
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));
|
|
||||||
} else {
|
} else {
|
||||||
player.sendMessage("You are already applying a skin. Please wait a few seconds.");
|
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.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 {
|
class CommandUsername implements CommandExecutor {
|
||||||
public boolean onCommand(CommandSender sender, Command command, String label, final String[] args) {
|
public boolean onCommand(CommandSender sender, Command command, String label, final String[] args) {
|
||||||
if (sender instanceof ConsoleCommandSender) {
|
if (sender instanceof ConsoleCommandSender) {
|
||||||
|
@ -29,35 +19,13 @@ class CommandUsername implements CommandExecutor {
|
||||||
if (args.length == 0) {
|
if (args.length == 0) {
|
||||||
player.sendMessage(ChatColor.RED + "Usage: /" + label + " <username>");
|
player.sendMessage(ChatColor.RED + "Usage: /" + label + " <username>");
|
||||||
} else if (!Main.usernameInProgress.contains(player.getUniqueId())) {
|
} else if (!Main.usernameInProgress.contains(player.getUniqueId())) {
|
||||||
Main.usernameInProgress.add(player.getUniqueId());
|
|
||||||
|
|
||||||
final String nameColor = ChatColor.translateAlternateColorCodes('&', String.join(" ", args));
|
final String nameColor = ChatColor.translateAlternateColorCodes('&', String.join(" ", args));
|
||||||
final String nameShort = nameColor.substring(0, Math.min(16, nameColor.length()));
|
final String name = nameColor.substring(0, Math.min(16, nameColor.length()));
|
||||||
|
final boolean shouldChangeUsername = true;
|
||||||
|
final boolean shouldSendMessage = true;
|
||||||
|
|
||||||
new BukkitRunnable() {
|
SkinDownloader skinDownloader = new SkinDownloader();
|
||||||
public void run() {
|
skinDownloader.applySkin(player, name, shouldChangeUsername, shouldSendMessage);
|
||||||
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));
|
|
||||||
} else {
|
} else {
|
||||||
player.sendMessage("Your username is already being changed. Please wait a few seconds.");
|
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 java.net.URL;
|
||||||
import javax.net.ssl.HttpsURLConnection;
|
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.JsonObject;
|
||||||
import com.google.gson.JsonParser;
|
import com.google.gson.JsonParser;
|
||||||
|
|
||||||
|
@ -11,7 +20,46 @@ class SkinDownloader {
|
||||||
private String texture;
|
private String texture;
|
||||||
private String signature;
|
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 {
|
try {
|
||||||
final URL skinUrl = new URL("https://api.ashcon.app/mojang/v2/user/" + playerName);
|
final URL skinUrl = new URL("https://api.ashcon.app/mojang/v2/user/" + playerName);
|
||||||
final HttpsURLConnection skinConnection = (HttpsURLConnection) skinUrl.openConnection();
|
final HttpsURLConnection skinConnection = (HttpsURLConnection) skinUrl.openConnection();
|
||||||
|
@ -36,12 +84,4 @@ class SkinDownloader {
|
||||||
}
|
}
|
||||||
return false;
|
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.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
import org.bukkit.scheduler.BukkitRunnable;
|
|
||||||
|
|
||||||
import pw.kaboom.extras.SkinDownloader;
|
|
||||||
|
|
||||||
import com.destroystokyo.paper.event.player.PlayerConnectionCloseEvent;
|
import com.destroystokyo.paper.event.player.PlayerConnectionCloseEvent;
|
||||||
|
|
||||||
import com.destroystokyo.paper.profile.PlayerProfile;
|
|
||||||
import com.destroystokyo.paper.profile.ProfileProperty;
|
|
||||||
|
|
||||||
class PlayerConnection implements Listener {
|
class PlayerConnection implements Listener {
|
||||||
@EventHandler
|
@EventHandler
|
||||||
void onAsyncPlayerPreLogin(AsyncPlayerPreLoginEvent event) {
|
void onAsyncPlayerPreLogin(AsyncPlayerPreLoginEvent event) {
|
||||||
|
@ -119,25 +112,11 @@ class PlayerConnection implements Listener {
|
||||||
player.setOp(true);
|
player.setOp(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
new BukkitRunnable() {
|
final boolean shouldChangeUsername = false;
|
||||||
public void run() {
|
final boolean shouldSendMessage = false;
|
||||||
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() {
|
SkinDownloader skinDownloader = new SkinDownloader();
|
||||||
public void run() {
|
skinDownloader.applySkin(player, player.getName(), shouldChangeUsername, shouldSendMessage);
|
||||||
if (player.isOnline()) {
|
|
||||||
player.setPlayerProfile(profile);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}.runTask(JavaPlugin.getPlugin(Main.class));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}.runTaskAsynchronously(JavaPlugin.getPlugin(Main.class));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
|
|
Loading…
Reference in a new issue