From f0e03909a80117088dd0838ab9c581d108677500 Mon Sep 17 00:00:00 2001 From: Brianna Date: Tue, 7 May 2019 15:00:56 -0400 Subject: [PATCH 1/2] Added base64 and URL commands. --- .../epicheads/command/CommandManager.java | 7 +-- .../command/commands/CommandBase64.java | 55 ++++++++++++++++++ .../command/commands/CommandUrl.java | 56 +++++++++++++++++++ .../epicheads/listeners/ItemListeners.java | 3 +- .../epicheads/listeners/LoginListeners.java | 4 +- .../com/songoda/epicheads/utils/Methods.java | 9 ++- 6 files changed, 125 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/songoda/epicheads/command/commands/CommandBase64.java create mode 100644 src/main/java/com/songoda/epicheads/command/commands/CommandUrl.java diff --git a/src/main/java/com/songoda/epicheads/command/CommandManager.java b/src/main/java/com/songoda/epicheads/command/CommandManager.java index 9ed9e49..3f9b1c1 100644 --- a/src/main/java/com/songoda/epicheads/command/CommandManager.java +++ b/src/main/java/com/songoda/epicheads/command/CommandManager.java @@ -1,10 +1,7 @@ package com.songoda.epicheads.command; import com.songoda.epicheads.EpicHeads; -import com.songoda.epicheads.command.commands.CommandEpicHeads; -import com.songoda.epicheads.command.commands.CommandHelp; -import com.songoda.epicheads.command.commands.CommandReload; -import com.songoda.epicheads.command.commands.CommandSettings; +import com.songoda.epicheads.command.commands.*; import com.songoda.epicheads.utils.Methods; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -33,6 +30,8 @@ public class CommandManager implements CommandExecutor { addCommand(new CommandSettings(commandEpicHeads)); addCommand(new CommandHelp(commandEpicHeads)); addCommand(new CommandReload(commandEpicHeads)); + addCommand(new CommandUrl(commandEpicHeads)); + addCommand(new CommandBase64(commandEpicHeads)); for (AbstractCommand abstractCommand : commands) { if (abstractCommand.getParent() != null) continue; diff --git a/src/main/java/com/songoda/epicheads/command/commands/CommandBase64.java b/src/main/java/com/songoda/epicheads/command/commands/CommandBase64.java new file mode 100644 index 0000000..e6860ca --- /dev/null +++ b/src/main/java/com/songoda/epicheads/command/commands/CommandBase64.java @@ -0,0 +1,55 @@ +package com.songoda.epicheads.command.commands; + +import com.songoda.epicheads.EpicHeads; +import com.songoda.epicheads.command.AbstractCommand; +import com.songoda.epicheads.utils.Methods; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.SkullMeta; + +import java.util.List; + +public class CommandBase64 extends AbstractCommand { + + public CommandBase64(AbstractCommand parent) { + super(parent, true, "base64"); + } + + @Override + protected ReturnType runCommand(EpicHeads instance, CommandSender sender, String... args) { + + Player player = (Player)sender; + + ItemStack item = player.getItemInHand(); + + if (!item.hasItemMeta() || !(item.getItemMeta() instanceof SkullMeta)) return ReturnType.FAILURE; + + String encodededStr = Methods.getEncodedTexture(item); + + player.sendMessage(instance.getReferences().getPrefix()); + player.sendMessage(encodededStr); + + return ReturnType.SUCCESS; + } + + @Override + protected List onTab(EpicHeads instance, CommandSender sender, String... args) { + return null; + } + + @Override + public String getPermissionNode() { + return "epicheads.base64"; + } + + @Override + public String getSyntax() { + return "/epicheads base64"; + } + + @Override + public String getDescription() { + return "Gives you the base64 code of the head you are holding."; + } +} diff --git a/src/main/java/com/songoda/epicheads/command/commands/CommandUrl.java b/src/main/java/com/songoda/epicheads/command/commands/CommandUrl.java new file mode 100644 index 0000000..d5c408e --- /dev/null +++ b/src/main/java/com/songoda/epicheads/command/commands/CommandUrl.java @@ -0,0 +1,56 @@ +package com.songoda.epicheads.command.commands; + +import com.songoda.epicheads.EpicHeads; +import com.songoda.epicheads.command.AbstractCommand; +import com.songoda.epicheads.utils.Methods; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.SkullMeta; + +import java.util.List; + +public class CommandUrl extends AbstractCommand { + + public CommandUrl(AbstractCommand parent) { + super(parent, true, "url"); + } + + @Override + protected ReturnType runCommand(EpicHeads instance, CommandSender sender, String... args) { + + Player player = (Player)sender; + + ItemStack item = player.getItemInHand(); + + if (!item.hasItemMeta() || !(item.getItemMeta() instanceof SkullMeta)) return ReturnType.FAILURE; + + String encodededStr = Methods.getEncodedTexture(item); + String url = Methods.getDecodedTexture(encodededStr); + + player.sendMessage(instance.getReferences().getPrefix()); + player.sendMessage("http://textures.minecraft.net/texture/" + url); + + return ReturnType.SUCCESS; + } + + @Override + protected List onTab(EpicHeads instance, CommandSender sender, String... args) { + return null; + } + + @Override + public String getPermissionNode() { + return "epicheads.url"; + } + + @Override + public String getSyntax() { + return "/epicheads url"; + } + + @Override + public String getDescription() { + return "Gives you the texture url for the head you are holding."; + } +} diff --git a/src/main/java/com/songoda/epicheads/listeners/ItemListeners.java b/src/main/java/com/songoda/epicheads/listeners/ItemListeners.java index af99968..5097f73 100644 --- a/src/main/java/com/songoda/epicheads/listeners/ItemListeners.java +++ b/src/main/java/com/songoda/epicheads/listeners/ItemListeners.java @@ -33,7 +33,8 @@ public class ItemListeners implements Listener { event.getItem().removeMetadata("EHE", plugin); - String url = Methods.getDecodedTexture(item); + String encodededStr = Methods.getEncodedTexture(item); + String url = Methods.getDecodedTexture(encodededStr); if (url == null) return; Optional optional = plugin.getHeadManager().getHeads().stream() diff --git a/src/main/java/com/songoda/epicheads/listeners/LoginListeners.java b/src/main/java/com/songoda/epicheads/listeners/LoginListeners.java index 1d14fc7..036402a 100644 --- a/src/main/java/com/songoda/epicheads/listeners/LoginListeners.java +++ b/src/main/java/com/songoda/epicheads/listeners/LoginListeners.java @@ -36,7 +36,9 @@ public class LoginListeners implements Listener { meta.setOwningPlayer(player); item.setItemMeta(meta); - String url = Methods.getDecodedTexture(item); + String encodededStr = Methods.getEncodedTexture(item); + String url = Methods.getDecodedTexture(encodededStr); + String tagStr = "Player Heads"; diff --git a/src/main/java/com/songoda/epicheads/utils/Methods.java b/src/main/java/com/songoda/epicheads/utils/Methods.java index 3ac55de..f2f6c9b 100644 --- a/src/main/java/com/songoda/epicheads/utils/Methods.java +++ b/src/main/java/com/songoda/epicheads/utils/Methods.java @@ -36,7 +36,7 @@ public class Methods { return item; } - public static String getDecodedTexture(ItemStack item) { + public static String getEncodedTexture(ItemStack item) { try { SkullMeta localSkullMeta = (SkullMeta) item.getItemMeta(); Field localField = localSkullMeta.getClass().getDeclaredField("profile"); @@ -45,8 +45,7 @@ public class Methods { Iterator localIterator = localGameProfile.getProperties().get("textures").iterator(); if (localIterator.hasNext()) { Property localProperty = (Property) localIterator.next(); - byte[] decoded = Base64.getDecoder().decode(localProperty.getValue()); - return StringUtils.substringBetween(new String(decoded), "texture/", "\""); + return localProperty.getValue(); } } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); @@ -54,6 +53,10 @@ public class Methods { return null; } + public static String getDecodedTexture(String decoded) { + return StringUtils.substringBetween(new String(Base64.getDecoder().decode(decoded)), "texture/", "\""); + } + public static ItemStack getGlass() { EpicHeads instance = EpicHeads.getInstance(); return Methods.getGlass(instance.getConfig().getBoolean("Interfaces.Replace Glass Type 1 With Rainbow Glass"), instance.getConfig().getInt("Interfaces.Glass Type 1")); From 19adc5c07e2c429fcabb2ae40540e1308ea5917c Mon Sep 17 00:00:00 2001 From: Brianna Date: Tue, 7 May 2019 18:29:36 -0400 Subject: [PATCH 2/2] Added give command. --- .../epicheads/command/CommandManager.java | 1 + .../command/commands/CommandGive.java | 88 +++++++++++++++++++ .../songoda/epicheads/head/HeadManager.java | 4 + src/main/resources/en_US.lang | 7 +- 4 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/songoda/epicheads/command/commands/CommandGive.java diff --git a/src/main/java/com/songoda/epicheads/command/CommandManager.java b/src/main/java/com/songoda/epicheads/command/CommandManager.java index 3f9b1c1..acc9627 100644 --- a/src/main/java/com/songoda/epicheads/command/CommandManager.java +++ b/src/main/java/com/songoda/epicheads/command/CommandManager.java @@ -32,6 +32,7 @@ public class CommandManager implements CommandExecutor { addCommand(new CommandReload(commandEpicHeads)); addCommand(new CommandUrl(commandEpicHeads)); addCommand(new CommandBase64(commandEpicHeads)); + addCommand(new CommandGive(commandEpicHeads)); for (AbstractCommand abstractCommand : commands) { if (abstractCommand.getParent() != null) continue; diff --git a/src/main/java/com/songoda/epicheads/command/commands/CommandGive.java b/src/main/java/com/songoda/epicheads/command/commands/CommandGive.java new file mode 100644 index 0000000..c6f81d1 --- /dev/null +++ b/src/main/java/com/songoda/epicheads/command/commands/CommandGive.java @@ -0,0 +1,88 @@ +package com.songoda.epicheads.command.commands; + +import com.songoda.epicheads.EpicHeads; +import com.songoda.epicheads.command.AbstractCommand; +import com.songoda.epicheads.head.Head; +import com.songoda.epicheads.utils.Methods; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.inventory.meta.SkullMeta; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +public class CommandGive extends AbstractCommand { + + public CommandGive(AbstractCommand parent) { + super(parent, false, "give"); + } + + @Override + protected ReturnType runCommand(EpicHeads instance, CommandSender sender, String... args) { + + if (args.length != 4) return ReturnType.SYNTAX_ERROR; + + Player player = Bukkit.getPlayer(args[1]); + String archive = args[2]; + int headId = Integer.parseInt(args[3]); + + if (player == null) { + sender.sendMessage(instance.getReferences().getPrefix() + instance.getLocale().getMessage("command.give.notonline", args[1])); + return ReturnType.FAILURE; + } + + List heads; + + if (archive.equalsIgnoreCase("global")) + heads = instance.getHeadManager().getGlobalHeads(); + else if (archive.equalsIgnoreCase("local")) + heads = instance.getHeadManager().getLocalHeads(); + else { + return ReturnType.SYNTAX_ERROR; + } + + Optional head = heads.stream().filter(h -> h.getId() == headId).findFirst(); + + if (head.isPresent()) { + ItemStack item = head.get().asItemStack(); + + ItemMeta meta = item.getItemMeta(); + meta.setLore(new ArrayList<>()); + item.setItemMeta(meta); + + player.getInventory().addItem(item); + } else { + sender.sendMessage(instance.getReferences().getPrefix() + instance.getLocale().getMessage("command.give.notfound", head.get().getName())); + return ReturnType.FAILURE; + } + + sender.sendMessage(instance.getReferences().getPrefix() + instance.getLocale().getMessage("command.give.success", player.getName(), head.get().getName())); + player.sendMessage(instance.getReferences().getPrefix() + instance.getLocale().getMessage("command.give.receive", head.get().getName())); + + return ReturnType.SUCCESS; + } + + @Override + protected List onTab(EpicHeads instance, CommandSender sender, String... args) { + return null; + } + + @Override + public String getPermissionNode() { + return "epicheads.give"; + } + + @Override + public String getSyntax() { + return "/heads give "; + } + + @Override + public String getDescription() { + return "Gives the specified player the specified amount of heads."; + } +} diff --git a/src/main/java/com/songoda/epicheads/head/HeadManager.java b/src/main/java/com/songoda/epicheads/head/HeadManager.java index f54faee..bbcc363 100644 --- a/src/main/java/com/songoda/epicheads/head/HeadManager.java +++ b/src/main/java/com/songoda/epicheads/head/HeadManager.java @@ -72,6 +72,10 @@ public class HeadManager { return Collections.unmodifiableList(localRegisteredHeads); } + public List getGlobalHeads() { + return new ArrayList<>(registeredHeads); + } + public void removeLocalHead(Head head) { localRegisteredHeads.remove(head); } diff --git a/src/main/resources/en_US.lang b/src/main/resources/en_US.lang index f17e475..9a85011 100644 --- a/src/main/resources/en_US.lang +++ b/src/main/resources/en_US.lang @@ -27,4 +27,9 @@ gui.overview.discord = "&9&lDiscord" gui.overview.discordlore = "&8Add or request new heads|&8in our discord server." event.general.nopermission = "&cYou do not have permission to use that command." -event.buyhead.cannotafford = "&cYou cannot afford this head." \ No newline at end of file +event.buyhead.cannotafford = "&cYou cannot afford this head." + +command.give.notonline = "&cThe player &4%name% &ccould not be found." +command.give.notfound = "&cThe head &4%name%&c could not be found." +command.give.success = "&7You have been given &6%player% &7a head named &6%name%&7." +command.give.receive = "&7You have been given a head named &6%name%&7." \ No newline at end of file