Saving hashmap homes players

Discussion in 'Plugin Development' started by KAM202, Jul 7, 2019.

  1. Offline

    KAM202

    Hey! I want save a hashmap with players homes

    My HashMap declaration:
    Code:
    public HashMap<UUID, HomeClass> homeData = new HashMap<UUID, HomeClass>();
    My HomeClass:
    Code:
    public class HomeClass {
    
        UUID uuid;
        String nazwa;
        Location loc;
      
        public HomeClass(UUID cuuid, String cnazwa, Location cloc) {
            uuid = cuuid;
            nazwa = cnazwa;
            loc = cloc;
        }
      
        public Location getLocation() {
            return loc;
        }
        public UUID getUUID() {
            return uuid;
        }
        public String getName() {
            return nazwa;
        }
    }
    Now how i can save in file like this:
    Code:
    homes:
      name:
        location...
    name:
       location...
    I cant getKey from hashmap becouse i must use "UUID" so how do it?
     
  2. Online

    timtower Ninja on the waves Moderator

    @KAM202 You first need to rethink your config.
    What would name be? Do you need it if you have the UUID already?
     
  3. Offline

    KAM202

    @timtower
    Yes, because every player can have several homes.

    So I have to do something like that: HashMap holding a specific player with a class that has all its homes.
     
  4. Online

    timtower Ninja on the waves Moderator

    Then you need a list of HomeClasses in the HashMap, not a single one.
    Or multiple homes in the HomeClass.
    Either way: you need to change that.
     
  5. Offline

    KAM202

    @timtower
    like this?
    Code:
    public HashMap<HomeClass> homeData = new HashMap<HomeClass>();
    or what?
     
  6. Online

    timtower Ninja on the waves Moderator

    Well, that won't compile.
    Depends on how you want to store everything.
    Do you want 1 HomeClass instance for each player or multiple?
     
  7. Offline

    KAM202

    @timtower for multiple.
    I want HomeClass to store all home for all players
     
  8. Online

    timtower Ninja on the waves Moderator

    Then you need Map<UUID, HomeClass>
    And a map inside the homeclass for the home locations.
     
  9. Offline

    KAM202

    @timtower
    ok done but i need now get location from file with keys and values:
    Code:
    homes:
      home:
        ==: org.bukkit.Location
        world: world
        x: 44.84774124063776
        y: 255.0
        z: 34.24123266017826
        pitch: 43.18517
        yaw: 163.58963
    and can i import this location like saving? (p.getLocation())
    ?
     
  10. Online

    timtower Ninja on the waves Moderator

    @KAM202 You might want to serialize it yourself, getting it back is very annoying.
    And like said: you might want to reconsider your saving method.
    Try this:
    Code:
    homes:
      uuid:
         homename: location
         homename2: location2
     
  11. Offline

    KAM202

    @timtower nooo this home i have in another file not homes.yml - UUID.yml
    and here i have actually:
    Code:
    nick: Laureanel
    god: false
    money: 1000
    lasttimelogin: 1562519083501
    homes:
      home:
        ==: org.bukkit.Location
        world: world
        x: 44.84774124063776
        y: 255.0
        z: 34.24123266017826
        pitch: 43.18517
        yaw: 163.58963
    and i want get all home name's and location's from file but standard for(int ...) will not work..
     
  12. Online

    timtower Ninja on the waves Moderator

    @KAM202 configurationsection#getKeys(false) will do the trick.
    homes being a configurationsection.
     
  13. Offline

    KAM202

    Code:
    for(String s : section.getKeys(true)) {
                    for(String key : config.getConfigurationSection(s).getKeys(false)) {
                        World w = Bukkit.getServer().getWorld(config.getString(key + ".world"));
                        double x = config.getDouble(key + ".x");
                        double y = config.getDouble(key + ".y");
                        double z = config.getDouble(key + ".z");
                        homeData.get(p.getUniqueId()).hLOC.put(config.getString(key), new Location(w, x, y, z));
                    }
                }
    giving me NULL in "for(String key : config.getConfigurationSection(s).getKeys(false))"
     
  14. Online

    timtower Ninja on the waves Moderator

    @KAM202 Well, that highly depends on the rest of your code that I can't see.
    I am just gonna assume that section is a part of config, config is the full file, and that you are trying to get configurationSection(s) that doesn't exist because it is a child of section.
     
  15. Offline

    KAM202

    @timtower

    Full import homes from file:
    Code:
    ConfigurationSection section = config.getConfigurationSection("homes");
                if (section == null) {
                    homeData.get(p.getUniqueId()).hLOC = new HashMap<String, Location>();
                    return;
                }
                for(String s : section.getKeys(false)) {
                        World w = Bukkit.getServer().getWorld(config.getString("homes." + s + ".world"));
                        double x = config.getDouble("homes." + s + ".x");
                        double y = config.getDouble("homes." + s + ".y");
                        double z = config.getDouble("homes." + s + ".z");
                        homeData.get(p.getUniqueId()).hLOC.put(config.getString("homes." + s), new Location(w, x, y, z));
                  
                }
    still giving error..I understand correctly:
    Code:
    for(String s : section.getKeys(false)) {
    will give me home's name's like "home1, home2, home3 ..."
    so i uses s + ".world" then will give me homes.s.world ?
     
  16. Online

    timtower Ninja on the waves Moderator

    @KAM202 Why don't you do section.getString(s+".world") ?
    You already have the section, so why not use it?
     
  17. Offline

    KAM202

    @timtower
    again
    Code:
    World w = Bukkit.getServer().getWorld(section.getString(s + ".world"));
    giving: name cannot be null

    and how i can set empty hashmap if player hasn't any home's? (key: homes: not existing)
     
  18. Online

    timtower Ninja on the waves Moderator

    @KAM202 Then the value does not exist in the config.
    And you can also just have an empty map.
     
  19. Offline

    KAM202

    not exist? D:
    Code:
    nick: Laureanel
    god: false
    op: false
    money: 1000
    lasttimelogin: 1562523516875
    homes:
      jd:
        ==: org.bukkit.Location
        world: world
        x: 48.786328849562416
        y: 255.0
        z: 34.713229105979195
        pitch: 25.759005
        yaw: -349.99896
      home:
        ==: org.bukkit.Location
        world: world
        x: 50.04313815892542
        y: 256.0
        z: 42.565332186285026
        pitch: 25.759005
        yaw: -349.99896
     
  20. Online

    timtower Ninja on the waves Moderator

    @KAM202 Nope, because you are still using the default saving method which saves class, doesn't save everything as a map what you need.
     
  21. Offline

    KAM202

    @timtower

    can you give an example? Do you mean to save the location?
     
  22. Online

    timtower Ninja on the waves Moderator

    @KAM202 I do mean how you save the location.
    You need to set the values yourself, not set the entire Location in 1 call.
     

Share This Page