Solved Saving ArrayList in custom object

Discussion in 'Plugin Development' started by The_Spaceman, Oct 26, 2019.

  1. Offline

    The_Spaceman

    Hello,

    I want to save an ArrayList<UUID> inside of a custom object, this is what i have:
    Code:java
    1. ArrayList<UUID> uuidList = someList;
    2. @Override
    3. public Map<String, Object> serialize() {
    4. Map<String, Object> map = new HashMap<>();
    5. map.put("list", uuidList);
    6. return map;
    7. }
    8.  
    9. public static Object deserialize(Map<String, Object> args) {
    10. Object o = new Object();
    11. o.setList((ArrayList<UUID>) args.getOrDefault("list", new ArrayList<>()));
    12. }


    but when the server tries to deserialize I get the eror:
    Show Spoiler

    Code:java
    1. org.bukkit.configuration.InvalidConfigurationException: could not determine a constructor for the tag tag:yaml.org,2002:java.util.UUID
    2. in 'string', line 48, column 11:
    3. - !!java.util.UUID '4c386ae3-8e21- ...
    4. ^
    5.  
    6. at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:56) ~[server.jar:git-Spigot-9de398a-9c887d4]
    7. at org.bukkit.configuration.file.FileConfiguration.load(FileConfiguration.java:160) ~[server.jar:git-Spigot-9de398a-9c887d4]
    8. at org.bukkit.configuration.file.FileConfiguration.load(FileConfiguration.java:128) ~[server.jar:git-Spigot-9de398a-9c887d4]
    9. at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(YamlConfiguration.java:183) [server.jar:git-Spigot-9de398a-9c887d4]
    10. at com.spaceman.tport.fileHander.Files.reloadConfig(Files.java:31) [TPort.jar:?]
    11. at com.spaceman.tport.fileHander.Files.getConfig(Files.java:36) [TPort.jar:?]
    12. at com.spaceman.tport.Main.onEnable(Main.java:164) [TPort.jar:?]
    13. at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) [server.jar:git-Spigot-9de398a-9c887d4]
    14. at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:352) [server.jar:git-Spigot-9de398a-9c887d4]
    15. at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:417) [server.jar:git-Spigot-9de398a-9c887d4]
    16. at org.bukkit.craftbukkit.v1_14_R1.CraftServer.enablePlugin(CraftServer.java:461) [server.jar:git-Spigot-9de398a-9c887d4]
    17. at org.bukkit.craftbukkit.v1_14_R1.CraftServer.enablePlugins(CraftServer.java:375) [server.jar:git-Spigot-9de398a-9c887d4]
    18. at org.bukkit.craftbukkit.v1_14_R1.CraftServer.reload(CraftServer.java:810) [server.jar:git-Spigot-9de398a-9c887d4]
    19. at org.bukkit.Bukkit.reload(Bukkit.java:576) [server.jar:git-Spigot-9de398a-9c887d4]
    20. at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:27) [server.jar:git-Spigot-9de398a-9c887d4]
    21. at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) [server.jar:git-Spigot-9de398a-9c887d4]
    22. at org.bukkit.craftbukkit.v1_14_R1.CraftServer.dispatchCommand(CraftServer.java:710) [server.jar:git-Spigot-9de398a-9c887d4]
    23. at org.bukkit.craftbukkit.v1_14_R1.CraftServer.dispatchServerCommand(CraftServer.java:695) [server.jar:git-Spigot-9de398a-9c887d4]
    24. at net.minecraft.server.v1_14_R1.DedicatedServer.handleCommandQueue(DedicatedServer.java:433) [server.jar:git-Spigot-9de398a-9c887d4]
    25. at net.minecraft.server.v1_14_R1.DedicatedServer.b(DedicatedServer.java:397) [server.jar:git-Spigot-9de398a-9c887d4]
    26. at net.minecraft.server.v1_14_R1.MinecraftServer.a(MinecraftServer.java:971) [server.jar:git-Spigot-9de398a-9c887d4]
    27. at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:816) [server.jar:git-Spigot-9de398a-9c887d4]
    28. at java.lang.Thread.run(Unknown Source) [?:1.8.0_231]
    29. Caused by: org.yaml.snakeyaml.constructor.ConstructorException: could not determine a constructor for the tag tag:yaml.org,2002:java.util.UUID
    30. in 'string', line 48, column 11:
    31. - !!java.util.UUID '4c386ae3-8e21- ...
    32. ^
    33.  
    34. at org.yaml.snakeyaml.constructor.SafeConstructor$ConstructUndefined.construct(SafeConstructor.java:541) ~[server.jar:git-Spigot-9de398a-9c887d4]
    35. at org.yaml.snakeyaml.constructor.BaseConstructor.constructObjectNoCheck(BaseConstructor.java:204) ~[server.jar:git-Spigot-9de398a-9c887d4]
    36. at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:193) ~[server.jar:git-Spigot-9de398a-9c887d4]
    37. at org.yaml.snakeyaml.constructor.BaseConstructor.constructSequenceStep2(BaseConstructor.java:364) ~[server.jar:git-Spigot-9de398a-9c887d4]
    38. at org.yaml.snakeyaml.constructor.BaseConstructor.constructSequence(BaseConstructor.java:348) ~[server.jar:git-Spigot-9de398a-9c887d4]
    39. at org.yaml.snakeyaml.constructor.SafeConstructor$ConstructYamlSeq.construct(SafeConstructor.java:499) ~[server.jar:git-Spigot-9de398a-9c887d4]
    40. at org.yaml.snakeyaml.constructor.BaseConstructor.constructObjectNoCheck(BaseConstructor.java:204) ~[server.jar:git-Spigot-9de398a-9c887d4]
    41. at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:193) ~[server.jar:git-Spigot-9de398a-9c887d4]
    42. at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping2ndStep(BaseConstructor.java:453) ~[server.jar:git-Spigot-9de398a-9c887d4]
    43. at org.yaml.snakeyaml.constructor.SafeConstructor.constructMapping2ndStep(SafeConstructor.java:184) ~[server.jar:git-Spigot-9de398a-9c887d4]
    44. at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping(BaseConstructor.java:434) ~[server.jar:git-Spigot-9de398a-9c887d4]
    45. at org.yaml.snakeyaml.constructor.SafeConstructor$ConstructYamlMap.construct(SafeConstructor.java:521) ~[server.jar:git-Spigot-9de398a-9c887d4]
    46. at org.bukkit.configuration.file.YamlConstructor$ConstructCustomObject.construct(YamlConstructor.java:28) ~[server.jar:git-Spigot-9de398a-9c887d4]
    47. at org.yaml.snakeyaml.constructor.BaseConstructor.constructObjectNoCheck(BaseConstructor.java:204) ~[server.jar:git-Spigot-9de398a-9c887d4]
    48. at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:193) ~[server.jar:git-Spigot-9de398a-9c887d4]
    49. at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping2ndStep(BaseConstructor.java:453) ~[server.jar:git-Spigot-9de398a-9c887d4]
    50. at org.yaml.snakeyaml.constructor.SafeConstructor.constructMapping2ndStep(SafeConstructor.java:184) ~[server.jar:git-Spigot-9de398a-9c887d4]
    51. at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping(BaseConstructor.java:434) ~[server.jar:git-Spigot-9de398a-9c887d4]
    52. at org.yaml.snakeyaml.constructor.SafeConstructor$ConstructYamlMap.construct(SafeConstructor.java:521) ~[server.jar:git-Spigot-9de398a-9c887d4]
    53. at org.bukkit.configuration.file.YamlConstructor$ConstructCustomObject.construct(YamlConstructor.java:28) ~[server.jar:git-Spigot-9de398a-9c887d4]
    54. at org.yaml.snakeyaml.constructor.BaseConstructor.constructObjectNoCheck(BaseConstructor.java:204) ~[server.jar:git-Spigot-9de398a-9c887d4]
    55. at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:193) ~[server.jar:git-Spigot-9de398a-9c887d4]
    56. at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping2ndStep(BaseConstructor.java:453) ~[server.jar:git-Spigot-9de398a-9c887d4]
    57. at org.yaml.snakeyaml.constructor.SafeConstructor.constructMapping2ndStep(SafeConstructor.java:184) ~[server.jar:git-Spigot-9de398a-9c887d4]
    58. at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping(BaseConstructor.java:434) ~[server.jar:git-Spigot-9de398a-9c887d4]
    59. at org.yaml.snakeyaml.constructor.SafeConstructor$ConstructYamlMap.construct(SafeConstructor.java:521) ~[server.jar:git-Spigot-9de398a-9c887d4]
    60. at org.bukkit.configuration.file.YamlConstructor$ConstructCustomObject.construct(YamlConstructor.java:28) ~[server.jar:git-Spigot-9de398a-9c887d4]
    61. at org.yaml.snakeyaml.constructor.BaseConstructor.constructObjectNoCheck(BaseConstructor.java:204) ~[server.jar:git-Spigot-9de398a-9c887d4]
    62. at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:193) ~[server.jar:git-Spigot-9de398a-9c887d4]
    63. at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping2ndStep(BaseConstructor.java:453) ~[server.jar:git-Spigot-9de398a-9c887d4]
    64. at org.yaml.snakeyaml.constructor.SafeConstructor.constructMapping2ndStep(SafeConstructor.java:184) ~[server.jar:git-Spigot-9de398a-9c887d4]
    65. at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping(BaseConstructor.java:434) ~[server.jar:git-Spigot-9de398a-9c887d4]
    66. at org.yaml.snakeyaml.constructor.SafeConstructor$ConstructYamlMap.construct(SafeConstructor.java:521) ~[server.jar:git-Spigot-9de398a-9c887d4]
    67. at org.bukkit.configuration.file.YamlConstructor$ConstructCustomObject.construct(YamlConstructor.java:28) ~[server.jar:git-Spigot-9de398a-9c887d4]
    68. at org.yaml.snakeyaml.constructor.BaseConstructor.constructObjectNoCheck(BaseConstructor.java:204) ~[server.jar:git-Spigot-9de398a-9c887d4]
    69. at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:193) ~[server.jar:git-Spigot-9de398a-9c887d4]
    70. at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping2ndStep(BaseConstructor.java:453) ~[server.jar:git-Spigot-9de398a-9c887d4]
    71. at org.yaml.snakeyaml.constructor.SafeConstructor.constructMapping2ndStep(SafeConstructor.java:184) ~[server.jar:git-Spigot-9de398a-9c887d4]
    72. at org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping(BaseConstructor.java:434) ~[server.jar:git-Spigot-9de398a-9c887d4]
    73. at org.yaml.snakeyaml.constructor.SafeConstructor$ConstructYamlMap.construct(SafeConstructor.java:521) ~[server.jar:git-Spigot-9de398a-9c887d4]
    74. at org.bukkit.configuration.file.YamlConstructor$ConstructCustomObject.construct(YamlConstructor.java:28) ~[server.jar:git-Spigot-9de398a-9c887d4]
    75. at org.yaml.snakeyaml.constructor.BaseConstructor.constructObjectNoCheck(BaseConstructor.java:204) ~[server.jar:git-Spigot-9de398a-9c887d4]
    76. at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:193) ~[server.jar:git-Spigot-9de398a-9c887d4]
    77. at org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor.java:159) ~[server.jar:git-Spigot-9de398a-9c887d4]
    78. at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:146) ~[server.jar:git-Spigot-9de398a-9c887d4]
    79. at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:524) ~[server.jar:git-Spigot-9de398a-9c887d4]
    80. at org.yaml.snakeyaml.Yaml.load(Yaml.java:437) ~[server.jar:git-Spigot-9de398a-9c887d4]
    81. at org.bukkit.configuration.file.YamlConfiguration.loadFromString(YamlConfiguration.java:54) ~[server.jar:git-Spigot-9de398a-9c887d4]
    82. ... 22 more


    and it can't deserialize the ArrayList. But saving the ArrayList like file.getConfigFile().set("somePlace", list); works... but that is not what I want.
    I only get errors when deserializing...

    and yes, I did register the class using:
    ConfigurationSerialization.registerClass(SomeClass.class, "SomeClass");

    And I also know it has its issues with deserializing Enums, now I save the name and use Enum.valueOf(name).

    What should I do with saving the ArrayList?
    And is there a better way to save enums and getting the enums using the ConfigurationSerializable?

    Thank you
     
    Last edited: Oct 26, 2019
  2. Offline

    robertlit

    Just do HashMap<String, ArrayList<UUID>>
     
  3. Offline

    The_Spaceman

    I don't understand you with 'just do'.
    I have the feeling that this also won't work:
    Code:java
    1. @Override
    2. public Map<String, Object> serialize() {
    3. Map<String, Object> map = new HashMap<>();
    4. Map<String, UUID> newMap = new HashMap<>();
    5. newMap.put("IDontKnow", uuidList);
    6. map.put("list", newMap);
    7. return map;
    8. }


    Or is this not wat you ment?
    maybe I can save it as a base64 string object?
     
  4. Offline

    Zombie_Striker

    @The_Spaceman
    UUID (for some reason) are not serializable. Because of this, when saving uuids, you will need to convert the List<UUID> to a List<String> and just convert all uuids to strings using .toString(). Then, when loading the uuids, use UUID.fromString(....) to get the uuid object.
     
  5. Offline

    The_Spaceman

    yes this works, thank you

    to deserialize:
    ArrayList<UUID> list = (((ArrayList<String>) args.getOrDefault("list", new ArrayList<>())).stream().map(UUID::fromString).collect(Collectors.toCollection(ArrayList::new)));

    to serialize:
    map.put("list", list.stream().map(UUID::toString).collect(Collectors.toList()));
     

Share This Page