Solved Getting all existing players

Discussion in 'Spigot Plugin Development' started by MaxKrissigo, May 15, 2016.

  1. How would I go about checking whether a command argument is equal to an existing player who has joined the server before? Thanks Max.
     
  2. Are you looking to check if the player is online now, or if he has ever been on?
     
  3. Ever been on.
     
  4. Well first you would have to create a listener that adds every player that joins to a Hashmap, then simply check if that player is in the hashmap.
     
  5. Well, if you mean you want the args to get a player, then that's easy. But if you want all the exciting that have joined the server before, that gets a little tricky. You could either make a config and add whenever a player joins, it adds it to there, and if its already there, don;t. Or you could make a Array-list with the same mechanics as the config, but would be wiped when the server was restarted. Both are not very good to do, because they are likely gonna get really big, quite fast.
     
  6. Tokyojack is right. Your best bet would be to use SQL to save the players
     
  7. I'll try that out, thanks.
    EDIT: I do have a player config, but it stores their UUIDs
     
  8. I agree. ^
     
    • Agree Agree x 1
  9. Ok so I think I know what I have to do, but in the first place, I'd need to check whether it is existent to any player, and then change that into a UUID and check. How would I check if it is a Minecraft player? Is there a Mojang API for that?
     
  10. Try using this
    Code (Text):
    Bukkit.getOfflinePlayer(UUID);
     
  11. I just did a little research, and you dont even need to save all the player info, Spigot does that for you. Use the OfflinePlayer object instead
    Code (Text):
            OfflinePlayer player = Bukkit.getOfflinePlayer("Notch");
            if (player.hasPlayedBefore()){
                //Do something
            }
     
    • Agree Agree x 1
  12. Try this if these things if that doesn't work!
    Code (Text):
    public static UUID fetchUUID(String s) throws Exception {
        URL url = new URL("https://api.mojang.com/users/profiles/minecraft/" + s);
        String uuid = (String)((JSONObject) new JSONParser().parse(new InputStreamReader(url.openStream()))).get("id");
        String realUUID = uuid.substring(0, 8) + "-" + uuid.substring(8, 12) + "-" + uuid.substring(12, 16) + "-" + uuid.substring(16, 20) + "-" + uuid.substring(20, 32);
        return UUID.fromString(realUUID)
    }

    String argsuuid = fetchUUID(args[0]);
    //this gets the uuid from a player name
     
    and if that doesn't work:

    Code (Text):
    String uuidname = getServer().getOfflinePlayer(UUID).getName();
    //this gets the uuid from a player name thats offline
     
    I haven't tested these things, but they look like they would work.
     
  13. Hmm, didn't know that! The more you know, eh? :eek:
     
  14. I tried that, but I'm getting null and tones of errors in the console when I type /stats <username>. The error is saying it is on line 288. Here is the error and my code:
    Code (Text):
    null
    org.bukkit.command.CommandException: Unhandled exception executing command 'stats' in plugin MainPlugin v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-e6f93f4-935f18b]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-e6f93f4-935f18b]
        at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchCommand(CraftServer.java:645) ~[spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.PlayerConnection.handleCommand(PlayerConnection.java:1350) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.PlayerConnection.a(PlayerConnection.java:1185) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_92]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_92]
        at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile:45) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:721) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:660) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:559) [spigot.jar:git-Spigot-e6f93f4-935f18b]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_92]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
        at me.MaxKrissigo.MainPlugin.Main.onCommand(Main.java:288) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-e6f93f4-935f18b]
        ... 15 more
    >
     
    Code:
    Code (Text):
                if (command.getName().equalsIgnoreCase("stats") && sender.hasPermission("mainplugin.stats")) {
                 
                    Player p = (Player) sender;
                 
                    if (args.length == 0) {
                     
                        p.sendMessage("You must specify a player!");
                     
                    } else if (args.length == 1) {
                     
                        String name = args[1]; //this line is 288
                     
                        OfflinePlayer player = Bukkit.getOfflinePlayer(args[1]);
                        if (player.hasPlayedBefore()){
                         
                            p.sendMessage("YES");
                         
                         
                        } else {
                         
                            p.sendMessage(name + " has never joined the server!");
                         
                        }
                     
                        invs = Bukkit.getServer().createInventory(null, 27, name + "'s Stats");
                     
                        deathss = createItem2(Material.DIAMOND, ChatColor.RED + "Deaths");
                        killss = createItem2(Material.DIAMOND, ChatColor.RED + "Kills");
                        killstreaks = createItem2(Material.DIAMOND, ChatColor.RED + "Kill Streak");
                     
                        invs.setItem(12, killss);
                        invs.setItem(13, deathss);
                        invs.setItem(13, killstreaks);
                         
                    }
                }
     
  15. You are checking if the args is at least 1 long, but then you are trying to access the second argument. Dont forget that in java counting starts at 0!
    Code (Text):
    args[1] //this is the SECOND argument
     
    • Like Like x 1
  16. I keep making those dumb argument mistakes! xD. Thanks for the help it worked!