Saving an ArrayList of Locations and loading them

Discussion in 'Plugin Development' started by HouseMD, Sep 30, 2012.

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

    HouseMD

    What would be the best way to save and load a bunch on Locations?
    i'm currently saving locations by saving two blocks that are used to select the area, saving the x,y,z coords and then recalculating the area every startup/reload and saving them into one arraylist.
    im sure saving them to their or arraylists would be much better however im not sure if its worth the trouble?

    i save the locations in a yml file, it works with all worlds and locations currently.

    my yml file looks like this as an example:

    Code:
    Arenas:
      Worlds:
        world:
          Name:
            test1:
              Locations:
                l1:
                  X: 36
                  Y: 68
                  Z: 226
                l2:
                  X: 37
                  Y: 68
                  Z: 227
    I load the blocks like this:

    Code:
        public static void getBlocks() {
            for(String s: w) {
                for(String b : a) {
                   
                    if(GGCPlugin.GameSettings.getConfigurationSection(
                        "Arenas.Worlds." + s + ".Name.").getKeys(false).contains(b)) {
                        Bukkit.broadcastMessage("w: "+ s);
                        Bukkit.broadcastMessage("b: "+ b);
                   
                    int l1x = GGCPlugin.GameSettings.getInt("Arenas.Worlds." + s + ".Name." + b + ".Locations.l1.X");
                    int l1y = GGCPlugin.GameSettings.getInt("Arenas.Worlds." + s + ".Name." + b + ".Locations.l1.Y");
                    int l1z = GGCPlugin.GameSettings.getInt("Arenas.Worlds." + s + ".Name." + b + ".Locations.l1.Z");
                   
                    int l2x = GGCPlugin.GameSettings.getInt("Arenas.Worlds." + s + ".Name." + b + ".Locations.l2.X");
                    int l2y = GGCPlugin.GameSettings.getInt("Arenas.Worlds." + s + ".Name." + b + ".Locations.l2.Y");
                    int l2z = GGCPlugin.GameSettings.getInt("Arenas.Worlds." + s + ".Name." + b + ".Locations.l2.Z");
                   
                    Location l1 = new Location(Bukkit.getWorld(s),l1x,l1y,l1z);
                    Location l2 = new Location(Bukkit.getWorld(s),l2x,l2y,l2z);
                   
                    Util.loadBlocks(l1, l2);
                    }
                }
            }
        }
    Simple calculations to get all blocks inside the area:

    Code:
       
    public static ArrayList<Location> gameArea = new ArrayList<>();
     
     
    public static void loadBlocks(Location l1, Location l2) {
            int mix, max, miy, miz, may, maz;
     
            // //
            if (l1.getBlockX() < l2.getBlockX()) {
                mix = l1.getBlockX();
                max = l2.getBlockX();
            } else {
                mix = l2.getBlockX();
                max = l1.getBlockX();
            }
     
            // //
     
            if (l1.getBlockY() < l2.getBlockY()) {
                miy = l1.getBlockY();
                may = l2.getBlockY();
            } else {
                miy = l2.getBlockY();
                may = l1.getBlockY();
            }
            // //
     
            if (l1.getBlockZ() < l2.getBlockZ()) {
                miz = l1.getBlockZ();
                maz = l2.getBlockZ();
            } else {
                miz = l2.getBlockZ();
                maz = l1.getBlockZ();
            }
            // //
     
            for (int x = mix; x <= max; x++) {
                for (int y = miy; y <= may; y++) {
                    for (int z = miz; z <= maz; z++) {
                        Location k = new Location(l1.getWorld(), x, y, z);
                        blocks.add(k);
                    }
                }
            }
     
            gameArea.addAll(blocks);
     
        }

    Any suggestions to improve it?
     
  2. Offline

    Sagacious_Zed Bukkit Docs

    Instead of manually deconstructing a Location's essential data, I create a proxy object that is ConfigurationSerializiable and feed that object straight to the Configuration API which can then read and write the object on its own.
     
Thread Status:
Not open for further replies.

Share This Page