Need help with boolean use (True/False in config)

Discussion in 'Plugin Development' started by DaStrobel, Jan 16, 2020.

Thread Status:
Not open for further replies.
  1. Hey Hey,

    I'm stuck at something for nearly the whole day.

    My plan: Creating a line in my config, that use "true" or "false" to enable and disable a command.

    Problem: To command doesnt work, if i add
    Code:
    if(cfg.getString("test").equals("true")) {
    
    
    
    } else {
    p.sendMessage("§4[BetterLife] §aThis Command is disabled!");
    }
    }
    (first line and last lines of my command)

    This is what i have:

    My Command Class:
    Code:
    package com.DaStrobel.BetterLife.Commands;
    
    import org.bukkit.entity.Player;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.YamlConfiguration;
    import java.io.File;
    
    import org.bukkit.event.Listener;
    import org.bukkit.command.CommandExecutor;
    
    public class Commands implements CommandExecutor, Listener {
      
        File configFile;
        YamlConfiguration cfg;
                  
        public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) {
            configFile = new File("plugins/BetterLife/config.yml");
            YamlConfiguration cfg = YamlConfiguration.loadConfiguration(configFile);
            final Player p = (Player)sender;
          
            if(cfg.getString("test").equals("true")) {
                if (cmd.getName().equalsIgnoreCase("bl_commands")) {
                        p.sendMessage("§4[BetterLife] §e" + "== Command List ==");
                } else {
                    p.sendMessage("§4[BetterLife] §aThis Command is disabled!");
                }
            }
            return true;
        }
    }
    My Main Class:
    Code:
    package com.DaStrobel.BetterLife;
    
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.event.Listener;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import com.DaStrobel.BetterLife.Commands.Commands;
    
    public class Main extends JavaPlugin implements CommandExecutor, Listener
    {          
    
        public void onEnable() {
            super.onEnable();
            Bukkit.getConsoleSender().sendMessage("§aBetterLife loaded.");
                  
            InitConfig();
         
            Bukkit.getPluginManager().registerEvents((Listener)new Commands(), (Plugin)this);
    
            this.getCommand("bl_commands").setExecutor((CommandExecutor)new Commands());
          
            System.out.println("[BetterLife] Succesfully (re)loaded config.yml");
        }
      
        public void onDisable() {
            Bukkit.getConsoleSender().sendMessage("§c[BetterLife] disabled.");
        }
      
        private void InitConfig() {
            this.reloadConfig();
            this.getConfig().addDefault("test", "true");
            this.getConfig().options().copyDefaults(true);
            this.saveConfig();
            }
    }
    My Config:
    Code:
    test: true

    As you can see, it's not in my Main class, it's in a seperated one. (I never done that boolean thing for true/false in a second class)

    Any help is appreciated!
    Best regards, DS
     
    Last edited: Jan 16, 2020
  2. Offline

    Symphonic

    1. It's bad practice to put a Listener and a CommandExecutor in the same class IMO
    2. You don't need to do all that work for the config. In your onEnable put this.saveDefaultConfig(); and then when you need the config instead of doing cfg.whatever do this.getConfig().whatever; You don't need to make a Yaml/FileConfiguration for the default config.
    3. Don't use .equals when comparing strings. Bad practice, use if (string1 == string2)
    4. why are you calling onEnable in onEnable
     
  3. Offline

    Strahan

    I agree with the above, all save:
    You have to use .equals to compare Strings, assuming you want to compare contents.

    That aside, why are you even using String at all OP? Config supports boolean. You can do getConfig().set("status", boolVariable); then when you read it, do boolean status = getConfig().getBoolean("status");

    Also
    • Don't embed the color character, use the ChatColor enum. So for example instead of "§c[BetterLife] disabled." you'd do ChatColor.RED + "[BetterLife] disabled." or ChatColor.translateAlternateColorCodes('&', "&c[BetterLife] disabled.")
    • Don't cast a variable without ensuring the variable is the appropriate type for the cast. If the console ran your command, it'd crash when you try to set the sender to Player
    • I see only one command setting Commands class as executor. If there will be just one command,
      if (cmd.getName().equalsIgnoreCase("bl_commands")) is not required.
    • If a class extends JavaPlugin, it does not need to implement CommandExecutor as it already does
    • For that matter, why are you implementing CommandExecutor and Listener in main when you have neither command handling or listeners present?
    • There is no need to send enable/disable messages; Spigot already does that so all you accomplish is cluttering the console log
    • Don't use println for console messages, use the logger. That's why it exists.
     
  4. Offline

    Symphonic

    TIL, guess i never really compared strings before.
    I noticed most of these things but I was going for the obvious issues.
     
  5. Hey!

    Thank you both for your ideas and tips!
    I fixed most of those thing, You stated above.

    The code above, doesn't show my whole code, for example, my commands class got a few more commands, but i only showed one, since i don't wanted to post my whole code, and also since it's not nessesarry to set up one working command for true/false. (I can just copy the thing to my other commands, once i got one working :) )

    However, I just managed it (finally :3) by myself, to get it work. ( with if(cfg.getBoolean("test")) )

    Thank you guys and have a great day/night!

    DS
     
Thread Status:
Not open for further replies.

Share This Page