Storing and Loading two locations from Hashmap?

Discussion in 'Plugin Development' started by ImpaTheImpaler, Sep 5, 2019.

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

    ImpaTheImpaler

    I am trying to store two locations from a hashmap to a yml file, and the storing works completely fine. However when I try to reload the values on reboot to set them to the hashmap, I get this error: https://prnt.sc/p251ka
    I have been trying to fix this for about half an hour now and I decided I needed some help. Here is the code from the storing class:

    Code:
    package shops;
    
    import main.Main;
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.configuration.file.YamlConfiguration;
    
    import java.io.File;
    import java.io.IOException;
    
    public class ShopFunctions {
    
        public static void saveData() {
            File data = new File(Main.instance.getDataFolder() + "/shops/npc_shops.yml");
            YamlConfiguration config = new YamlConfiguration();
    
            for (Location loc : ShopHandler.shop.keySet()) {
    
                Location loc1 = ShopHandler.shop.get(loc);
    
                String s = String.valueOf(loc.getWorld().getName()) + "," +
                        loc.getX() + "," + loc.getY() + "," + loc.getZ();
    
                String s1 = String.valueOf(loc1.getWorld().getName()) + "," +
                        loc1.getX() + "," + loc1.getY() + "," + loc1.getZ();
    
                config.set(s, s1); // trader loc, chest loc
    
                try {
                    config.save(data);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
    
        }
    
        public static void loadData() { // saving seems to work fine, its this function that appears broken
            File data = new File(Main.instance.getDataFolder() + "/shops/npc_shops.yml");
            YamlConfiguration config = new YamlConfiguration();
    
            try {
                config.load(data);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            for (String key : config.getKeys(false)) {
                String[] str = key.split(",");
                World world = Bukkit.getWorld(str[0]);
                double x = Double.valueOf(str[1]);
                double y = Double.valueOf(str[2]);
                double z = Double.valueOf(str[3]);
                double x1 = Double.valueOf(str[4]);
                double y1 = Double.valueOf(str[5]);
                double z1 = Double.valueOf(str[6]);
                Location chest = new Location(world, x1, y1, z1);
                Location ent = new Location(world, x, y, z);
                ShopHandler.shop.put(ent, chest);
            }
        }
    
    }
    
     
  2. Hi,
    Could you please provide some line numbers?
    It's very difficult to pinpoint the exact cause of the bug if we cant see line numbers.
     
  3. Offline

    ImpaTheImpaler

    The line that creates the error is at the end "double y = Double.valueOf(str[2]);"
     
  4. Offline

    CraftCreeper6

    @ImpaTheImpaler
    ArrayIndexOutOfBounds means that str[2] doesn't exist.
     
  5. Offline

    ImpaTheImpaler

    That makes sense, but the array is being created up in the save data file and there are definitely more than 3 values.
     
  6. Offline

    CraftCreeper6

    @ImpaTheImpaler
    Print out the value of "key" before you split the string.
     
  7. Offline

    ImpaTheImpaler

    For some reason my values are saving with a semi colon after them..
     
  8. Offline

    CraftCreeper6

  9. Offline

    ImpaTheImpaler

    I've managed to get the first location to save, but I can't seem to get the second to work. It currently saves like this
    world,-1296,64,-505: world,-1293,64,-508

    Code:
            for (String key : config.getKeys(false)) {
                Bukkit.getConsoleSender().sendMessage(key);
                String[] str = key.split(",");
                World world = Bukkit.getWorld(str[0]);
                double x = Double.valueOf(str[1]);
                double y = Double.valueOf(str[2]);
                double z = Double.valueOf(str[3]);
    
               
                String[] str1 = key.split(",");
    
    
                double x1 = Double.valueOf(str1[1]);
                double y1 = Double.valueOf(str1[2]);
                double z1 = Double.valueOf(str1[3]);
                Location chest = new Location(world, x1, y1, z1);
    
                Location ent = new Location(world, x, y, z);
                ShopHandler.shop.put(ent, chest);
            }
    How do I get it to read after the semi colon? Do I have to split it twice, once for the semi colon and once for the comma after?
     
  10. Offline

    timtower Administrator Administrator Moderator

    @ImpaTheImpaler That is just your editor.
    Open up notepad++ instead of the regular notepad.
     
  11. Offline

    Strahan

    You know Location implements ConfigurationSerializable, I'd just write the Locations to the config directly rather than mess around doing manual serialization.
     
Thread Status:
Not open for further replies.

Share This Page