Solved Having Problems with saving Data to a config.

Discussion in 'Plugin Development' started by Boardaic, Jul 13, 2016.

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

    Boardaic

    Im trying to make the command /setspawn <1/2/3...> and save the players coordinates in to the config.
    But i'm kinda having problems with it.
    In the setSpawn Class you'll see the method setSpawn, when i enter /setspawn 1 in chat, the method is executed, but for some reason it returns false, so then the plugin tells me, please enter a number between 1 and 12.

    Here's my code: (pls dont comment my style of code, i'm sure there's a lot more efficient way of dooing it, but i'm a complete noob at coding)

    This is the class of the Command:
    Code:
    public class Setspawn implements CommandExecutor {
     
        private Main plugin;
    
        public Setspawn() {
        this.plugin = plugin;
        }
    
        public boolean setSpawn(String args, String cords) {
        if(args == "1") {
        plugin.getConfig().set("spawn1", cords);
        plugin.saveConfig();
        return true;
        }else if(args == "2") {
        plugin.getConfig().set("spawn2", cords);
        plugin.saveConfig();
        return true;
        }else if(args == "3") {
            plugin.getConfig().set("spawn3", cords);
            plugin.saveConfig(); 
            return true;
            }else if(args == "4") {
                plugin.getConfig().set("spawn4", cords);
                plugin.saveConfig();
                return true;
                }else if(args == "5") {
                    plugin.getConfig().set("spawn5", cords);
                    plugin.saveConfig();
                    return true;
                    }else if(args == "6") {
                        plugin.getConfig().set("spawn6", cords);
                        plugin.saveConfig();
                        return true;
                        }else if(args == "7") {
                            plugin.getConfig().set("spawn7", cords);
                            plugin.saveConfig();
                            return true;
                            }else if(args == "8") {
                                plugin.getConfig().set("spawn8", cords);
                                plugin.saveConfig();
                                return true;
                                }else if(args == "9") {
                                    plugin.getConfig().set("spawn9", cords);
                                    plugin.saveConfig();
                                    return true;
                                    }else if(args == "10") {
                                        plugin.getConfig().set("spawn10", cords);
                                        plugin.saveConfig();
                                        return true;
                                        }else if(args == "11") {
                                            plugin.getConfig().set("spawn11", cords);
                                            plugin.saveConfig();
                                            return true;
                                            }else if(args == "12") {
                                                plugin.getConfig().set("spawn12", cords);
                                                plugin.saveConfig();
                                                return true;
                                                }else{
                                                    return false;
                                                }
     
        }
     
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player s = (Player)sender;
            if (cmd.getName().equalsIgnoreCase("setspawn")) {
                if(args.length == 0) {
                s.sendMessage("This line of code is missing");
                }else if(args.length == 1) {
                Location l = s.getLocation().getBlock().getLocation();
                String cords = l.toString();
                setSpawn(args[0], cords);
                if(setSpawn(args[0], cords) == false) {
                    s.sendMessage(Settings.Prefix + "§cPlease enter a Number between §b1 §cand §b12.");
                }else{
                    s.sendMessage(Settings.Prefix + "§bSpawn §a" + args[1] + "§b was set.");
                }
                return true;
            } else {
                s.sendMessage(Settings.Prefix + "§cYou have entered to many arguments. You need Help with the command? Use: §b/setspawn help");
            }
            }
             
            return true;
        }
    }
    
    
    
    
    Here's my Main:

    Code:
    public class Main extends JavaPlugin{
     
        @Override
        public void onEnable() {
            System.out.println("onEnable succesfully executed");
            getConfig().options().copyDefaults(true);
            saveConfig();
            System.out.println("Config loaded");
            Bukkit.getServer().getPluginCommand("setspawn").setExecutor(new Setspawn());
        }
     
        @Override
        public void onDisable() {
            System.out.println("Plugin is beeing shut down");
        }
     
        public void registerEvents() {
            new de.ainucraft.runners.main.Settings();
            new de.ainucraft.runners.cmds.Setspawn();
        }
     
     
    
    }
    
    Thanks for any Help!
     
    Last edited: Jul 17, 2016
  2. use .equals(...) instead of == ...

    And instead of creating a if for every number, use a for-loop
     
  3. Offline

    ArsenArsen

    First of, you never save it. Second of, Che k if the number is in range and if it is your path in the set is "spawn" + number
     
  4. Are you blind or what :confused:


    @Boardaic
    In your Setspawn class you never initialize 'plugin'
    Code:
    private Main plugin;
    
        public Setspawn() {
        this.plugin = plugin; //you assign the variable above to itself, pretty useless huh?
        }
    How you should do it:

    Add a parameter 'Main plugin' in the constructor of the Setspawn class, then 'this.plugin = plugin' will actually work
    When you set the executor in the main class, add 'this' in the () in 'new SetSpawn(/*here*/)
     
  5. Offline

    ArsenArsen

    Wow I'm sorry I did not see it @FisheyLP
     
  6. Offline

    Boardaic

    Sorry, i don't really understand either of your replys, this is the first proper Plugin im trying to code :'D Thx for the reply though :)
     
  7. @Boardaic Instead of checking for a string numbed multiple, multiple times why not just use it!

    Example
    String s = "Apple"
    System.out.println("I am eating an " + s);

    Now whatever I set s as it will always put it there without me needing to do a bunch of checks
    A number example
    int i = 20;
    setLocation("Home" + i, location)

    You can apply this to yours by just using the string args after the word spawn in your config set, it will change all of that code down to 2 lines!
     
    mine-care likes this.
  8. Offline

    Boardaic

    Well yes i understand that, but i want it to return false, if the number isnt a number between 1 and 12 and that doesnt really solve my problem :/
     
  9. @Boardaic Parse it to an int and do an if statement?
     
  10. i let you my code, it works perfectly! but it's a quite different as you... any questions, ask me.
    Code:
    if (args[0].equalsIgnoreCase("setposarena1")){
                        p.sendMessage(ChatColor.GREEN+"Player "+ChatColor.AQUA+"1 "+ChatColor.GREEN+"starting!");
                        int blockX = p.getLocation().getBlockX();
                        int blockY = p.getLocation().getBlockY();
                        int blockZ = p.getLocation().getBlockZ();
                        int pitch = (int) p.getLocation().getPitch();
                        int yaw = (int) p.getLocation().getYaw();
                        pl.getConfig().set("arena.players.player1.x",blockX);
                        pl.getConfig().set("arena.players.player1.y",blockY);
                        pl.getConfig().set("arena.players.player1.z",blockZ);
                        pl.getConfig().set("arena.players.player1.pitch",pitch);
                        pl.getConfig().set("arena.players.player1.yaw",yaw);
                        pl.saveConfig();
                        Util.broadcast(ChatColor.GREEN+"Player "+ChatColor.AQUA+"1 "+ChatColor.GREEN+"spawn has been set!");
                        return true;
                    }
     
  11. @PistoFrancoPuumm
    Instead of spoon-feeding (giving "example code"), Explain to the OP what he/she needs to do, this way they will learn what to do and won't come back a week later asking the exact same question.
     
  12. Offline

    Boardaic

    thanks for your help, i think i've understood, it worked and i know what the error was, thanks :)
     
Thread Status:
Not open for further replies.

Share This Page