Value in the config cannot be overwritten.

Discussion in 'Plugin Development' started by Waffelle, May 27, 2021.

  1. Offline

    Waffelle

    Hello, I would like to be able to change a value in the configuration ingame. The creation of the configuration and the readout are already working.
    But the value cannot be changed.

    The Main code:
    Code:
    public final class TestPlugin extends JavaPlugin {
    
        public static TestPlugin INSTANS;
    
        public void log(String text) {
            Bukkit.getConsoleSender().sendMessage(text);
        }
    
        public FileConfiguration config = getConfig();
    
        @Override
        public void onEnable() {
            log("ver 1.0");
            INSTANS = this;
            this.registCommands();
            registConfig();
        }
    
        private void registCommands() {
            Bukkit.getPluginCommand("Testcommand").setExecutor(new TestCommand());
        }
    
        public void registConfig() {
            config.addDefault("Spawn.X", 0);
            config.addDefault("Spawn.Y", 0);
            config.addDefault("Spawn.Z", 0);
    
            config.options().copyDefaults(true);
            saveConfig();
        }
    }
    and the command:

    Code:
    public class TestCommand implements CommandExecutor {
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    
            Player player = (Player) sender;
            Location Position = player.getLocation();
            int x = Position.getBlockX();
            int y = Position.getBlockY();
            int z = Position.getBlockZ();
            player.sendMessage("§ePosition" + " §bX: §f" + x + " §bY: §f" + y + " §bZ: §f" + z);
    
            TestPlugin.INSTANS.config.set("Spawn.X", x);
            TestPlugin.INSTANS.config.set("Spawn.Y", y);
            TestPlugin.INSTANS.config.set("Spawn.Z", z);
    
    
            return false;
        }
    }
     
  2. Offline

    davidclue

    @Waffelle After you modify the config you have to use
    Code:
    plugin.saveConfig();
     
  3. Offline

    Strahan

    Also Location implements ConfigurationSerializable, so don't waste your time manually serializing. Your manual serialization is losing positional information as well since you are only saving block X/Y/Z.

    Here is an example of managing spawn's Location directly in the config, assuming one is using an old MC server that is before getLocation() existed. Figured I'd demo that since it's slightly more complicated than just using the Location specific get:
    Code:
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
      if (!(sender instanceof Player)) {
        sender.sendMessage("Console cannot teleport, silly!");
        return true;
      }
    
      Player p = (Player)sender;
      switch (cmd.getName().toLowerCase()) {
      case "setspawn":
        getConfig().set("spawn", p.getLocation());
        saveConfig();
        p.sendMessage("Spawn set!");
        break;
    
      case "spawn":
        Object spawn;
        if ((spawn = getConfig().get("spawn")) instanceof Location) {
          p.teleport((Location)spawn);
          return true;
        }
        p.sendMessage("I can't find spawn, so you're stuck.");
        break;
      }
      return true;
    }
     

Share This Page