Best way to access this?

Discussion in 'Spigot Plugin Development' started by Kyllian, Apr 26, 2017.

  1. Hey, I want to make a command wich adds a pitch for every home a player has:

    Code (Text):
    Set<String> players = fc.getData().getKeys(false);
                    for (String str : players) {
                        for (String homes : fc.getData().getStringList(str + ".homes.")) {
                            fc.getData().set(homes + ".Pitch", new Float(1));
                        }
                    }
    I'm quite bad at accessing files. But this is my file:

    Code (Text):
    3cfa3047-782d-3cdd-9ee9-74fc60d93cd8:
      fly: false
      god: false
      homes:
        oke:
          world: world
          X: -42.38082052154324
          Y: 69.0
          Z: 249.89942158957027
          Yaw: -22.29183
        hallo!:
          world: world
          X: -42.38082052154324
          Y: 69.0
          Z: 249.89942158957027
          Yaw: -22.29183
      vanish: false
      tptoggle: false
      frozen: false
      msgToggle: false
      banned: false
      cmdSpy: false
    So I want to set the pitch to 1 for every home, Mine should be working I just dont really understand whats going wrong
     
  2. I saw this:
    loc.setPitch(90F);
    loc.setYaw(90F);
    I don't know if this can help :)
     
  3. You need to save the file after editing. FileConfiguration#save(File).
     
  4. Yes I do that, But it doesnt seem to access the homes
     
  5. I canĀ“t see anything of this in the Code. Please post your full code.
     
  6. Try removing the second period in ".homes." because now you're referring to a different path than you're intending to. Correct me if I'm wrong.
     
  7. Choco

    Moderator

    This doesn't make any sense whatsoever.
    1. Your "homes" in the configuration file is not a StringList, but rather a ConfigurationSection. Call #getConfigurationSection() and get its keys
    2. The period at the end of "homes" is unnecessary and likely breaking the code as well (EDIT: Sniped on this point)

    3.
    wat... why? Ever hear of primitive types? "1f"
     
  8. Full code:

    Code (Text):
    package me.kyllian.system32.commands;

    import java.util.Set;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;

    import me.kyllian.system32.utils.FileCreator;
    import me.kyllian.system32.utils.Messages;

    public class RepairPlugin implements CommandExecutor {
       
        private Messages mes = new Messages();
        private FileCreator fc = FileCreator.getInstance();

        public boolean onCommand(CommandSender s, Command cmd, String commandLabel, String[] args) {
            if (cmd.getName().equalsIgnoreCase("repairplugin")) {
                if (args.length == 0) {
                    if (!(s.hasPermission("system32.repairplugin"))) {
                        mes.noPermissions(s);
                        return true;
                    }
                    Set<String> warps = fc.getWarpData().getKeys(false);
                    for (String str : warps) {
                        fc.getWarpData().set(str + ".Pitch", new Float(1));
                    }
                    Set<String> players = fc.getData().getKeys(false);
                    for (String str : players) {
                        for (String homes : fc.getData().getConfigurationSection(str + ".homes").getKeys(true)) {
                            fc.getData().set(homes + ".Pitch", new Float(1));
                        }
                    }
                    Set<String> jails = fc.getJailData().getKeys(false);
                    for (String str : jails) {
                        fc.getJailData().set(str + ".Pitch", new Float(1));
                    }
                    fc.getSpawnData().set("Pitch", new Float(1));
                    fc.saveData();
                    mes.pluginRepaired(s);
                } else {
                    mes.tooManyArguments(s);
                    return true;
                }
            }
            return true;
        }
    }
     
    Will fix the new Float thing.

    Added configuationsection check. Now I get an NPE in that line

    Code (Text):
                        for (String homes : fc.getData().getConfigurationSection(str + ".homes").getKeys(true)) {