Solved Retrieve info from a site like NameMC

Discussion in 'Plugin Development' started by Wispyy, Nov 19, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    Wispyy

    Hi :p
    So I've been tweaking around with a plugin of mine and I've been using GitHub to fetch my username and put it into the code, but when I change it (Minecraft, not Bukkit username) it will be tedious to login and change the file. The code below works fine how it is, but when adding in the namemc URL it errors. (None in console, I have it nullcheck and if it returns null it uses my Bukkit username.) So am I able to retrieve certain parts of the webpage such as my name or is there another site I can easily use?
    URL (open)

    Code:
    public static ItemStack returnSkull() {
    
            Server server = Bukkit.getServer();
    
           try {
    
           URL address = new URL("https://raw.githubusercontent.com/WispySkies/fetch/master/fetch.txt");
    
    
           InputStreamReader pageInput = new InputStreamReader(address.openStream());
    
           BufferedReader source = new BufferedReader(pageInput);
    
           try {
    
                   String srcl = source.readLine();
    
                  ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
                  SkullMeta meta = (SkullMeta) skull.getItemMeta();
                  meta.setOwner(srcl);
                  meta.setDisplayName("§1Particles §1By");
                  ArrayList<String> skulllore = new ArrayList<String>();
                  skulllore.add("§2" + srcl);
                  meta.setLore(skulllore);
                  skull.setItemMeta(meta);
                  return skull;
    
            } catch(IOException e) {
    
                  server.getLogger().info("Particles - Couldn't read the line and add it to the ItemStack. [See Developer]");
                  return null;
            }
    } catch (IOException e) {
    
           server.getLogger().info("Particles - Failed to retrieve username from GitHub. [See Developer]");
    return null;
    }
    }
    


    Null Checking (open)

    Code:
    ItemStack sk = Main.returnSkull();
    
    if (sk == null) {
           ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
           SkullMeta meta = (SkullMeta) skull.getItemMeta();
           meta.setOwner("Notch");
           meta.setDisplayName("§1Particles §1By");
           ArrayList<String> skulllore = new ArrayList<String>();
           skulllore.add("§2Wispyy");
           meta.setLore(skulllore);
           skull.setItemMeta(meta);
           inv.setItem(53, skull);
    } else {
           inv.setItem(53, sk);
    }
    


    Edit: Tried to fix the indent, sorry but it looks bad now :L
     
    Last edited: Nov 19, 2016
  2. Offline

    Zombie_Striker

    @Wispyy
    The main goal of this plugin is to display your name no matter what you change it to? If so, why not use the UUID system to get your player's name? The (psudo)code below will display your current name:
    Code:
    name = Bukkit.getOfflinePlayer( YOUR UUID ).getName()
    If you do not know your UUID, look at your server's directory and find the "usercache" file. Inside there you can locate your current name and your UUID.
     
  3. Offline

    Wispyy

    Oh, thanks. I never really found a solution of UUID to names, only names to UUIDs. :)
    Edit: @Zombie_Striker it just gives me the UUID, not the name?
    I did:
    Code:
    meta.setOwner(Bukkit.getOfflinePlayer("357bfd34-5bb3-45c0-9081-078bf3bfb38a").getName());
    
    but it shows up as 357bfd34-5bb3-45c0-9081-078bf3bfb38a not my username.
     
    Last edited: Nov 19, 2016
  4. Offline

    Zombie_Striker

    @Wispyy
    It seems that Bukkit does not look up the offline player's data. To get around this, use this class that evilmidget38 created:
    https://gist.github.com/evilmidget38/a5c971d2f2b2c3b3fb37

    All you need to do is create an arraylist that contains only your UUID, create the NameFetcher class, call the method "call", and get the first (and only) value in the map. Do this once in the onEnable and store that name somewhere (that way you only need to access the mojang servers once)
     
    I Al Istannen likes this.
  5. Offline

    I Al Istannen

    @Zombie_Striker @Wispyy
    Keep in mind the mojang API is rate limited!

    You can see an overview on the Mojang API on wiki.vg.

    The rate limit:
     
  6. Offline

    HeartandSoul

    Actually if you want an offline user directory, your MC client has one. Its out of sync once you make it into ones own directory.
     
  7. Offline

    Wispyy

  8. Offline

    Zombie_Striker

    @Wispyy

    If your problem has been solved, mark this thread a solved.
     
Thread Status:
Not open for further replies.

Share This Page