Solved How can I load locations to a file?

Discussion in 'Plugin Development' started by bernivic135, Feb 21, 2016.

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

    bernivic135

    Hi, i'm creating a home manager for my plugin. I have created the custom file and all of that. I can save locations, but I don't know how to load the information. I wan't to load locations.

    The code that I have:
    Code:
    public class HomeManager {
    
        private static Map<UUID, Location> locations = new HashMap<UUID, Location>();
    
        public static void addLocation(Player p, Location loc) {
            locations.put(p.getUniqueId(), loc);
        }
    
        public static void loadLocations(FileConfiguration file) {
            // Where I need the new code.
        }
    
        public static void saveLocations(FileConfiguration file) {
            for (Map.Entry<UUID, Location> entry : locations.entrySet()) {
                file.set("homes." + entry.getKey().toString() + ".x", entry.getValue().getBlockX());
                file.set("homes." + entry.getKey().toString() + ".y", entry.getValue().getBlockY());
                file.set("homes." + entry.getKey().toString() + ".z", entry.getValue().getBlockZ());
                file.set("homes." + entry.getKey().toString() + ".world", entry.getValue().getWorld());
            }
        }
    }
     
  2. Offline

    elian1203

    Make sure to save the file after saving the locations, or else nothing will happen and you will be confused. To load them just use things like:
    double x = file.getDouble("homes." + home + ".x");
    and then for the world you'd use getServer().getWorld();
     
  3. Offline

    bernivic135

    Yes, but to iterate through all the entries in the yml file, how can I do it?
     
  4. Offline

    Xerox262

    @bernivic135 Same way you do it with the hashmap, except using FileConfiguration#entrySet() woops I meant MemorySection#getValues(); which will return you a map, which you can iterate through like you did your hashmap. Also I suggest storing the world using the world name, and getting it with Bukkit.getWorld();
     
  5. Offline

    elian1203

    for (String home : file.getConfigurationSection("homes").getKeys(false)) {}
     
  6. Offline

    Xerox262

    @elian1203 That would only get the keys, he needs an entry set to get the value.
     
  7. Offline

    elian1203

    @Xerox262 That would get all the homes, which he could do file.get("homes." + home + ".x"); etc...
     
  8. Offline

    bernivic135

    Hi. I can't load the locations to the hashmap. It gives me no error.
    My Code:
    Code:
    public class HomeManager {
    
        private static Map<UUID, Location> locations = new HashMap<UUID, Location>();
    
        public static void addLocation(Player p, Location loc) {
            locations.put(p.getUniqueId(), loc);
        }
    
        public static boolean containsPlayer(Player p) {
            if(locations.containsKey(p.getUniqueId())) return true;
            else return false;
        }
    
        public static Location getLocation(Player p) {
            if(locations.containsKey(p.getUniqueId())) {
                return locations.get(p.getUniqueId());
            } else {
                return null;
            }
        }
    
        public static void removePlayer(Player p) {
            locations.remove(p.getUniqueId());
        }
    
        public static void loadLocations(FileConfiguration file) {
            for (String home : file.getConfigurationSection("homes").getKeys(false)) {
                UUID id = UUID.fromString(home);
                locations.put(id, new Location(Bukkit.getWorld(file.get("homes." + home + "world").toString()),
                        Double.parseDouble(file.get("homes." + home + ".x").toString()),
                        Double.parseDouble(file.get("homes." + home + ".y").toString()),
                        Double.parseDouble(file.get("homes." + home + ".z").toString())));
            }
        }
    
        public static void saveLocations(FileConfiguration file) {
            for (Map.Entry<UUID, Location> entry : locations.entrySet()) {
                file.set("homes." + entry.getKey().toString() + ".x", entry.getValue().getX());
                file.set("homes." + entry.getKey().toString() + ".y", entry.getValue().getY());
                file.set("homes." + entry.getKey().toString() + ".z", entry.getValue().getZ());
                file.set("homes." + entry.getKey().toString() + ".world", entry.getValue().getWorld().getName());
            }
        }
    }
    
    And my onEnable() and onDisable():
    Code:
    public void onEnable() {
    
            getLogger().info("Infinity Kingdom v" + getDescription().getVersion() + " has been enabled!");
            listeners();
    
            homeLocationsFile = new File(getDataFolder(), "homeLocations.yml");
            homeLocations = YamlConfiguration.loadConfiguration(homeLocationsFile);
    
            if(Bukkit.getWorld("lobby") == null) {
    
                WorldCreator wc = new WorldCreator("lobby");
                wc.environment(World.Environment.NORMAL);
                wc.createWorld();
    
            }
    
            getCommand("lobby").setExecutor(new LobbyCommand());
            getCommand("home").setExecutor(new HomeCommand());
    
            SQL.openConnection();
            HomeManager.loadLocations(homeLocations);
            saveLocationsConfig();
    
            instance = this;
    
        }
    
        @Override
        public void onDisable() {
    
            getLogger().info("Infinity Kingdom v" + getDescription().getVersion() + " has been disabled!");
            instance = null;
            HomeManager.saveLocations(homeLocations);
            saveLocationsConfig();
    
            SQL.closeConnection();
    
        }
     
    Last edited: Feb 22, 2016
  9. Offline

    bernivic135

  10. Offline

    mythbusterma

    @bernivic135

    First off, none of that should be static. Just rip all the static out of that class.

    Second, try debugging it, print out what is happening. I'm sure you'll spot the error.

    Third, clean up that code. The code you have loading that location is an absolute mess and probably contains the error. Clean it up so it'll be easier to work with.

    Also, if you're using 1.8+, Locations implement ConfigurationSerializable, which means they can be saved like anything else to the config.
     
  11. Offline

    bernivic135

    @mythbusterma
    But how can then access the save locations from the onEnable()?
     
  12. Offline

    I Al Istannen

  13. Offline

    Zombie_Striker

    Use getters and setters to get those fields.
     
  14. Offline

    bernivic135

    @Zombie_Striker
    But how can I access then a non-static field from a static context?
     
  15. Offline

    teej107

    Think Object Oriented Programming (OOP)!
     
  16. Offline

    bernivic135

    @Zombie_Striker
    I know, for the moment everything is static.
    Code:
    private static Map<UUID, Location> locations = new HashMap<UUID, Location>();
    
        public static void addLocation(Player p, Location loc) {
            locations.put(p.getUniqueId(), loc);
        }
    
        public static boolean containsPlayer(Player p) {
            if(locations.containsKey(p.getUniqueId())) return true;
            else return false;
        }
    
        public static Location getLocation(Player p) {
            if(locations.containsKey(p.getUniqueId())) {
                return locations.get(p.getUniqueId());
            } else {
                return null;
            }
        }
    
        public static void removePlayer(Player p) {
            locations.remove(p.getUniqueId());
        }
    
        public static void loadLocations(FileConfiguration file) {
            Iterator it = file.getConfigurationSection("homes").getKeys(true).iterator();
            while(it.hasNext() == true) {
                UUID id = UUID.fromString(it.next().toString());
                locations.put(id, Location.deserialize(file.getConfigurationSection("homes." + id.toString()).getValues(true)));
            }
        }
    
        public static void saveLocations(FileConfiguration file) {
            for (Map.Entry<UUID, Location> entry : locations.entrySet()) {
                file.set("homes." + entry.getKey().toString() + ".location", entry.getValue().serialize().toString());
            }
        }
    But when I save the file looks like this:
    Code:
    homes:
      0021415e-472d-40ef-871a-9754360d3868:
        x: 3.1580942120227444
        y: 4.0
        z: 8.26350389481183
        world: lobby
        location: '{world=lobby, x=3.9923329890381307, y=4.0, z=27.302850522341664, pitch=0.0,
          yaw=0.0}'
    
    And it teleports me to a different location than the one I saved.
     
    Last edited: Feb 23, 2016
  17. Offline

    Zombie_Striker

    @bernivic135
    • Create HomeManager in the main class.
    • After creating HomeManager, create all the other classes that use HomeManager
    • Pass the instance of HomeManager through the other class's constructor.
    • Use that instance of HomeManager that was passed through the constructor to access all of it's methods.
    The main idea is to pass around the one instance instead of abusing static. You know, Object Oriented Programming.

    As for your other problem, can you post what you mean by teleporting to a different location?
     
  18. Offline

    bernivic135

    @Zombie_Striker
    It teleports me to the first location, the one that isn't on location after I reset the server

    Ok, so I someway managed to get It to work! Thanks for the support.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 24, 2016
Thread Status:
Not open for further replies.

Share This Page