title: How to use getConfig() to edit config.yml in a method outside of the Main class
body: <p>I've had a persistent issue for the path couple days that no amount of searching has helped me overcome. I only recently began dabbling into Bukkit plugin development, and all this simple plugin current does is set join/quit messages and AFK status. However, the latter part is giving me issues.</p>
  
  <p>My Spigot server console is outputting <strong>Could not pass event PlayerJoinEvent to ServerBasics</strong> when a player joins and <strong>Unhandled exception executing command 'afk' in plugin ServerBasics</strong> when /afk is typed by a player.</p>
  
  <p>The logic: Main class's onEnable() sets command executor for AfkCommand and event listener for JoinListener, which has the main focus of monitor when a player joins. At this point, it's supposed to call the toggle() method in ToggleAFK when a player joins, passing the player's name and a false boolean to add an entry for them in the config.yml and make sure they and not afk, should they type the /afk command later. ToggleAFK should then save the config with afk.[player] equaling false.</p>
  
  <p>At the same time, AfkCommand should also call ToggleAFK with the player's name and true/false parameter, and ToggleAFK should handle it in the same way. However, it's failing to do so.</p>
  
  <p>I've set broadcast messages all through the code to see when it's misfiring. When a player joins the server, JoinListener broadcasts <em>init</em> and <em>joined</em> but not <em>toggled</em>, and AfkCommand broadcasts <em>a</em>, <em>b</em>, and <em>c</em> but not <em>d</em>, so we know the problem is in ToggleAFK. ToggleAFK does not broadcast <em>instanced</em> or <em>g</em>.</p>
  
  <p>The problem seems to be with me creating an instance of the Main class, but I don't know how else to enable the getConfig() commands in another class, and it wouldn't be at all neat if all my config.yml edits were handled in Main. I've read that the Main class shouldn't be instanced again, but I also found a tutorial in which it was instanced in the way I've done it in ToggleAFK, yet mine doesn't work (although it seems just fine when I do that in AfkCommand).</p>
  
  <p>What are my options? Is there a different way to edit the config file from other classes? Am I forced to edit them in Main? Is the issue because ToggleAFK is called two classes after Main instead of just one? Any help is appreciated.</p>
  
  <p>My Main class:</p>
  
  <pre><code>package com.timothyreavis.serverbasics;
  
  import org.bukkit.plugin.java.JavaPlugin;
  
  public class Main extends JavaPlugin {
  
      @Override
      public void onEnable()
      {
          getCommand("afk").setExecutor(new AfkCommand(this));
  
          getServer().getPluginManager().registerEvents(new JoinListener(null), this);
      }
  
  }
  </code></pre>
  
  <p>My JoinListener class (Sets afk to false when player joins. I'll soft code the join messages and stuff later):</p>
  
  <pre><code>package com.timothyreavis.serverbasics;
  
  import org.bukkit.Bukkit;
  import org.bukkit.entity.Player;
  import org.bukkit.event.EventHandler;
  import org.bukkit.event.Listener;
  import org.bukkit.event.player.PlayerJoinEvent;
  import org.bukkit.event.player.PlayerQuitEvent;
  
  import net.md_5.bungee.api.ChatColor;
  
  public class JoinListener implements Listener
  {
      // create ToggleAFK class instance
      ToggleAFK afk;
      public JoinListener(ToggleAFK instance) {
          afk = instance;
      }
  
      @EventHandler
      public void onPlayerJoin(PlayerJoinEvent event)
      {
          Bukkit.broadcastMessage("init");
          // Player joins
          Player player = event.getPlayer();
          Bukkit.broadcastMessage("joined");
          afk.toggle(player, false);
          Bukkit.broadcastMessage("toggled");
          // set player entry as not afk in config.yml
          Bukkit.broadcastMessage("h");
  
          if (!player.hasPlayedBefore()) {
              // if player is new
              event.setJoinMessage(ChatColor.DARK_PURPLE + "Welcome " + ChatColor.GREEN + event.getPlayer().getName() + ChatColor.DARK_PURPLE + " to the server!");
  
              // tell player to read rules
              event.getPlayer().sendMessage(ChatColor.YELLOW + "Hi, " + player.getName() + "! " + Bukkit.getServerName() + " is a rather unique server with custom plugins, so please read our rules and commands before getting started.");
          } else {
              // if player isn't new
              event.setJoinMessage(ChatColor.GREEN + "[+] " + event.getPlayer().getName());
          }
      }
      @EventHandler
      public void onPlayerQuit(PlayerQuitEvent event)
      {
          // Player quits
          event.setQuitMessage(ChatColor.RED + "[-] " + event.getPlayer().getName());
      }
  }
  </code></pre>
  
  <p>My ToggleAFK class (this should edit the config.yml file when called):</p>
  
  <pre><code>package com.timothyreavis.serverbasics;
  
  import org.bukkit.Bukkit;
  import org.bukkit.entity.Player;
  
  import net.md_5.bungee.api.ChatColor;
  
  public class ToggleAFK {
      // create Main class instance
      Main plugin;
      public ToggleAFK(Main instance) {
          plugin = instance;
          Bukkit.broadcastMessage("instanced");
      }
  
      public Boolean toggle(Player player, Boolean status) {
          Bukkit.broadcastMessage("g");
          plugin.getConfig().set("afk." + player, status);
          plugin.saveConfig();
          if (status) {
              Bukkit.broadcastMessage(ChatColor.DARK_PURPLE + player.getName() + " is now AFK.");
          } else {
              Bukkit.broadcastMessage(ChatColor.DARK_PURPLE + player.getName() + " is no longer AFK.");
          }
          return true;
      }
  }
  </code></pre>
  
  <p>And my AfkCommand class (handles /afk and calls ToggleAFK):</p>
  
  <pre><code>package com.timothyreavis.serverbasics;
  
  import org.bukkit.Bukkit;
  import org.bukkit.command.Command;
  import org.bukkit.command.CommandExecutor;
  import org.bukkit.command.CommandSender;
  import org.bukkit.entity.Player;
  //import org.bukkit.event.player.PlayerMoveEvent;
  
  public class AfkCommand implements CommandExecutor {
      // create Main class instance
      Main plugin;
      public AfkCommand (Main instance) {
          plugin = instance;
      }
      // create ToggleAFK class instance
      ToggleAFK afk;
      public AfkCommand (ToggleAFK instance) {
          afk = instance;
      }
  
      @Override
      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
          Bukkit.broadcastMessage("a");
          if (sender instanceof Player) {
              Player player = (Player) sender;
              Bukkit.broadcastMessage("b");
              if (plugin.getConfig().getBoolean("afk."+sender) == false) {
                  Bukkit.broadcastMessage("c");
                  afk.toggle(player, true);
                  Bukkit.broadcastMessage("d");
              } else {
                  afk.toggle(player, false);
                  Bukkit.broadcastMessage("d");
              }
          } else {
              Bukkit.broadcastMessage("e");
              return false;
          }
          // if command is used correctly, return true
          Bukkit.broadcastMessage("f");
          return true;
      }
  
  }
  </code></pre>