Solved saveConfig(); Problems.

Discussion in 'Plugin Development' started by Xydez, Jul 16, 2017 at 1:39 PM.

  1. Offline

    Xydez

    Hey, So I was working on this plugin but when I tried to add config.yml functionality it just completely broke. The thing I'm trying to do is that when someone does

    /cb on

    it turns it on and sets it in the config. Any ideas?

    - onCommand method
    Code:java
    1.  
    2. package xyz.cb;
    3.  
    4. import static org.bukkit.ChatColor.BOLD;
    5. import static org.bukkit.ChatColor.DARK_RED;
    6. import static org.bukkit.ChatColor.GRAY;
    7. import static org.bukkit.ChatColor.GREEN;
    8. import static org.bukkit.ChatColor.RED;
    9. import static org.bukkit.ChatColor.RESET;
    10.  
    11. import java.util.ArrayList;
    12. import java.util.HashMap;
    13.  
    14. import org.bukkit.Bukkit;
    15. import org.bukkit.ChatColor;
    16. import org.bukkit.command.Command;
    17. import org.bukkit.command.CommandSender;
    18. import org.bukkit.entity.Player;
    19. import org.bukkit.event.EventHandler;
    20. import org.bukkit.event.Listener;
    21. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    22. import org.bukkit.plugin.java.JavaPlugin;
    23.  
    24.  
    25. public class Main extends JavaPlugin implements Listener {
    26.  
    27. public boolean DisableCommands = false;
    28. public void onEnable () {
    29. Bukkit.getPluginManager().registerEvents(this, this);
    30. //getConfig().options().copyDefaults(true);
    31. saveDefaultConfig();
    32. saveConfig();
    33.  
    34. System.out.println("Config Recap:");
    35. System.out.println("- DisableCommands: " + getConfig().getBoolean("DisableCommands"));
    36. System.out.println("- warnings: " + getConfig().getInt("warnings"));
    37. System.out.println("- kickmsg: '" + getConfig().getString("kickmsg") + "'");
    38. System.out.println("- warnmsg: '" + getConfig().getString("warnmsg") + "'");
    39.  
    40. }
    41.  
    42.  
    43. //@SuppressWarnings("unused")
    44. public boolean onCommand (CommandSender sender, Command cmd, String label, String[] args) {
    45. if(cmd.getName().equalsIgnoreCase("cb") || cmd.getName().equalsIgnoreCase("commandblocker")) {
    46. if(args.length == 1) {
    47. if(args[0].equalsIgnoreCase("off")) {
    48. DisableCommands = false;
    49. getConfig().set("DisableCommands", false);
    50. saveConfig();
    51. reloadConfig();
    52. if(DisableCommands == false && getConfig().getBoolean("DisableCommands", DisableCommands)) {
    53. sender.sendMessage(GREEN + "" + BOLD + "Success! " + RESET + GRAY + "Disabled Command filter");
    54. return true;
    55. } else {
    56. sender.sendMessage(RED + "" + BOLD + "Error: " + RESET + GRAY + "Couldn't disable command-filter...");
    57. return true;
    58. }
    59. }
    60. if(args[0].equalsIgnoreCase("on")) {
    61. DisableCommands = true;
    62. getConfig().set("DisableCommands", true);
    63. saveConfig();
    64. reloadConfig();
    65. if(DisableCommands == true && getConfig().getBoolean("DisableCommands", DisableCommands)) {
    66. sender.sendMessage(GREEN + "" + BOLD + "Success! " + RESET + GRAY + "Enabled Command filter");
    67. return true;
    68. } else {
    69. sender.sendMessage(RED + "" + BOLD + "Error: " + RESET + GRAY + "Couldn't enable command-filter...");
    70. return true;
    71. }
    72. }
    73. if(args[0].equalsIgnoreCase("reload")) {
    74. reloadConfig();
    75. sender.sendMessage(GREEN + "Reloaded config.yml");
    76. }
    77. } else {
    78. sender.sendMessage(DARK_RED + "" + BOLD + "Invalid syntax: " + RESET + GRAY + "/cb <on/off/reload>");
    79. }
    80.  
    81.  
    82. return true;
    83. }
    84. return true;
    85. }
    86.  
    87. public HashMap<Player, Integer> warned = new HashMap<Player, Integer>();
    88.  
    89. @EventHandler
    90. public void onCommand(PlayerCommandPreprocessEvent e){
    91. Player p = e.getPlayer();
    92. if (DisableCommands == true && p instanceof Player) {
    93. if(!e.getMessage().toLowerCase().startsWith("/cb") && !e.getMessage().toLowerCase().startsWith("/commandblocker")) {
    94. e.setCancelled(true);
    95. // p.sendMessage(RED + "" + BOLD + "Hey! " + RESET + GRAY + "Commands are disabled on this server");
    96. p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("warnmsg")));
    97. if(warned.get(p) == null) warned.put(p, 0);
    98.  
    99. ArrayList<Player> cooldown = new ArrayList<Player>();
    100. cooldown.add(p);
    101. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    102. public void run() {
    103. cooldown.remove(p);
    104. }
    105.  
    106. }, 300);
    107.  
    108.  
    109. if(getConfig().getInt("warnings") != -1) {
    110. int i = warned.get(p);
    111. i++;
    112. warned.put(p, i);
    113. if(warned.get(p) > getConfig().getInt("warnings")) {
    114. p.kickPlayer(ChatColor.translateAlternateColorCodes('&', getConfig().getString("kickmsg")));
    115. }
    116. }
    117. }
    118. }
    119. }
    120. }
    121.  
     
    Last edited: Jul 17, 2017 at 2:29 AM
  2. @Xydez What broke? Is there an error message?

    EDIT: You don't have to call reload config after you saveConfig();
     
  3. Offline

    Xydez

    The error message I made myself just says "couldn't enable command-filter", Which basically means it's not true in the config.

    I looked at the config, and there I see a DisableCommands: false which should be set to true.

    EDIT: ok thx, but the main problem still remains
     
  4. @Xydez in both of your if statements, you are only using one "=" sign. For if statements, you must use "==".
     
  5. Offline

    Xydez

    Yeah it says DisableCommand = true; in the code

    PS: Why are you "Recommended/Required" to use the edit button, Isn't it just easier to read from top-to-bottom. Is it some kind of storage thing or is it so you don't have to scroll through a giant discussion?
     
    Last edited: Jul 16, 2017 at 2:02 PM
  6. @Xydez Use the edit button next time:)

    Anyway, is your config file being updated when you run the command? Like is "DisableCommands" being set to true when you run /cb on?
     
  7. Offline

    Xydez

    Yes
    Code:java
    1. DisableCommands = true;
    2. getConfig().set("DisableCommands", true);
    3. saveConfig();
     
  8. @Xydez Sorry I meant in the config, is the config changing?
     
  9. Offline

    Xydez

  10. @Xydez Ok, I think I know the problem but I didnt see it because they look almost the same. Some times you use "config" when you reference the configuration in the code, and sometimes you use getConfig(); Always use getConfig();
     
  11. Offline

    Xydez

    Last edited: Jul 17, 2017 at 2:22 AM
  12. @Xydez Is all your code in one class? If so, can you post the entire thing? not just the onCommand() method?
     
  13. Offline

    Xydez

    The code is in one class. Yeah, kind of a beginner mistake but it's easier. Updated code.
     
  14. @Xydez
    Use the getConfig().options().copyDefaults(true).
    And using saveDefaultConfig(); will not do anything if there are no defaults. just use saveConfig();
     
  15. Offline

    Xydez

    I have been looking in the wrong config folder. Sh*t.

    Well, it does change in the right config folder. But it still won't disable the command-filter
     
  16. @Xydez
    Wow. *facepalm* So now the config changes when you run /cb on or /cb off?
     
  17. Offline

    Xydez

    Yeah, But it still won't see that DisableCommand: true is true in the config.

    It's like really blind or something (This is only for /cb off.)
     
  18. @Xydez remove the second argument from the getBoolean(path) method. And you don't need the reloadConfig() methods before saving. Also, try printing out the getBoolean("DisableCommands") just to see if its null or not
     
  19. Offline

    Xydez

    No need to debug anymore. The code Finally works!

    All thanks to the (getConfig().getBoolean("DisableCommands") == false)
     
  20. @Xydez
    Darn, I'm kinda mad I didn't see that. If you compare two things with "==" and you have "&&" or "||" you need parentheses.
     
  21. Offline

    Xydez

Share This Page