Solved Unexpected Exception

Discussion in 'Plugin Help/Development/Requests' started by Markyroson, Jun 3, 2015.

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

    Markyroson

    I am not sure what I am doing wrong here, but I always get this exception when doing the command /staff add admin <player>.

    Code:
      @SuppressWarnings("deprecation")
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
      {
          Player p = (Player) sender;
        if (commandLabel.equalsIgnoreCase("staff")) {
          if (args.length == 0) {
            if (sender.hasPermission("StaffLists.staff.view.base")) {
                sender.sendMessage("§6§l=====< §2§lStaffLists by Markyroson §6§l>=====");
                sender.sendMessage(ChatColor.GOLD + "/staff members");
                sender.sendMessage(ChatColor.GOLD + "/staff reload " + ChatColor.AQUA + "(for admins/owners only)");
                sender.sendMessage(ChatColor.GOLD + "/staff help");
            } else {
              sender.sendMessage(ChatColor.RED + "You don't have permissions to do this!");
              return true;
            }
    
            return false;
          }
    //PROBLEM AREA IS HERE ---------------------------------------------------------------------------------------
          if(args.length == 2) {
          if(args[0].equalsIgnoreCase("add")) {
              if(args[1].equalsIgnoreCase("admin")) {
                  Player target = Bukkit.getServer().getPlayer(args[2]);
                  if (target == null) {
                      p.sendMessage("That player isn't online");
                  } else {
                      List<String> list = plugin.getConfig().getStringList("Admin");
                      list.add(name);
                      plugin.getConfig().set("Admin", list);
                      plugin.saveConfig();
              }
          }
              else if(args[1].equalsIgnoreCase("mod") || args[1].equalsIgnoreCase("moderator")) {
                  Player target = Bukkit.getServer().getPlayer(args[2]);
                  if (target == null) {
                      p.sendMessage("That player isn't online");
                  } else {
                      List<String> list = plugin.getConfig().getStringList("Mod");
                      list.add(name);
                      plugin.getConfig().set("Mod", list);
                      plugin.saveConfig();
              }
              }
      }
          } //PROBLEM AREA ENDS HERE ---------------------------------------------------------------------------
          if (args.length == 1) {
            if (args[0].equalsIgnoreCase("help")) {
              if (sender.hasPermission(Permissions.VIEW_HELP.add())) {    //if player has permission do this
                if (args[0].equalsIgnoreCase("help")) {
                  sender.sendMessage(ChatColor.WHITE + "[StaffLists version " + ChatColor.AQUA + getDescription().getVersion() + ChatColor.WHITE + "] " + ChatColor.DARK_AQUA + "This is the help menu of Staff Lists, made by Markyroson");
                  sender.sendMessage(ChatColor.GOLD + "/staff " + ChatColor.AQUA + "shows you the full list of plugin commands");
                  sender.sendMessage(ChatColor.GOLD + "/staff help " + ChatColor.AQUA + "shows you this menu");
                  sender.sendMessage(ChatColor.GOLD + "/staff members " + ChatColor.AQUA + "shows you the staff of the server");
                  sender.sendMessage(ChatColor.GOLD + "/staff reload " + ChatColor.AQUA + "reloads the staff config");
                  return true;
                }
              } else {
                sender.sendMessage(ChatColor.RED + "You dont have permission to do this!");    //if they don't have permission send this message
                return true;
              }
            } else if (args[0].equalsIgnoreCase("reload")) {
              if (sender.hasPermission(Permissions.RELOAD.add())) {
                reloadConfig();    //reload config file
                sender.sendMessage(ChatColor.LIGHT_PURPLE + "The staff lists config is reloaded!");    //send player completion message
                return true;
              }
            } else if (args[0].equalsIgnoreCase("members")) {
                if (sender.hasPermission(Permissions.VIEW_MEMBERS.add())) {
                    if (args[0].equalsIgnoreCase("members")) {
                        sender.sendMessage(ChatColor.GOLD + "Owner(s): " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Owner")));
                          sender.sendMessage(ChatColor.AQUA + "Admin(s): " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Admin")));
                          sender.sendMessage(ChatColor.GREEN + "Moderator(s): " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Mod")));
                          sender.sendMessage(ChatColor.RED + "On the server: " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Server")));
                        } else {
                          sender.sendMessage(ChatColor.RED + "You don't have permissions to do this!");    //if this is called then sender does not have permission to view this and therefore send them message saying so
                          return true;
                    }
                }
    
            } else {
                sender.sendMessage(ChatColor.RED + "This command doesn't exist! Use /staff help for info!");    //to fall here the command must not have existed, therefore send this message telling the sender so
              return true;
            }
          }
       }
        return false;
          }
    The error/exception I get if done in console:
    Code:
    [17:32:21] [Server thread/WARN]: Unexpected exception while parsing console command "staff add admin Markyroson"
    org.bukkit.command.CommandException: Unhandled exception executing command 'staff' in plugin Staff-Lists v0.3
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[minecraft_server.jar:git-Bukkit-30d751b]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[minecraft_server.jar:git-Bukkit-30d751b]
        at org.bukkit.craftbukkit.v1_8_R2.CraftServer.dispatchCommand(CraftServer.java:625) ~[minecraft_server.jar:git-Bukkit-30d751b]
        at org.bukkit.craftbukkit.v1_8_R2.CraftServer.dispatchServerCommand(CraftServer.java:611) [minecraft_server.jar:git-Bukkit-30d751b]
        at net.minecraft.server.v1_8_R2.DedicatedServer.aN(DedicatedServer.java:372) [minecraft_server.jar:git-Bukkit-30d751b]
        at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:337) [minecraft_server.jar:git-Bukkit-30d751b]
        at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:626) [minecraft_server.jar:git-Bukkit-30d751b]
        at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:534) [minecraft_server.jar:git-Bukkit-30d751b]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R2.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
        at me.Markyroson.StaffLists.Main.onCommand(Main.java:62) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[minecraft_server.jar:git-Bukkit-30d751b]
        ... 8 more
    If done by player then nothing happens.

    Any ideas?
     
  2. Offline

    justin_393

    The exception is because you didn't check if the sender was a player. You just cast it to player. It should be if (sender instanceof Player) {
    Player player = (Player) sender;
    }

    If you want to make it work for console you need ConsoleSender instead of Player.
    Gimme a min to read rest of code

    (Where did you define the name variable? I can't seem to find it..) EDIT: Wow, I cant read code on a phone...
    Also, you'll get a NPE if the config doesn't contain any Strings.

    EDIT: What Method is deprecated? It's usually deprecated for a reason. Also, it's good practice to put @Override above a Method that's extended from another class.
     
    Last edited: Jun 3, 2015
    Markyroson likes this.
  3. Offline

    Markyroson

    getPlayer is deprecated. The config does contain strings.

    Now that I made it check if it is an instance of a player literally nothing happens when I enter the command, I checked the config and nothing was added, even reloaded and even turned off the server to no avail.

    My config code:

    Code:
    Owner: Place the Owners of the server here!
    Admin: Place the Admins of the server here!
    Mod: Place the Mods of the server here!
    Server: Place the name of your server here
    Updated main code:
    Code:
    @SuppressWarnings("deprecation")
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
      {
          if (sender instanceof Player) {
          Player p = (Player) sender;
        if (commandLabel.equalsIgnoreCase("staff")) {
          if (args.length == 0) {
            if (sender.hasPermission("StaffLists.staff.view.base")) {
                sender.sendMessage("§6§l=====< §2§lStaffLists by Markyroson §6§l>=====");
                sender.sendMessage(ChatColor.GOLD + "/staff members");
                sender.sendMessage(ChatColor.GOLD + "/staff reload " + ChatColor.AQUA + "(for admins/owners only)");
                sender.sendMessage(ChatColor.GOLD + "/staff help");
            } else {
              sender.sendMessage(ChatColor.RED + "You don't have permissions to do this!");
              return true;
            }
    
            return false;
          }
    //BEGINNING OF PROBLEM CODE -------------------------------------------------------------
          if(args.length == 2) {
          if(args[0].equalsIgnoreCase("add")) {
              if(args[1].equalsIgnoreCase("admin")) {
                  Player target = Bukkit.getServer().getPlayer(args[2]);
                  if (target == null) {
                      p.sendMessage("That player isn't online");
                  } else {
                      List<String> list = plugin.getConfig().getStringList("Admin");
                      list.add(name);
                      plugin.getConfig().set("Admin", list);
                      plugin.saveConfig();
              }
          }
              else if(args[1].equalsIgnoreCase("mod") || args[1].equalsIgnoreCase("moderator")) {
                  Player target = Bukkit.getServer().getPlayer(args[2]);
                  if (target == null) {
                      p.sendMessage("That player isn't online");
                  } else {
                      List<String> list = plugin.getConfig().getStringList("Mod");
                      list.add(name);
                      plugin.getConfig().set("Mod", list);
                      plugin.saveConfig();
              }
              }
      }
          }
    //END OF PROBLEM CODE ----------------------------------------------------------------------------
          if (args.length == 1) {
            if (args[0].equalsIgnoreCase("help")) {
              if (sender.hasPermission(Permissions.VIEW_HELP.add())) {    //if player has permission do this
                if (args[0].equalsIgnoreCase("help")) {
                  sender.sendMessage(ChatColor.WHITE + "[StaffLists version " + ChatColor.AQUA + getDescription().getVersion() + ChatColor.WHITE + "] " + ChatColor.DARK_AQUA + "This is the help menu of Staff Lists, made by Markyroson");
                  sender.sendMessage(ChatColor.GOLD + "/staff " + ChatColor.AQUA + "shows you the full list of plugin commands");
                  sender.sendMessage(ChatColor.GOLD + "/staff help " + ChatColor.AQUA + "shows you this menu");
                  sender.sendMessage(ChatColor.GOLD + "/staff members " + ChatColor.AQUA + "shows you the staff of the server");
                  sender.sendMessage(ChatColor.GOLD + "/staff reload " + ChatColor.AQUA + "reloads the staff config");
                  return true;
                }
              } else {
                sender.sendMessage(ChatColor.RED + "You dont have permission to do this!");    //if they don't have permission send this message
                return true;
              }
            } else if (args[0].equalsIgnoreCase("reload")) {
              if (sender.hasPermission(Permissions.RELOAD.add())) {
                reloadConfig();    //reload config file
                sender.sendMessage(ChatColor.LIGHT_PURPLE + "The staff lists config is reloaded!");    //send player completion message
                return true;
              }
            } else if (args[0].equalsIgnoreCase("members")) {
                if (sender.hasPermission(Permissions.VIEW_MEMBERS.add())) {
                    if (args[0].equalsIgnoreCase("members")) {
                        sender.sendMessage(ChatColor.GOLD + "Owner(s): " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Owner")));
                          sender.sendMessage(ChatColor.AQUA + "Admin(s): " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Admin")));
                          sender.sendMessage(ChatColor.GREEN + "Moderator(s): " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Mod")));
                          sender.sendMessage(ChatColor.RED + "On the server: " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Server")));
                        } else {
                          sender.sendMessage(ChatColor.RED + "You don't have permissions to do this!");    //if this is called then sender does not have permission to view this and therefore send them message saying so
                          return true;
                    }
                }
    
            } else {
                sender.sendMessage(ChatColor.RED + "This command doesn't exist! Use /staff help for info!");    //to fall here the command must not have existed, therefore send this message telling the sender so
              return true;
            }
          }
        return false;
          }
     
  4. Offline

    caderape

    @Markyroson
    if(args.length==2){
    you have 3 arguments. add admin and player. The command will never be called.

    And if you do it withouth the player name, you will get an error here :
    Player target = Bukkit.getServer().getPlayer(args[2]);

    For you information, args[0] is args.lengh == 1
     
  5. Offline

    Markyroson

    I removed it by now get:
    Code:
    [20:41:08] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'staff' in plugin Staff-Lists v0.3
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[minecraft_server.jar:git-Bukkit-30d751b]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[minecraft_server.jar:git-Bukkit-30d751b]
        at org.bukkit.craftbukkit.v1_8_R2.CraftServer.dispatchCommand(CraftServer.java:625) ~[minecraft_server.jar:git-Bukkit-30d751b]
        at net.minecraft.server.v1_8_R2.PlayerConnection.handleCommand(PlayerConnection.java:1077) [minecraft_server.jar:git-Bukkit-30d751b]
        at net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java:937) [minecraft_server.jar:git-Bukkit-30d751b]
        at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(SourceFile:37) [minecraft_server.jar:git-Bukkit-30d751b]
        at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(SourceFile:9) [minecraft_server.jar:git-Bukkit-30d751b]
        at net.minecraft.server.v1_8_R2.PlayerConnectionUtils$1.run(SourceFile:13) [minecraft_server.jar:git-Bukkit-30d751b]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_45]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_45]
        at net.minecraft.server.v1_8_R2.SystemUtils.a(SourceFile:60) [minecraft_server.jar:git-Bukkit-30d751b]
        at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:670) [minecraft_server.jar:git-Bukkit-30d751b]
        at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:336) [minecraft_server.jar:git-Bukkit-30d751b]
        at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:626) [minecraft_server.jar:git-Bukkit-30d751b]
        at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:534) [minecraft_server.jar:git-Bukkit-30d751b]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    Caused by: java.lang.NullPointerException
        at me.Markyroson.StaffLists.Main.onCommand(Main.java:89) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[minecraft_server.jar:git-Bukkit-30d751b]
        ... 15 more
     
  6. Offline

    caderape

    @Markyroson Removed what ? you just have to put if args.lengh == 3

    1. Caused by: java.lang.NullPointerException
    2. at me.Markyroson.StaffLists.Main.onCommand(Main.java:89) ~[?:?]
    I dun know where is your line 89

    Edit: List<String> list = plugin.getConfig().getStringList("Mod");
    Are you sure there's an existing list in the config file ?
     
  7. Offline

    Markyroson

    Here is the entirety of my config file:

    Code:
    # http://dev.bukkit.org/bukkit-plugins/staff-lists/
    # Made by Markyroson
    # don't forget to place "" or '' around the text you don't want to get printed!
    # For Example, to have a line blank (and not display base message) do something like the following. for this example we
    # will use the line Admin
    # Admin: ''
    # You can use & for color codes (shift + 7)
    
    Owner: Place the Owners of the server here!
    Admin: Place the Admins of the server here!
    Mod: Place the Mods of the server here!
    Server: Place the name of your server here
    
    Line 89 is
    Code:
     List<String> list = plugin.getConfig().getStringList("Admin");
     
  8. Offline

    mine-care

  9. Online

    timtower Moderator Moderator

    Moved to Bukkit alternatives
     
  10. Offline

    Markyroson

    I know more or less how to read a stacktrace. The issue is that no matter what I do I always get the same error. I did notice some things in the code (which I corrected) but it does not change the outcome.

    @justin_393

    Updated code:

    Code:
     if(args[0].equalsIgnoreCase("add")) {
              if(args[1].equalsIgnoreCase("admin")) {
                      List<String> list = (List<String>) plugin.getConfig().getList("Admin");
                      list.add(args[2]);
                      plugin.getConfig().set("Admin", list);
                      plugin.saveConfig();
                      plugin.reloadConfig();
                      p.sendMessage("DONE!! :-)");
                      return true;
          }
              else if(args[1].equalsIgnoreCase("mod") || args[1].equalsIgnoreCase("moderator")) {
                      List<String> list = (List<String>)plugin.getConfig().getList("Mod");
                      list.add(args[2]);
                      plugin.getConfig().set("Mod", list);
                      plugin.saveConfig();
                      plugin.reloadConfig();
                      p.sendMessage("DONE!");
                      return true;
              }
      }
    Updated config code:
    Code:
    # http://dev.bukkit.org/bukkit-plugins/staff-lists/
    # Made by Markyroson
    # don't forget to place "" or '' around the text you don't want to get printed!
    # For Example, to have a line blank (and not display base message) do something like the following. for this example we
    # will use the line Admin
    # Admin: ''
    # You can use & for color codes (shift + 7)
    
    Owner:
    - Name
    Admin:
    - Name
    Mod:
    - Markyroson
    Server: Place the name of your server here
    
    Stack trace:
    Code:
    [19:25:11] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'staff' in plugin Staff-Lists v0.3
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[minecraft_server.jar:git-Bukkit-30d751b]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[minecraft_server.jar:git-Bukkit-30d751b]
        at org.bukkit.craftbukkit.v1_8_R2.CraftServer.dispatchCommand(CraftServer.java:625) ~[minecraft_server.jar:git-Bukkit-30d751b]
        at net.minecraft.server.v1_8_R2.PlayerConnection.handleCommand(PlayerConnection.java:1077) [minecraft_server.jar:git-Bukkit-30d751b]
        at net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java:937) [minecraft_server.jar:git-Bukkit-30d751b]
        at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(SourceFile:37) [minecraft_server.jar:git-Bukkit-30d751b]
        at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(SourceFile:9) [minecraft_server.jar:git-Bukkit-30d751b]
        at net.minecraft.server.v1_8_R2.PlayerConnectionUtils$1.run(SourceFile:13) [minecraft_server.jar:git-Bukkit-30d751b]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_45]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_45]
        at net.minecraft.server.v1_8_R2.SystemUtils.a(SourceFile:60) [minecraft_server.jar:git-Bukkit-30d751b]
        at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:670) [minecraft_server.jar:git-Bukkit-30d751b]
        at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:336) [minecraft_server.jar:git-Bukkit-30d751b]
        at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:626) [minecraft_server.jar:git-Bukkit-30d751b]
        at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:534) [minecraft_server.jar:git-Bukkit-30d751b]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    Caused by: java.lang.NullPointerException
        at me.Markyroson.StaffLists.Main.onCommand(Main.java:90) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[minecraft_server.jar:git-Bukkit-30d751b]
        ... 15 more
    I know that line 90 is null (according to the stack trace) yet it does not appear null in the code to me, any ideas?

    I also used this as a reference/rough idea how to do this to no avail.
     
    Last edited: Jun 5, 2015
  11. Offline

    mine-care

    @Markyroson what is line 90? Is it still the one that gets a list from the config? Have you debuged at all?
     
  12. Offline

    Boomer

    if line 90 is the ... plugin.getConfig... one then you have either
    1) no yml part to load - but you show that that is not true, you have that line in the file
    2) the plugin object is null at the point you use it.
     
  13. Offline

    Markyroson

    Yes I have debugged as the error moved down a line and is in both line 3 (aka 90) and line 12 (aka 99).

    That is strange (also see above comment I made for more info).
     
  14. Offline

    Boomer

    Whats strange about it - that confirms exactly what it is.
    You dont have plugin defined at that point of execution.
     
  15. Offline

    Markyroson

    Yes, but I am not sure where it becomes undefined. I will take a look a little later.
     
  16. Offline

    Boomer

    It doesn't become undefined.
    It never gets set.

    Post your whole class
     
  17. Offline

    Markyroson

    Main class:
    Code:
    package me.Markyroson.StaffLists;
    
    import java.io.IOException;
    import java.util.List;
    import java.util.logging.Logger;
    
    import me.Markyroson.Metrics.Metrics;
    import me.Markyroson.lib.Names;
    import me.Markyroson.lib.Enumerations.Permissions;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Sign;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.block.SignChangeEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    /**
    *
    * @author Markyroson
    * This is the main class/file for the plugin and, without it, the plugin would not function...PERIOD
    *
    */
    public class Main extends JavaPlugin implements Listener
    {
      public final Logger logger = Logger.getLogger(Names.logger);
      public static Main plugin;
    
      public void onEnable()
      {
        PluginDescriptionFile pdfFile = getDescription();
        this.logger.info(pdfFile.getName() + " Succesfully started up." + pdfFile.getVersion());    //start up message
        Bukkit.getServer().getPluginManager().registerEvents(this, this);    //register events
        getConfig().options().copyDefaults(true);    //set default options of file does not exist
        getConfig();    //get config file
        saveConfig();    //save config file
        try {
            Metrics metrics = new Metrics(this);    //create new instance of metrics
            metrics.start();    // start metrics
        } catch (IOException e) {
            // Failed to submit the stats :-(
        }
      }
    
      public void onDisable() {
        PluginDescriptionFile pdfFile = getDescription();
        this.logger.info(pdfFile.getName() + " Succesfully shut down.");    //shut down message
      }
    
    @SuppressWarnings({ "deprecation", "unchecked" })
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
      {
          if (sender instanceof Player) {
          Player p = (Player) sender;
        if (commandLabel.equalsIgnoreCase("staff")) {
          if (args.length == 0) {
            if (sender.hasPermission("StaffLists.staff.view.base")) {
                sender.sendMessage("§6§l=====< §2§lStaffLists by Markyroson §6§l>=====");
                sender.sendMessage(ChatColor.GOLD + "/staff members");
                sender.sendMessage(ChatColor.GOLD + "/staff reload " + ChatColor.AQUA + "(for admins/owners only)");
                sender.sendMessage(ChatColor.GOLD + "/staff help");
            } else {
              sender.sendMessage(ChatColor.RED + "You don't have permissions to do this!");
              return true;
            }
    
            return false;
          }
         // try {
        //  if(args.length == 2) {
          if(args[0].equalsIgnoreCase("add")) {
              if(args[1].equalsIgnoreCase("admin")) {
                //  Player target = Bukkit.getServer().getPlayer(args[2]);
                //  if (target == null) {
                //      p.sendMessage("That player isn't online");
               //   } else {
                      List<String> list = (List<String>) plugin.getConfig().getList("Admin");
                      list.add(args[2]);
                      plugin.getConfig().set("Admin", list);
                      plugin.saveConfig();
                      plugin.reloadConfig();
                      p.sendMessage("DONE!! :-)");
                      return true;
            //  }
          }
              else if(args[1].equalsIgnoreCase("mod") || args[1].equalsIgnoreCase("moderator")) {
                 // Player target = Bukkit.getServer().getPlayer(args[2]);
                //  if (target == null) {
                 //     p.sendMessage("That player isn't online");
                //  } else {
                      List<String> list = (List<String>)plugin.getConfig().getList("Mod");
                      list.add(args[2]);
                      plugin.getConfig().set("Mod", list);
                      plugin.saveConfig();
                      plugin.reloadConfig();
                      p.sendMessage("DONE!");
                      return true;
        //      }
              }
      }
         // } catch(NullPointerException e)
        //  {
       //       System.out.print("NullPointerException caught");
       //   }
        //  }
          if (args.length == 1) {
            if (args[0].equalsIgnoreCase("help")) {
              if (sender.hasPermission(Permissions.VIEW_HELP.add())) {    //if player has permission do this
                if (args[0].equalsIgnoreCase("help")) {
                  sender.sendMessage(ChatColor.WHITE + "[StaffLists version " + ChatColor.AQUA + getDescription().getVersion() + ChatColor.WHITE + "] " + ChatColor.DARK_AQUA + "This is the help menu of Staff Lists, made by Markyroson");
                  sender.sendMessage(ChatColor.GOLD + "/staff " + ChatColor.AQUA + "shows you the full list of plugin commands");
                  sender.sendMessage(ChatColor.GOLD + "/staff help " + ChatColor.AQUA + "shows you this menu");
                  sender.sendMessage(ChatColor.GOLD + "/staff members " + ChatColor.AQUA + "shows you the staff of the server");
                  sender.sendMessage(ChatColor.GOLD + "/staff reload " + ChatColor.AQUA + "reloads the staff config");
                  return true;
                }
              } else {
                sender.sendMessage(ChatColor.RED + "You dont have permission to do this!");    //if they don't have permission send this message
                return true;
              }
            } else if (args[0].equalsIgnoreCase("reload")) {
              if (sender.hasPermission(Permissions.RELOAD.add())) {
                reloadConfig();    //reload config file
                sender.sendMessage(ChatColor.LIGHT_PURPLE + "The staff lists config is reloaded!");    //send player completion message
                return true;
              }
            } else if (args[0].equalsIgnoreCase("members")) {
                if (sender.hasPermission(Permissions.VIEW_MEMBERS.add())) {
                    if (args[0].equalsIgnoreCase("members")) {
                        sender.sendMessage(ChatColor.GOLD + "Owner(s): " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Owner")));
                          sender.sendMessage(ChatColor.AQUA + "Admin(s): " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Admin")));
                          sender.sendMessage(ChatColor.GREEN + "Moderator(s): " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Mod")));
                          sender.sendMessage(ChatColor.RED + "On the server: " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Server")));
                        } else {
                          sender.sendMessage(ChatColor.RED + "You don't have permissions to do this!");    //if this is called then sender does not have permission to view this and therefore send them message saying so
                          return true;
                    }
                }
    
            } else {
                sender.sendMessage(ChatColor.RED + "This command doesn't exist! Use /staff help for info!");    //to fall here the command must not have existed, therefore send this message telling the sender so
              return true;
            }
          }
       }}
        return false;
          }
    
      @EventHandler
      public void onPlayerPlace(SignChangeEvent e) {
          Player p = e.getPlayer();    //get player in event
        if ((e.getPlayer().hasPermission(Permissions.ADMIN.add())) &&
          (e.getLine(0).contains("[staff]"))) {    //if first line contains [staff] AND player has permission contained in Names.admin do the following
          e.setLine(0, ChatColor.GREEN + "[" + ChatColor.AQUA + "Staff" + ChatColor.GREEN + "]");    //set line 1
          e.setLine(1, ChatColor.DARK_AQUA + "Click here to");    //set line 2
          e.setLine(2, ChatColor.BLUE + "see the");    //set line 3
          e.setLine(3, ChatColor.DARK_GREEN + "server's staff");    //set line 4
        } else {
            p.sendMessage("You don't have permission to perform this command!");    //if they don't have permission, send this message
        }
      
      }
    
      @EventHandler
      public void interact(PlayerInteractEvent e) {
        Player p = e.getPlayer();
        if ((e.getAction() == Action.RIGHT_CLICK_BLOCK) &&
          (p.hasPermission("StaffLists.staff.view.base")) && (
          (e.getClickedBlock().getType().equals(Material.WALL_SIGN)) || (e.getClickedBlock().getType().equals(Material.SIGN_POST)))) {
          Sign sign = (Sign)e.getClickedBlock().getState();
          if ((sign.getLine(0).contains(ChatColor.GREEN + "[" + ChatColor.AQUA + "Staff" + ChatColor.GREEN + "]")) && (sign.getLine(1).contains(ChatColor.DARK_AQUA + "Click here to")) && (p.hasPermission(Permissions.VIEW_PERM_BASE.add()))) {
            p.sendMessage(ChatColor.GOLD + "Owner(s): " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Owner")));
            p.sendMessage(ChatColor.DARK_AQUA + "Admin(s): " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Admin")));
            p.sendMessage(ChatColor.GREEN + "Moderator(s): " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Mod")));
            p.sendMessage(ChatColor.RED + "On the server: " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("Server")));
          }
        }
      }
    }
     
  18. Offline

    Boomer

    exactly that.
    You define plugin...
    But you never give it a value at any point.

    onEnable ....

    plugin=this;

    and that will do it.


    Just remember always for null pointer errors:
    whenever using

    Some.object.with().many().other.parts().and().pieces
    in a dereferencing manner, like setting some other object to the value of ^^that thing

    the system will throw a null pointer if it can't be dereferenced further. Pieces could be null, and setting something = Some.object.with().many().other.parts().and().pieces wont throw an error if pieces is null, it just sets something=null

    But, if the object returned by and() is a null object, you can't get the pieces parameter on a null object.
    Or, if parts() returns a null object, you can't do the and() method on that null object
    Or if other is a null object, you cant do the parts() method on it
    Or if many() returns a null object, you cant get the other property from it
    Or if with() returns a null object, you cant get the many() method to work on it
    or if object is null, then with() wont work on it
    or if the Some object itself is null, then there is nothing to act on


    Thats how you debug null pointer errors - find the line. Then look at everything being dereferenced. Chase it all the way.

    In the case of plugin.getConfig().getList("Blah")...

    Check 1: Is "Blah" an actual existing entry in the yml file
    Check 2: well, getConfig() ALWAYS will be part of a plugin object if the object exists, it will never return null
    Check 3: plugin may be null - is it set by the time it reaches here

    Thats proper debugging for null pointers - check each dereferencable object in the chain.
     
  19. Offline

    Markyroson

    Thank you, that worked and fixed the issue. How would I go about removing a particular entry from the list in the config? I tried plugin.getConfig().remove thinking it might exist as well as plugin.getConfig().delete but they do not :-(

    EDIT: I found this (is the latter "erasing a value" one) but it only removes everything from the list.
     
    Last edited: Jun 6, 2015
  20. Offline

    Boomer

    as far as i know, the only way is to get your list item, purge the entries from the list, then write the shorter list back to config.
     
    Markyroson likes this.
  21. Offline

    Markyroson

    How would you go about retaining the list, minus that particular entry, to be able to re-write it to the config though?
     
  22. Offline

    Boomer

    Thats where you'll need to do some of your own investigation with java core concepts on list manipulation.

    Either you ARE persisting a list object after loading, so that you can edit that persisted object and resave on demand, or you're just loading the list from config 'temporarily' to then do something else with it.

    If the latter.. then make a method that takes the 'delete-me' element as an argument, and load in the list from config, manipulate that list to remove the argument element, then write that modified list to the log.
    Or a list-returning function that takes the delete argument, loads the config into a temp list, iterates it and any result not matching the delete is added to a new list, then write that returned list to the config.

    How you impliment is up to what you can best understand , how you plan to incorporate the list of information from the config into the grand picture of your plugin - if its already a list shared throughout the plugin, you already have it. if its constantly checking the config each time it does something, then work based on that. And like loops and iterator systems, and set manipulators, there are many ways to pull off editing a list object, find one that you can understand.
     
  23. Offline

    Markyroson

    Thank you for your help.
     
Thread Status:
Not open for further replies.

Share This Page