Solved Reading other configuration files: null

Discussion in 'Plugin Development' started by AlarmedDino, Sep 11, 2016.

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

    AlarmedDino

    I am crafting the classic homes plugin where you can set a home with /sethome and teleport back to that location with /home. I have it all working, when the player issues the command /sethome their location is written to "homes.yml" and read from such file when /home is issued. This works perfectly. The problem comes when I want to import homes from SimpleHomes, the plugin i had installed while i was developing this one. SimpleHomes saves the player's homes into a configuration file, "Homes.yml". The data is saved in the following way:
    Code:
    PlayerName:
        homeName:
            world: worldName
            x: integer
            y: integer
            z: integer
    I save my data in pretty much the exact same way, the only differences being that instead of integers i use doubles and instead of the player's name i use the UUID. I renamed the SimpleHomes Homes.yml file to simpleHomes.yml and put it into my plugins data folder. I created an event listener that attempts to convert their home to my format by reading the data from "simpleHomes.yml" and write it to "homes.yml". Every single time i have tried to read the data from the SimpleHomes file it reads null for the world name string and 0 for the integers. I know it can access the "simpleHomes.yml" file because it does remove the player's node from the config file. Sorry if this was a little wordy.

    EventListener for onPlayerJoin (Is supposed to do the conversion to my format):
    Code:
    public class EventPlayerListener implements Listener {
    
        JavaPlugin p;
        FileConfiguration fc, homeConfig;
        ConfigHelper homeCH;
     
        public EventPlayerListener(JavaPlugin p, ConfigHelper homeCH) {
            this.p = p;
            this.homeCH = homeCH;
            this.homeConfig = homeCH.getConfig();
         
            ConfigHelper ch = new ConfigHelper(p, "simpleHomes.yml");
            fc = ch.getConfig();
            ch.load();
        }
     
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            Player player = e.getPlayer();
            String name = player.getName();
         
            setHome(player, "default", fc.getString(name + ".default.world"), fc.getDouble(name + ".default.x"), fc.getDouble(name+".default.y"), fc.getDouble(name+".default.z"));
            player.sendMessage(fc.getString(name+".default.world")+ " " + fc.getDouble(name+".default.x") + " " + fc.getInt(name+".default.x"));
            fc.set(player.getName(), null);
        }
     
        public void setHome(Player player, String home, String world, double x, double y, double z) {
            String uuid = player.getUniqueId().toString();
            homeConfig.set(uuid + "." + home+".world", world);
            homeConfig.set(uuid + "." + home+".x", x);
            homeConfig.set(uuid + "." + home+".y", y+0.2);
            homeConfig.set(uuid + "." + home+".z", z);
            homeConfig.set(uuid+".homeNum", homeConfig.getInt(uuid+".homeNum")+1);
            homeCH.saveConfig();
        }
    }
    ConfigHelper:
    Code:
    public class ConfigHelper {
    
        private File file;
        private FileConfiguration fileConfig;
     
        public ConfigHelper(JavaPlugin plugin, String fileName) {
            file = new File(plugin.getDataFolder(), fileName);
            fileConfig = YamlConfiguration.loadConfiguration(file);
        }
     
        public void saveConfig() {
            try {
                fileConfig.save(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
     
        public void load() {
            try {
                fileConfig.load(file);
            } catch (IOException | InvalidConfigurationException e) {
                e.printStackTrace();
            }
        }
     
        public FileConfiguration getConfig() {
            return fileConfig;
        }
    }
    A players home from simpleHomes.yml:
    Code:
    riss227:
      default:
        world: world
        x: 460
        y: 86
        z: 477
    A players home from my Homes.yml:
    Code:
    d85cd525-5d94-4cf2-80b2-6b22b3b72c43:
      default:
        x: 501.8729127071449
        y: 78.2
        z: -134.5402317823001
        world: world_nether
      homeNum: 1
    What i get from the conversion (Notice a world node isn't even there):
    Code:
    d85cd525-5d94-4cf2-80b2-6b22b3b72c43:
      default:
        x: 0.0
        y: 0.2
        z: 0.0
      homeNum: 1
     
    Last edited: Sep 11, 2016
  2. Offline

    Zombie_Striker

    Your issue is that the values are not being read correctly.
    This seems to be the only place where you get all these values. If the world is null and if the ints are either 0 or -1, then the path you provided does not exist. If this is the case, then the path you provided is not the actual path. Use Config#contains(PATH) to see if the path exists. Start from the "name", and work down until you find the path that does not exist/ is wrong.
     
  3. Offline

    AlarmedDino

    I checked if all the paths existed and it magically started working... i didn't change anything else. Sorry for wasting your time.
     
  4. Offline

    Zombie_Striker

    @AlarmedDino
    It's not wasting my time. This is why I'm here.

    Now that your problem has been solved, mark this thread as solved. (Top,left side of this page, click Thread Tools, Edit title, Click the "no prefix" and change it to "solved")
     
Thread Status:
Not open for further replies.

Share This Page