Solved Having trouble saving config in onDisable() so that it works after a reload

Discussion in 'Plugin Development' started by nickdeveloper, Jan 19, 2019.

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

    nickdeveloper

    Hello, I am trying to make a simple /spawn plugin. Here is my code:

    Code:
    package io.github.**********.Spawn;
    
    import java.util.Arrays;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public final class Spawn extends JavaPlugin {
        @Override
        public void onEnable() {
            getLogger().info("Initialized Spawn");
            getConfig();
        }
       
        List<Integer> spawnCoords = getConfig().getIntegerList("spawn");
               
        Location spawn = new Location(Bukkit.getWorld("world"), spawnCoords.get(0), spawnCoords.get(1), spawnCoords.get(2));
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player P = (Player) sender;
           
            if (cmd.getName().equalsIgnoreCase("spawn")) {
                P.sendMessage(ChatColor.GRAY + "" + ChatColor.ITALIC + "Teleporting to spawn");
                P.teleport(spawn);
                return true;
               
            } else if (cmd.getName().equalsIgnoreCase("setspawn")) {
                if (P.isOp()) {
                    List<Integer> playerPosition = Arrays.asList(P.getLocation().getBlockX(), P.getLocation().getBlockY(), P.getLocation().getBlockZ());
                    P.sendMessage(ChatColor.GREEN + "Spawn set at " + playerPosition.get(0) + " " + playerPosition.get(1) + " " + playerPosition.get(2));
                    getConfig().set("spawn", playerPosition);
                    saveConfig();
                    return true;
                   
                } else {
                    P.sendMessage(ChatColor.RED + "You do not have permission to use this command.");
                    return true;
                }
            }
           
            return true;
        }
       
        @Override
        public void onDisable() {
            saveConfig();
            getLogger().info("Deactivated Spawn");
        }
    }
    
    Essentially, /setspawn does not change the location in the config.yml. In addition, there is no config.yml created in the plugins folder. Spawn.jar doesn't even have a plugins folder, it's just the jar alone. The config.yml should maintain changes when there is a reload, but it isn't working. I am stuck.

    I fixed it; I had to move the spawnCoords list and spawn Location inside onCommand() so that it retrieved an updated value.

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

Share This Page