From 4fa20a0367909d7e2b49429223a4b87f7f26b2d9 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Sat, 20 Jun 2020 09:26:48 +1200 Subject: [PATCH] Add /grabhead and use local skins if possible with skinapi --- .../libraryaddict/disguise/LibsDisguises.java | 1 + .../commands/utils/GrabHeadCommand.java | 129 ++++++++++++++++++ .../disguise/utilities/DisguiseUtilities.java | 6 +- .../disguise/utilities/SkinUtils.java | 9 ++ .../utilities/metrics/MetricsInitalizer.java | 7 + .../utilities/translations/LibsMsg.java | 6 + src/main/resources/plugin.yml | 11 +- 7 files changed, 167 insertions(+), 2 deletions(-) create mode 100644 src/main/java/me/libraryaddict/disguise/commands/utils/GrabHeadCommand.java diff --git a/src/main/java/me/libraryaddict/disguise/LibsDisguises.java b/src/main/java/me/libraryaddict/disguise/LibsDisguises.java index 11bda603..0d79d63b 100644 --- a/src/main/java/me/libraryaddict/disguise/LibsDisguises.java +++ b/src/main/java/me/libraryaddict/disguise/LibsDisguises.java @@ -162,6 +162,7 @@ public class LibsDisguises extends JavaPlugin { registerCommand("copydisguise", new CopyDisguiseCommand()); registerCommand("grabskin", new GrabSkinCommand()); registerCommand("savedisguise", new SaveDisguiseCommand()); + registerCommand("grabhead", new GrabHeadCommand()); } else { getLogger().info("Commands has been disabled, as per config"); } diff --git a/src/main/java/me/libraryaddict/disguise/commands/utils/GrabHeadCommand.java b/src/main/java/me/libraryaddict/disguise/commands/utils/GrabHeadCommand.java new file mode 100644 index 00000000..9ad7992a --- /dev/null +++ b/src/main/java/me/libraryaddict/disguise/commands/utils/GrabHeadCommand.java @@ -0,0 +1,129 @@ +package me.libraryaddict.disguise.commands.utils; + +import com.comphenix.protocol.wrappers.WrappedGameProfile; +import me.libraryaddict.disguise.DisguiseAPI; +import me.libraryaddict.disguise.LibsDisguises; +import me.libraryaddict.disguise.utilities.DisguiseUtilities; +import me.libraryaddict.disguise.utilities.LibsPremium; +import me.libraryaddict.disguise.utilities.SkinUtils; +import me.libraryaddict.disguise.utilities.reflection.NmsVersion; +import me.libraryaddict.disguise.utilities.reflection.ReflectionManager; +import me.libraryaddict.disguise.utilities.translations.LibsMsg; +import net.md_5.bungee.api.chat.ClickEvent; +import net.md_5.bungee.api.chat.ComponentBuilder; +import net.md_5.bungee.api.chat.HoverEvent; +import org.apache.commons.lang.StringUtils; +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.SkullMeta; +import org.bukkit.scheduler.BukkitRunnable; +import org.bukkit.scheduler.BukkitTask; + +import java.lang.reflect.Field; + +/** + * Created by libraryaddict on 20/06/2020. + */ +public class GrabHeadCommand implements CommandExecutor { + @Override + public boolean onCommand(CommandSender sender, Command command, String s, String[] strings) { + if (sender instanceof Player && !sender.isOp() && + (!LibsPremium.isPremium() || LibsPremium.getPaidInformation() == LibsPremium.getPluginInformation())) { + sender.sendMessage(ChatColor.RED + "Please purchase Lib's Disguises to enable player commands"); + return true; + } + + if (!sender.hasPermission("libsdisguises.grabhead")) { + sender.sendMessage(LibsMsg.NO_PERM.get()); + return true; + } + + if (!(sender instanceof Player)) { + sender.sendMessage(LibsMsg.NO_CONSOLE.get()); + return true; + } + + if (strings.length == 0) { + sendHelp(sender); + return true; + } + + String[] args = DisguiseUtilities.split(StringUtils.join(strings, " ")); + String skin = args[0]; + + String usable = SkinUtils.getUsableStatus(); + + if (usable != null) { + sender.sendMessage(usable); + return true; + } + + SkinUtils.SkinCallback callback = new SkinUtils.SkinCallback() { + private BukkitTask runnable = new BukkitRunnable() { + @Override + public void run() { + sender.sendMessage(LibsMsg.PLEASE_WAIT.get()); + } + }.runTaskTimer(LibsDisguises.getInstance(), 100, 100); + + @Override + public void onError(LibsMsg msg, Object... args) { + sender.sendMessage(msg.get(args)); + + runnable.cancel(); + } + + @Override + public void onInfo(LibsMsg msg, Object... args) { + sender.sendMessage(msg.get(args)); + } + + @Override + public void onSuccess(WrappedGameProfile profile) { + runnable.cancel(); + + DisguiseUtilities.setGrabHeadCommandUsed(); + + new BukkitRunnable() { + @Override + public void run() { + ItemStack skull = new ItemStack(Material.PLAYER_HEAD); + SkullMeta meta = (SkullMeta) skull.getItemMeta(); + + try { + Field field = meta.getClass().getDeclaredField("profile"); + field.setAccessible(true); + field.set(meta, profile.getHandle()); + } + catch (NoSuchFieldException | IllegalAccessException e) { + e.printStackTrace(); + } + + skull.setItemMeta(meta); + + ((Player) sender).getInventory().addItem(skull); + sender.sendMessage(LibsMsg.GRAB_HEAD_SUCCESS.get()); + } + }.runTask(LibsDisguises.getInstance()); + } + }; + + SkinUtils.grabSkin(skin, callback); + + return true; + } + + private void sendHelp(CommandSender sender) { + sender.sendMessage(LibsMsg.GRAB_DISG_HELP_1.get()); + sender.sendMessage(LibsMsg.GRAB_DISG_HELP_2.get()); + sender.sendMessage(LibsMsg.GRAB_DISG_HELP_3.get()); + sender.sendMessage(LibsMsg.GRAB_DISG_HELP_4.get()); + sender.sendMessage(LibsMsg.GRAB_DISG_HELP_5.get()); + sender.sendMessage(LibsMsg.GRAB_DISG_HELP_6.get()); + } +} diff --git a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java index 8bc6dc9d..b9710842 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseUtilities.java @@ -133,7 +133,7 @@ public class DisguiseUtilities { private static Gson gson; @Getter private static boolean pluginsUsed, commandsUsed, copyDisguiseCommandUsed, grabSkinCommandUsed, - saveDisguiseCommandUsed; + saveDisguiseCommandUsed, grabHeadCommandUsed; private static long libsDisguisesCalled; /** * Keeps track of what tick this occured @@ -173,6 +173,10 @@ public class DisguiseUtilities { grabSkinCommandUsed = true; } + public static void setGrabHeadCommandUsed() { + grabHeadCommandUsed = true; + } + public static void setCopyDisguiseCommandUsed() { copyDisguiseCommandUsed = true; } diff --git a/src/main/java/me/libraryaddict/disguise/utilities/SkinUtils.java b/src/main/java/me/libraryaddict/disguise/utilities/SkinUtils.java index fa0bffd6..a32f21c7 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/SkinUtils.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/SkinUtils.java @@ -213,7 +213,16 @@ public class SkinUtils { } } + WrappedGameProfile profile = DisguiseUtilities.getGameProfile(param); + + if (profile != null) { + callback.onInfo(LibsMsg.SKIN_API_USING_EXISTING_NAME); + callback.onSuccess(profile); + return; + } + callback.onInfo(LibsMsg.SKIN_API_USING_NAME); + handleName(param, modelType, callback); } } diff --git a/src/main/java/me/libraryaddict/disguise/utilities/metrics/MetricsInitalizer.java b/src/main/java/me/libraryaddict/disguise/utilities/metrics/MetricsInitalizer.java index ecd93080..95cd2db6 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/metrics/MetricsInitalizer.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/metrics/MetricsInitalizer.java @@ -81,6 +81,13 @@ public class MetricsInitalizer { } }); + metrics.addCustomChart(new Metrics.SimplePie("grabhead_command") { + @Override + public String getValue() { + return "" + DisguiseUtilities.isGrabHeadCommandUsed(); + } + }); + metrics.addCustomChart(new Metrics.SimplePie("save_disguise_command") { @Override public String getValue() { diff --git a/src/main/java/me/libraryaddict/disguise/utilities/translations/LibsMsg.java b/src/main/java/me/libraryaddict/disguise/utilities/translations/LibsMsg.java index 023c3fad..53126484 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/translations/LibsMsg.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/translations/LibsMsg.java @@ -233,6 +233,7 @@ public enum LibsMsg { SKIN_API_USING_FILE(ChatColor.GRAY + "File provided and found, now attempting to upload to mineskin.org"), SKIN_API_INVALID_NAME(ChatColor.RED + "Invalid name/file/uuid provided!"), SKIN_API_USING_UUID(ChatColor.GRAY + "UUID successfully parsed, now attempting to connect to mineskin.org"), + SKIN_API_USING_EXISTING_NAME(ChatColor.GRAY + "Found a saved skin under that name locally! Using that!"), SKIN_API_USING_NAME( ChatColor.GRAY + "Determined to be player name, now attempting to validate and connect to mineskin.org"), SAVE_DISG_HELP_1(ChatColor.GREEN + "The is what the disguise will be called in Lib's Disguises"), @@ -257,6 +258,11 @@ public enum LibsMsg { ChatColor.GREEN + "If you want the slim Alex version of the skin, append :slim. So 'myskin.png:slim'"), GRAB_DISG_HELP_6( ChatColor.GREEN + "You will be sent the skin data, but you can also use the saved names in disguises"), + GRAB_HEAD_SUCCESS(ChatColor.GREEN + "Head successfully grabbed and added to inventory!"), + GRAB_HEAD_HELP_1(ChatColor.GREEN + "Grab the head of a file, player or url! This is a Lib's Disguises feature."), + GRAB_HEAD_HELP_2(ChatColor.DARK_GREEN + "/grabhead https://somesite.com/myskin.png"), + GRAB_HEAD_HELP_3(ChatColor.DARK_GREEN + "/grabhead myskin.png - Skins must be in the folder!"), + GRAB_HEAD_HELP_4(ChatColor.DARK_GREEN + "/grabhead "), CUSTOM_DISGUISE_NAME_CONFLICT( ChatColor.RED + "Cannot create the custom disguise '%s' as there is a name conflict!"), ERROR_LOADING_CUSTOM_DISGUISE(ChatColor.RED + "Error while loading custom disguise '%s'%s"), diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 87c3d991..455ad19a 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -84,6 +84,10 @@ commands: aliases: [customdisguise, savedisg, customdisg, createdisguise, createdisg] permission: libsdisguises.seecmd.savedisguise description: Save a custom disguise to disguises.yml + grabhead: + aliases: [grabskull, playerhead, savehead, gethead] + permission: libsdisguises.seecmd.grabhead + description: Grab the head item of a skin, url or player permissions: libsdisguises.reload: @@ -123,6 +127,8 @@ permissions: default: false libsdisguises.multiname: description: Allows the command user to set names on different heights + libsdisguises.grabhead: + description: Allows the command user to use /grabhead libsdisguises.seecmd: description: See all commands in tab-completion default: true @@ -145,6 +151,7 @@ permissions: libsdisguises.seecmd.copydisguise: true libsdisguises.seecmd.grabskin: true libsdisguises.seecmd.savedisguise: true + libsdisguises.seecmd.grabhead: true libsdisguises.seecmd.disguiseviewself: description: See the /disguiseviewself command in tab-completion libsdisguises.seecmd.disguise: @@ -180,4 +187,6 @@ permissions: libsdisguises.seecmd.grabskin: description: See the /grabskin command in tab-completion libsdisguises.seecmd.savedisguise: - description: See the /savedisguise command in tab-completion \ No newline at end of file + description: See the /savedisguise command in tab-completion + libsdisguises.seecmd.grabhead: + description: See the /grabhead command in tab-completion \ No newline at end of file