Solved StringList to UUIDList

Discussion in 'Plugin Development' started by stamline, Mar 3, 2016.

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

    stamline

    I have a list of uuids that are strings. Just need to convert them to uuids and still want them in a list.
    They are strings just because i saved them to that in the config.

    Here is the list of strings:
    Code:
    List<String> list = plugin.getConfig().getStringList(player.getUniqueId().toString() + ".Friends");
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    stamline

    yeah, i know that. but i cant put in a list there. need to make a loop?
     
  4. Offline

    timtower Administrator Administrator Moderator

  5. Offline

    teej107

    @stamline keep in mind that the method will throw an error if the String can't be parsed to a UUID.
     
  6. @stamline you need to create a new list and copy the old lists content into the new one after converting the strings to uuids like timtower showed you in the post above. Like teej mentioned you also should catch the exception which will be thrown if the input string is illegal to make sure that corrupt uuids wont be used any further and your converting wont fail because some data aren't in the correct format
     
  7. Offline

    stamline

    Shouldn´t this work? I´m sure the string is a uuid, so no need to check that.
    Code:
    List<String> list = plugin.getConfig().getStringList(player.getUniqueId().toString() + ".Friends");
                    List<UUID> uuidlist = new ArrayList<UUID>();
                    for(int i=0;i<list.size();i++){
                        uuidlist.set(i, UUID.fromString(list.get(i)));
                    }
    list is the uuids in string format. And uuidlist is where i like to convert them to.
     
  8. Offline

    timtower Administrator Administrator Moderator

  9. Offline

    stamline

    But it doesn´t. Im checking by puting this in the loop.
    Code:
    player.sendMessage(uuidlist.get(i).toString());
     
    Last edited: Mar 5, 2016
  10. Offline

    timtower Administrator Administrator Moderator

  11. Offline

    stamline

    Code:
    else if(args[0].equalsIgnoreCase("list") && sender instanceof Player){
                Player player = (Player) sender;
                if(player.hasPermission("Friends.list")){
                    List<String> list = plugin.getConfig().getStringList(player.getUniqueId().toString() + ".Friends");
                    List<UUID> uuidlist = new ArrayList<UUID>();
                    for(int i=0;i<list.size();i++){
                        uuidlist.set(i, UUID.fromString(list.get(i)));
                    }
                }
            }
     
  12. Offline

    timtower Administrator Administrator Moderator

    @stamline And where is your message line?
     
  13. Offline

    stamline

    Removed it because it was not working, but i put it here.
    Code:
    else if(args[0].equalsIgnoreCase("list") && sender instanceof Player){
                Player player = (Player) sender;
                if(player.hasPermission("Friends.list")){
                    List<String> list = plugin.getConfig().getStringList(player.getUniqueId().toString() + ".Friends");
                    List<UUID> uuidlist = new ArrayList<UUID>();
                    for(int i=0;i<list.size();i++){
                        uuidlist.set(i, UUID.fromString(list.get(i)));
                        player.sendMessage(uuidlist.get(i).toString());
                    }
                }
            }
    This is how the config looks like
    Code:
    5e5179fa-5960-42c6-93d0-f701d6206806:
      Friends:
      - 5e5179fa-5960-42c6-93d0-f701d6206806
      - 76b03abb-99c4-4380-9480-38f1b4bbc818
    245bb240-a397-4fa2-9b06-e1afc0daa263:
      Friends:
      - 76b03abb-99c4-4380-9480-38f1b4bbc818
      - 5e5179fa-5960-42c6-93d0-f701d6206806
      - 245bb240-a397-4fa2-9b06-e1afc0daa263
    
    @timtower
    This is the error I get
    Code:
    [19:15:28] [Server thread/INFO]: AlbinMedoc issued server command: /friend list
    [19:15:28] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'friend' in plugin Friends v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-7d15d07-c194444]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-7d15d07-c194444]
        at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchCommand(CraftServer.java:645) ~[spigot.jar:git-Spigot-7d15d07-c194444]
        at net.minecraft.server.v1_9_R1.PlayerConnection.handleCommand(PlayerConnection.java:1302) [spigot.jar:git-Spigot-7d15d07-c194444]
        at net.minecraft.server.v1_9_R1.PlayerConnection.a(PlayerConnection.java:1137) [spigot.jar:git-Spigot-7d15d07-c194444]
        at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-7d15d07-c194444]
        at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-7d15d07-c194444]
        at net.minecraft.server.v1_9_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-7d15d07-c194444]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_51]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_51]
        at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile:45) [spigot.jar:git-Spigot-7d15d07-c194444]
        at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:716) [spigot.jar:git-Spigot-7d15d07-c194444]
        at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot.jar:git-Spigot-7d15d07-c194444]
        at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:655) [spigot.jar:git-Spigot-7d15d07-c194444]
        at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:554) [spigot.jar:git-Spigot-7d15d07-c194444]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_51]
    Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
        at java.util.ArrayList.rangeCheck(ArrayList.java:653) ~[?:1.8.0_51]
        at java.util.ArrayList.set(ArrayList.java:444) ~[?:1.8.0_51]
        at me.stamline.friends.Commands.Friend.onCommand(Friend.java:69) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-7d15d07-c194444]
        ... 15 more
    ..when i use..
    Code:
    else if(args[0].equalsIgnoreCase("list") && sender instanceof Player){
                Player player = (Player) sender;
                if(player.hasPermission("Friends.list")){
                    List<String> list = plugin.getConfig().getStringList(player.getUniqueId().toString() + ".Friends");
                    List<UUID> uuidlist = new ArrayList<UUID>();
                    for(int i=0;i<list.size();i++){
                        uuidlist.set(i, UUID.fromString(list.get(i)));
                        player.sendMessage(uuidlist.get(i).toString());
                    }
                }
            }
     
    Last edited: Mar 5, 2016
  14. Offline

    timtower Administrator Administrator Moderator

    @stamline Why not just use uuidlist.add instead of set
     
  15. Offline

    stamline

    Thanks man, that fixed it a bit. Now is the uuid showing in the chat. But im still getting a error message.

    Code:
    [23:46:34] [Server thread/INFO]: AlbinMedoc issued server command: /friend list
    [23:46:34] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'friend' in plugin Friends v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-7d15d07-c194444]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-7d15d07-c194444]
        at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchCommand(CraftServer.java:645) ~[spigot.jar:git-Spigot-7d15d07-c194444]
        at net.minecraft.server.v1_9_R1.PlayerConnection.handleCommand(PlayerConnection.java:1302) [spigot.jar:git-Spigot-7d15d07-c194444]
        at net.minecraft.server.v1_9_R1.PlayerConnection.a(PlayerConnection.java:1137) [spigot.jar:git-Spigot-7d15d07-c194444]
        at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-7d15d07-c194444]
        at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-7d15d07-c194444]
        at net.minecraft.server.v1_9_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-7d15d07-c194444]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_51]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_51]
        at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile:45) [spigot.jar:git-Spigot-7d15d07-c194444]
        at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:716) [spigot.jar:git-Spigot-7d15d07-c194444]
        at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot.jar:git-Spigot-7d15d07-c194444]
        at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:655) [spigot.jar:git-Spigot-7d15d07-c194444]
        at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:554) [spigot.jar:git-Spigot-7d15d07-c194444]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_51]
    Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
        at java.util.ArrayList.rangeCheck(ArrayList.java:653) ~[?:1.8.0_51]
        at java.util.ArrayList.get(ArrayList.java:429) ~[?:1.8.0_51]
        at me.stamline.friends.Commands.Friend.onCommand(Friend.java:70) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-7d15d07-c194444]
        ... 15 more
     
  16. Offline

    HoeMC

    Well, read it. You're trying to access an element of a list which isn't defined. Look at line 70 in your Friend class and you'll find the list causing the error.
     
  17. Offline

    stamline

    What do you mean, how can I define it?
    Im checking so the list isn´t 0.
    Code:
    if(list !=null && list.size() !=0){
                        for(int i=0;i<=list.size();i++){
    "Line 70"               uuidlist.add(UUID.fromString(list.get(i)));
                            player.sendMessage(uuidlist.get(i).toString());
                        }
                    }
     
  18. Offline

    HoeMC

    It's not that the list is null; you're trying to fetch an element outside of the list. Walk through your code and it's easy to see what's wrong. Pay attention to this line:
    Code:
    for(int i=0;i<=list.size();i++){
     
  19. Offline

    stamline

    wow, thank you. It should be like this.
    Code:
    for(int i=0;i<list.size();i++){
     
  20. Offline

    teej107

    @stamline Or you could use a for-each loop (aka enhanced for loop) :p
     
  21. Offline

    stamline

    @teej107 Yeah, that can work too
     
    Last edited: Mar 6, 2016
  22. @stamline if you are using java8 you could even go for lambda expressions :D would look smth like this then

    Make sure that the code is correct before you use it. i just came up with it without checking on the api

    Code:
    List<String> stringList = ...;
    List<UUID> uuidList = new ArrayList<>();
    stringList.forEach(entry-> uuidList.add(UUID.fromString(entry));
     
  23. Offline

    stamline

    thanks, that works.
     
Thread Status:
Not open for further replies.

Share This Page