UUIDFetcher

Discussion in 'Spigot Plugin Development' started by AllinoneGamer, May 30, 2017.

  1. I'm having problem with getting a player from a uuid using, Bukkit.getPlayer(uuid);, i know that the uuid isn't null because i printed the uuid out and it returned with the right one i know this because i put the uuid into namemc and it came up with my profile, yet it returns null when getting the player, here is my code:

    Code (Text):

         
    UUIDFetcher fetcher = new UUIDFetcher(Arrays.asList(args[0]));
               Map<String, UUID> response = null;
               try {
               response = fetcher.call();
               } catch (Exception e) {
               getLogger().warning("Either mojang services are down or there was an exception while running UUIDFetcher");
               e.printStackTrace();
               }
               UUID u = response.get(args[0]);
               if (u == null) {
                 p.sendMessage(ChatColor.RED + "Invalid player!");
               } else {
                 String uuid = u.toString();
                 Player blacklist = Bukkit.getPlayer(uuid);
                 p.sendMessage(ChatColor.GREEN + "Fetching UUID & IP");
                 String reason;
                 
                 
                 StringBuilder sb = new StringBuilder();
                 for (int i = 2; i < args.length; i++) {
                   if (i > 2)
                     sb.append(" ");
                   sb.append(args[i]);
                 }

                 reason = sb.toString();

                 getConfig().set(blacklist.getName() + ".Creator", p.getName());
                 getConfig().set(blacklist.getName() + ".UUID", u.toString());
                 getConfig().set(blacklist.getName() + ".reason", reason);
                 saveConfig();

                 blacklist.kickPlayer("" + ChatColor.DARK_PURPLE + ChatColor.BOLD + "Arythium " + ""
                     + ChatColor.GRAY + ChatColor.BOLD + "» " + ChatColor.LIGHT_PURPLE
                     + "You have been blacklisted for " + ChatColor.DARK_PURPLE + reason);
                 Bukkit.broadcastMessage("" + ChatColor.DARK_PURPLE + ChatColor.BOLD + "Arythium " + ""
                     + ChatColor.GRAY + ChatColor.BOLD + "» " + ChatColor.DARK_PURPLE + blacklist.getName()
                     + ChatColor.LIGHT_PURPLE + " has been blacklisted by " + ChatColor.DARK_PURPLE
                     + p.getName());
               }
             }

     
     
  2. When you say printed out, do you mean like a debug message?
     
  3. yeah and it printed out the proper uuid
     
  4. console stacktrace:
    Code (Text):
    30.05 18:49:59 [Server] INFO Tacts issued server command: /blacklist Tacts 13 30.05 18:50:00 [Server] INFO 42e38be6-1dba-4a70-b7db-7d61f01b47aa 30.05 18:50:00 [Server] ERROR null 30.05 18:50:00 [Server] INFO org.bukkit.command.CommandException: Unhandled exception executing command 'blacklist' in plugin Blacklist v1 30.05 18:50:00 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95] 30.05 18:50:00 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:181) ~[spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95] 30.05 18:50:00 [Server] INFO at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServer.java:767) ~[spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95] 30.05 18:50:00 [Server] INFO at net.minecraft.server.v1_7_R4.PlayerConnection.handleCommand(PlayerConnection.java:1043) [spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95] 30.05 18:50:00 [Server] INFO at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:880) [spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95] 30.05 18:50:00 [Server] INFO at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java:28) [spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95] 30.05 18:50:00 [Server] INFO at net.minecraft.server.v1_7_R4.PacketPlayInChat.handle(PacketPlayInChat.java:65) [spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95] 30.05 18:50:00 [Server] INFO at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:186) [spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95] 30.05 18:50:00 [Server] INFO at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:81) [spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95] 30.05 18:50:00 [Server] INFO at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:734) [spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95] 30.05 18:50:00 [Server] INFO at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:289) [spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95] 30.05 18:50:00 [Server] INFO at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584) [spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95] 30.05 18:50:00 [Server] INFO at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490) [spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95] 30.05 18:50:00 [Server] INFO at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95] 30.05 18:50:00 [Server] INFO Caused by: java.lang.NullPointerException 30.05 18:50:00 [Server] INFO at me.conflicted.blacklist.Main.onCommand(Main.java:60) ~[?:?] 30.05 18:50:00 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95] 30.05 18:50:00 [Server] INFO ... 13 more
     
  5. Player represents a Player which has been online at the time you called Bukkit::getPlayer(UUID|String), which means that if you call it when said player is offline, it returns null.

    Do note that you don't really require a non null Player, since you only need it for kicking the user. Simply:
    1. Try Bukkit::getPlayer(String) first, which will avoid you a call using UUIDFetcher
    2. If the player is not online, fetch their UUID through the UUIDFetcher
    3. Store all data in the config using their UUID, since their name could change. (unless it's specifically to blacklist a specific name for a specific user, i.e. 'banning' them till they change their name)
    4. Either reuse the Player instance from before, or call Bukkit::getPlayer(UUID) to get the online instance. If they're online, kick them.
    Please note that:
    • Step 2 should be done asynchronously, as the API does a HTTP GET request in the background, which is known to be slow (nothing you can do about it, it's simply the time it takes for your machine to talk another one)
    • Instead of manually joining the arguments, you could:
      • Wrap the whole array in a List using Arrays::asList(String[])
      • Use List::subList(int, int) to exclude the first two elements
      • And lastly call String::join(String, List<String>) to join it together (where the first parameter is the 'glue', basically " " in your case)