args problem

Discussion in 'Plugin Development' started by kingsofbart, Sep 8, 2015.

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

    kingsofbart

    Hello,


    I got a problem with this script:
    Code:java
    1. ]
    2. package Main;
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.plugin.Plugin;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class main extends JavaPlugin implements Listener{
    14.  
    15. public void onEnable() {
    16. getConfig().options().copyDefaults(true);
    17. saveConfig();
    18. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    19. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask((Plugin) this, new Runnable() {
    20. public void run() {
    21. for (Player p : Bukkit.getServer().getOnlinePlayers()) {
    22. String l = p.getPlayerListName();
    23. int lx = getConfig().getInt(l + ".exlevel");
    24. String le = getConfig().getString(l + ".Level");
    25. p.setCustomName(le);
    26. p.setExp(lx);
    27.  
    28. }
    29. }
    30. }, 0, 20);
    31. }
    32. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    33. Player p = (Player) sender;
    34.  
    35. if (!(sender instanceof Player)) {
    36. sender.sendMessage("You have to be online.");
    37. return true;
    38. }
    39. if (cmd.getName().equalsIgnoreCase("xs")) {
    40. if(args.length == 0){
    41. p.sendMessage(ChatColor.GREEN + "Use /xs help");
    42. return true;
    43. }if(args.equals("help")){
    44. p.sendMessage("todo");
    45. return true;
    46. }if(args[0].equals("add")){
    47. if(args[1].equals("level")){
    48. if(Bukkit.getPlayer(args[2]).isOnline()){
    49. if(isInt(args[3])){
    50. String le = getConfig().getString(p + ".Level");
    51. getConfig().set(le, le + args[2]);
    52. return true;
    53. }
    54. }
    55. }
    56. }
    57.  
    58.  
    59. }
    60. return true;
    61. }
    62. public boolean isInt(String str) {
    63. try {
    64. Integer.parseInt(str);
    65. return true;
    66. } catch (NumberFormatException e) {
    67. return false;
    68. }
    69. }
    70. }
    71.  
    72.  
    73.  

    The error:
    Code:
    org.bukkit.command.CommandException: Unhandled exception executing command 'xs' in plugin Xpsystem v0.1
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_60]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_60]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:714) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:653) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:556) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_60]
    Caused by: java.lang.IllegalArgumentException: Cannot set to an empty path
            at org.apache.commons.lang.Validate.notEmpty(Validate.java:321) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.configuration.MemorySection.set(MemorySection.java:163) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at Main.main.onCommand(main.java:50) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            ... 15 more
    
    Code:
    name: Xpsystem
    main: Main.main
    version: 0.1
    author: Magicdutchcraft
    commands:
       xs:
          description: Xpsystemcommand
          usage: /<command>
    
    Bart
     
    Last edited by a moderator: Sep 8, 2015
  2. Offline

    timtower Administrator Administrator Moderator

    @kingsofbart
    getConfig().set(le, le + args[2]);
    le = null, or at least not usable.
    Please use naming conventions, let variable names tell what they contain.
    Use a different main, you will get into trouble with that very fast.
    Use p.getName() or p.getUniqueUid().toString() instead of using it directly in strings.
    Check if the args is long enough
     
  3. @kingsofbart On line 50, you are trying to grab a string called "Level" that is under the player's name. Are you sure this string exists ? Where the error says "cannot set to an empty path" that is because the variable LE is null. Always implement checks on your code.
    Code:
    if(le == null) {
      getLogger().info("Couldn't grab the required string!  Aborting ... ");
      return false;
    }
     
  4. Offline

    kingsofbart

    Thanks all, @_zircon_ ill add it right now!
     
  5. Offline

    RoboticPlayer

    Don't use
    Code:
    if (args.equals(/*string/*) {
    //Stuff
    }
    Instead, use
    Code:
    if (args[/*argument number you want to check*/].equalsIgnoreCase(/*string*/)) {
    //do stuff
    }
     
Thread Status:
Not open for further replies.

Share This Page