1.15.2 Trying to get an NPC skin texture and signature from their name

Discussion in 'Spigot Plugin Development' started by VivianMusic, Mar 10, 2020.

  1. Hello, I am trying to get the skin texture and signature from a player's name.

    Here's my code

    Code (Text):
        public String[] getTextureAndSignature(String npcName) throws IOException {
            URL url_0 = new URL("https://api.mojang.com/users/profiles/minecraft/" + npcName);
            InputStreamReader reader_0 = new InputStreamReader(url_0.openStream());

            String uuid = new JsonParser().parse(reader_0).getAsJsonObject().get("id").getAsString();

            URL url_1 = new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid + "?unsigned=false");
            InputStreamReader reader_1 = new InputStreamReader(url_1.openStream());
            JsonObject textureProperty = new JsonParser().parse(reader_1).getAsJsonObject().get("properties").getAsJsonArray().get(0).getAsJsonObject();
            String texture = textureProperty.get("value").getAsString();
            String signature = textureProperty.get("signature").getAsString();

            return new String[] {texture, signature};
        }
     
    I am using this method, and passing
    Code (Text):
                    NPC knightNPC = new NPC ("Knight", ClassType.KNIGHT, instance.npcUtils.getTextureAndSignature("Knight")[0], instance.npcUtils.getTextureAndSignature("")[1], "Knight", new Location(Bukkit.getServer().getWorld("newnations"), 1, 2, 3));
    I am getting this error in console, then my plugin is disabled
    Code (Text):
    10.03 01:18:48 [Server] INFO [NationClasses] Enabling NationClasses v1.0
    10.03 01:18:48 [Server] WARN java.io.FileNotFoundException: https://api.mojang.com/users/profiles/minecraft/
    10.03 01:18:48 [Server] WARN at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1915)
    10.03 01:18:48 [Server] WARN at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1515)
    10.03 01:18:48 [Server] WARN at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:250)
    10.03 01:18:48 [Server] WARN at java.base/java.net.URL.openStream(URL.java:1140)
    10.03 01:18:48 [Server] WARN at com.squallz.nationclasses.utils.NPCUtils.getTextureAndSignature(NPCUtils.java:18)
    10.03 01:18:48 [Server] WARN at
     
  2. It's probably because you aren't specifying a string, which results in querying this url, which returns nothing, hence the error FileNotFoundException

    EDIT:
    Code (Java):
    ...instance.npcUtils.getTextureAndSignature("")[1]...
     
  3. Good catch, small brain moment.. getting this now though:
    Code (Text):
    10.03 01:36:55 [Server] WARN java.io.IOException: Server returned HTTP response code: 429 for URL: https://sessionserver.mojang.com/session/minecraft/profile/457b7d2c258f4a1b924378835e4f1e23nsigned=false
    10.03 01:36:55 [Server] WARN at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1919)
    10.03 01:36:55 [Server] WARN at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1515)
    10.03 01:36:55 [Server] WARN at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:250)
    10.03 01:36:55 [Server] WARN at java.base/java.net.URL.openStream(URL.java:1140)
    10.03 01:36:55 [Server] WARN at com.squallz.nationclasses.utils.NPCUtils.getTextureAndSignature(NPCUtils.java:23)
    10.03 01:36:55 [Server] WARN at com.squallz.nationclasses.utils.FileUtils.createNPCConfig(FileUtils.java:49)
    10.03 01:36:55 [Server] WARN at com.squallz.nationclasses.ClassesMain.onEnable(ClassesMain.java:26)
    10.03 01:36:55 [Server] WARN at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263)
    10.03 01:36:55 [Server] WARN at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337)
    10.03 01:36:55 [Server] WARN at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:435)
    10.03 01:36:55 [Server] WARN at com.rylinaux.plugman.util.PluginUtil.load(PluginUtil.java:366)
    10.03 01:36:55 [Server] WARN at com.rylinaux.plugman.util.PluginUtil.load(PluginUtil.java:318)
    10.03 01:36:55 [Server] WARN at com.rylinaux.plugman.util.PluginUtil.reload(PluginUtil.java:380)
    10.03 01:36:55 [Server] WARN at com.rylinaux.plugman.command.ReloadCommand.execute(ReloadCommand.java:122)
    10.03 01:36:55 [Server] WARN at com.rylinaux.plugman.PlugManCommandHandler.onCommand(PlugManCommandHandler.java:95)
    10.03 01:36:55 [Server] WARN at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45)
    10.03 01:36:55 [Server] WARN at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:159)
    10.03 01:36:55 [Server] WARN at org.bukkit.craftbukkit.v1_15_R1.CraftServer.dispatchCommand(CraftServer.java:742)
    10.03 01:36:55 [Server] WARN at net.minecraft.server.v1_15_R1.PlayerConnection.handleCommand(PlayerConnection.java:1825)
    10.03 01:36:55 [Server] WARN at net.minecraft.server.v1_15_R1.PlayerConnection.a(PlayerConnection.java:1633)
    10.03 01:36:55 [Server] WARN at net.minecraft.server.v1_15_R1.PacketPlayInChat.a(PacketPlayInChat.java:47)
    10.03 01:36:55 [Server] WARN at net.minecraft.server.v1_15_R1.PacketPlayInChat.a(PacketPlayInChat.java:5)
    10.03 01:36:55 [Server] WARN at net.minecraft.server.v1_15_R1.PlayerConnectionUtils.lambda$ensureMainThread$0(PlayerConnectionUtils.java:23)
    10.03 01:36:55 [Server] WARN at net.minecraft.server.v1_15_R1.TickTask.run(SourceFile:18)
    10.03 01:36:55 [Server] WARN at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeTask(IAsyncTaskHandler.java:136)
    10.03 01:36:55 [Server] WARN at net.minecraft.server.v1_15_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23)
    10.03 01:36:55 [Server] WARN at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeNext(IAsyncTaskHandler.java:109)
    10.03 01:36:55 [Server] WARN at net.minecraft.server.v1_15_R1.MinecraftServer.ba(MinecraftServer.java:1038)
     
  4. I guess it's rate limit. You can try to use some third-party APIs to get rid of it.
     
  5. Crap...any idea how I can get around it?
     
  6. You can only ping a url from mojang's API servers once every minute I believe, you need to cache any successful attempts, possibly write to a file, so once you fetch a skin, you don't need to fetch it again

    Also make sure you do this stuff Async, as to not hold up the main thread
     
  7. That's what I'm wanting to do, I am using this

    Code (Text):
        public void createNPCConfig (){
            File npcFile = new File(instance.getDataFolder(), "npcs.json");

            if(!(npcFile.getParentFile().exists())) {
                npcFile.getParentFile().mkdirs();
            }

            if(!(npcFile.exists())) {
                try {
                    npcFile.createNewFile();

                    Writer writer = null;
                    try {
                        writer = new FileWriter(npcFile);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create();

                    NPC knightNPC = new NPC ("Knight", ClassType.KNIGHT, instance.npcUtils.getTextureAndSignature("Knight")[0], instance.npcUtils.getTextureAndSignature("Knight")[1], "Knight", new Location(Bukkit.getServer().getWorld("newnations"), 1, 2, 3));

                    ItemStack knightSword = new ItemStack(Material.WOODEN_SWORD);
                    ItemMeta knightSwordMeta = knightSword.getItemMeta();
                    knightSwordMeta.setCustomModelData(1);
                    knightSwordMeta.setDisplayName(ChatColor.DARK_PURPLE + "Knight Sword");
                    List<String> knightSwordLore = new ArrayList<>();
                    knightSwordLore.add("");
                    knightSwordLore.add(ChatColor.RED + "" + ChatColor.ITALIC + "Forged with the blood of the Knight's");
                    knightSwordLore.add(ChatColor.RED + "" + ChatColor.ITALIC + "enemies...");
                    knightSwordMeta.setLore(knightSwordLore);
                    knightSword.setItemMeta(knightSwordMeta);

                    knightNPC.setMainhandItem(CraftItemStack.asNMSCopy(knightSword));

                    instance.npcUtils.addNPC(knightNPC);

                    for(NPC npcs : instance.npcUtils.npcList) {
                        gson.toJson(npcs, writer);
                    }

                    try {
                        writer.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    to grab the skin once, then it stores it in the config where I load it up later. Problem is the config is saving empty because it's erroring from trying to grab this texture
     
  8. Code (Text):
    10.03 02:14:07 [Server] ERROR Error occurred while enabling NationClasses v1.0 (Is it up to date?)
    10.03 02:14:07 [Server] INFO java.lang.IllegalArgumentException: Invalid UUID string: 457b7d2c258f4a1b924378835e4f1e23
    10.03 02:14:07 [Server] INFO at java.util.UUID.fromString(UUID.java:215) ~[?:?]
    10.03 02:14:07 [Server] INFO at com.squallz.nationclasses.objects.NPC.<init>(NPC.java:36) ~[?:?]
    10.03 02:14:07 [Server] INFO at com.squallz.nationclasses.utils.FileUtils.createNPCConfig(FileUtils.java:51) ~[?:?]
    New error...
     
  9. I can see clearly that the uuid is invalid, you should take a look at google
     
  10. I figured it out, dashes were missing