Create a list of UUIDs, save it to a file, then load it again

Discussion in 'Plugin Development' started by marti.2001, Apr 3, 2013.

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

    marti.2001

    Hello! I want to save an a some mobs' UUIDs to a file when the server stops, then load them to a list. Please help me. Thank you! :)
     
  2. Offline

    NoLiver92

    just the id? Someone correct me if im wrong, but when the server starts, it resets the mob uuids, so the mobs would be different.

    what are you saving the uuids for?
     
  3. Offline

    marti.2001

    No, I mean UUID(unique ID). It doesn't change after restarting the server. I want to have some special zombies which drop more xp on death. They spawn on player death. I have written the whole plugin, but this.:)
     
  4. Offline

    NoLiver92

    getting the uuid and saving it to config is the easy bit. I misunderstood what you were after, i dont know how to spawn a mob using uuid. but ill do some looking as well.
     
  5. Offline

    marti.2001

    No, I don't need to spawn a mob using uuid. I just need to save a list of uuids and load them later.
     
  6. Offline

    Terradominik

    you simly make a List<String> and store the UUID's with .toString there you can get the UUID's of these strings with UUID.fromString(String name);
     
  7. Offline

    marti.2001

    Can you make an example? :)
     
  8. Offline

    skore87

    Look-up how to write and read an object from a file and you will find your answer.
     
  9. Offline

    the_merciless

    No, your right. The uuid will get changed over a restart. Best option would be to save the mobs location. And check it again on restart.
     
  10. Offline

    NoLiver92

    the_merciless wouldnt you need to find the mob type, any items they are holding plus meta data as well as the location to make sure the same entity type with extras is there on restart?
     
  11. Offline

    the_merciless

    I guess it depends how accurate you want to be. Though if there is the same mob type in the same location, I think you can probably assume it is the same one.
     
  12. Offline

    ZeusAllMighty11

  13. Offline

    NoLiver92

    the_merciless In his reply to me to start with he said the mobs are special and not ordinary, this could possibly cause an issue if he just used location
     
  14. Offline

    the_merciless

  15. Offline

    ase34

    Save to file:
    Code:java
    1. File file = new File("PATH");
    2. file.createNewFile();
    3.  
    4. foreach (UUID uuid: uuids) {
    5. dos.writeLong(uid.getMostSignificantBits());
    6. dos.writeLong(uid.getLeastSignificantBits());
    7. }
    8.  
    9. dos.close();
    10. fos.close();


    Read from file:
    Code:java
    1. List<UUID> uuids = new ArrayList<UUID>();
    2. File file = new File("PATH");
    3. file.createNewFile();
    4.  
    5. while (dis.available() > 0) {
    6. long msbs = dis.readLong();
    7. long lsbs = dis.readLong();
    8. UUID uuid = new UUID(msbs, lsbs);
    9. uuids.add(uuid);
    10. }
    11.  
    12. dis.close();
    13. fis.close();
     
  16. Offline

    skore87

    You can just save the entire object btw.
     
  17. Offline

    marti.2001

    I'm trying this, but can you tell me how to get the list from the config and make it List<String> again?:)
     
  18. Offline

    ase34

    Use UUID.fromString(String), but I prefer using numbers instead of Strings using getLeastSignificantBits() and getMostSignificantBits()
     
  19. Offline

    marti.2001

    ase34 Thank you, but my question was different... :)
    And how to use strings? This is a list.
     
  20. Offline

    NoLiver92

    do this (based on if its in your config file):
    Code:
    List<String> uuidlist = plugin.getConfig().getStringList("UUIDs");
    "UUIDs" is the node the list is under in the config
     
  21. Offline

    ase34

    marti.2001 Use config.getStringList(). For converting a list of string to a list of UUIDs, use

    Code:java
    1. List<String> strings = config.getStringList("path");
    2. List<UUID> uuids= new ArrayList<UUID>();
    3.  
    4. for (String string: strings) {
    5. uuids.add(UUID.fromString(string));
    6. }
     
  22. Offline

    marti.2001

    ase34 It throws an error on startup in this line:
    Code:java
    1. List<String> strings = config.getStringList("path");
     
  23. Offline

    ase34

    marti.2001 What exact error? Is the variable 'config' set?
     
  24. Offline

    marti.2001

    ase34 Yes, it's set. That's the error:
    Error (open)

    Code:
    2013-04-04 16:11:45 [SEVERE] Could not load 'plugins\DeathZombes.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.IllegalArgumentException: File cannot be null
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:182)
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305)
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
        at org.bukkit.craftbukkit.v1_5_R2.CraftServer.loadPlugins(CraftServer.java:239)
        at org.bukkit.craftbukkit.v1_5_R2.CraftServer.reload(CraftServer.java:603)
        at org.bukkit.Bukkit.reload(Bukkit.java:184)
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:23)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:188)
        at org.bukkit.craftbukkit.v1_5_R2.CraftServer.dispatchCommand(CraftServer.java:523)
        at org.bukkit.craftbukkit.v1_5_R2.CraftServer.dispatchServerCommand(CraftServer.java:512)
        at net.minecraft.server.v1_5_R2.DedicatedServer.am(DedicatedServer.java:261)
        at net.minecraft.server.v1_5_R2.DedicatedServer.r(DedicatedServer.java:226)
        at net.minecraft.server.v1_5_R2.MinecraftServer.q(MinecraftServer.java:474)
        at net.minecraft.server.v1_5_R2.MinecraftServer.run(MinecraftServer.java:407)
        at net.minecraft.server.v1_5_R2.ThreadServerApplication.run(SourceFile:573)
    Caused by: java.lang.IllegalArgumentException: File cannot be null
        at org.apache.commons.lang.Validate.notNull(Validate.java:203)
        at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(YamlConfiguration.java:170)
        at org.bukkit.plugin.java.JavaPlugin.reloadConfig(JavaPlugin.java:117)
        at org.bukkit.plugin.java.JavaPlugin.getConfig(JavaPlugin.java:111)
        at me.marti.2001.deathzombies.Main.<init>(Main.java:36) //Line 36 is the line that I said.
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:178)
        ... 14 more
    
     
  25. Offline

    ase34

    java.lang.IllegalArgumentException: File cannot be null

    The problem comes from the loading of the config, not from my code.
     
  26. Offline

    NoLiver92

    did to check to see if the file exists, if not save it?
     
  27. Offline

    marti.2001

    Without this code the plugin loads correctly. I copied the code that loads the config from one of my other plugins.
    My config.yml is in the jar where is plugin.yml. The plugin doesn't generate its folder. The problem is the same if I make the plugin folder and copy config.yml in it.
     
  28. Offline

    NoLiver92

    marti.2001 Im not entirely clear on that. In your main code if the file does not exist in the plugin folder on the server and you load it, you get this error. you need to do something like this before you load it:

    Code:
    saveDefaultConfig();
     
  29. Offline

    marti.2001

    NoLiver92 Yes, I have that in onEnable(). As I said, I copied the code for the config from one of my other plugins, which does everything correctly.
     
  30. Offline

    NoLiver92

    marti.2001 ok would you be able to post some code so we can have a look?
     
Thread Status:
Not open for further replies.

Share This Page