Nothing happens when i write to config

Discussion in 'Plugin Development' started by XxZHALO13Xx, Jan 14, 2015.

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

    XxZHALO13Xx

    When i do the command i don't get an error but also nothing happens to the config..

    Command
    Code:
    private MessagesConfig messagesConfig;
    @Override
        public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
    
            if(sender instanceof Player){
                Player p = (Player) sender;
    
                if(args.length == 0){
                    p.sendMessage(Core.CORE + ChatColor.GREEN + "/mconfig set <Message>");
                }
                else{
                    String cmd = args[0];
    
                    if(cmd.equalsIgnoreCase("set")){
                        if(args.length >= 2){
                            StringBuilder str = new StringBuilder();
                            for (int i = 1; i < args.length; i++){
                                str.append(args[i] + " ");
                            }
    
                            String message = str.toString();
                            if(messagesConfig != null){
                            if(messagesConfig.mdata != null) {
                                messagesConfig.mdata.set("Messages.", message);
                                messagesConfig.saveMessageData();
                            }
                            }
                        }
                        else{
                            p.sendMessage(Core.CORE + ChatColor.RED + "Please specify correct args!");
                        }
                    }
                }
            }
    
            return true;
        }
    }
    
    MessagesConfig

    Code:
    public class MessagesConfig {
    
            private Core plugin;
            public final File messageData;
            public final YamlConfiguration mdata;
            public MessagesConfig(Core plugin) {
                this.plugin = plugin;
                messageData = new File(plugin.getDataFolder() + "/Data/messageData.yml");
                mdata = YamlConfiguration.loadConfiguration(messageData);
            }
    
    
    
    
            public void saveMessageData() {
                try {
                    mdata.save(messageData);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
    
            public void createMessageData(){
                if(!messageData.exists()){
                    try {
                        messageData.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
     
  2. Offline

    mythbusterma

    @XxZHALO13Xx

    So again, why are those variables public? Use proper Object Oriented methodology.

    Also, try debugging. I bet half of those are null.
     
    mine-care likes this.
  3. Offline

    SuperOriginal

    *hint* This is the same reason you were getting the NPE in your other thread, and your null check catches the error so nothing happens.

    You should not have null checks that solely exist to catch YOUR mistakes.
     
    mythbusterma likes this.
Thread Status:
Not open for further replies.

Share This Page