Error using commands to change config

Discussion in 'Spigot Plugin Development' started by DeathPenalizer, Jun 8, 2017.

  1. What Goes Wrong:
    I am trying to make a warp command, and it keeps yelling at me saying I cannot set to an empty path. Here is the code, the plugin.yml, and the error.


    Code:
    Main.java
    Code (Text):
           
            Player p = (Player)sender;
            String warpName = "";
               
            if (!(sender instanceof Player)) {
                return true;
            }
               
            if (cmd.getName().equalsIgnoreCase("setwarp")) {
                if (args.length == 0) {
                       
                    p.sendMessage(translate("&c&lError: &fYou must set a name when making a warp!"));
                   
                }else if (args.length == 1) {
                       
                    warpName = "";
                    for (int i = 1; i < args.length; i++)
                    {
                            warpName += args[i] + " ";
                    }
                    warpName = warpName.trim();
                               
                    getConfig().set("warps." + warpName, p.getLocation());
                    saveConfig();
                       
                }else {
                       
                    p.sendMessage(translate("&c&lError: &fIncorrect usage! &6/setwarp &c{name}"));
                       
                }
            }
               
            if (cmd.getName().equalsIgnoreCase("delwarp")) {
                if (args.length == 0) {
                       
                    p.sendMessage(translate("&c&lError: &fYou must choose a warp name to delete!"));
                   
                }else if (args.length == 1) {
                       
                   
                       
                }else {
                       
                    p.sendMessage(translate("&c&lError: &fIncorrect usage! &6/delwarp &c{name}"));
                       
                }
            }
               
            if (cmd.getName().equalsIgnoreCase("warp")) {
                if (args.length == 0) {
                       
                    p.sendMessage(translate("&c&lError: &fYou must name the warp you wish to teleport too!"));
                   
                }else if (args.length == 1) {
                       
                    if (p.hasPermission("Multi.warps." + warpName)) {
                           
                       
                           
                    }
                       
                }else {
                       
                    p.sendMessage(translate("&c&lError: &fIncorrect usage! &6/warp &c{name}"));
                       
                }
            }
       
            return false;
    This is all placed inside onCommand.

    Plugin.yml
    Code (Text):

    name: MultiUtil
    main: me.multigen.util.main
    version: 1.0
    author: LightDarkness
    commands:
      setwarp:
        description: Allows the player to set a warp.
        permission: Multi.setwarp
        permissions-message: Permission denied.
      delwarp:
        description: Allows the player to delete a warp.
        permission: Multi.delwarp
        permissions-message: Permission denied.
      warp:
        description: Allows the player to warp.
        permission: Multi.warp
        permissions-message: Permission denied.
     
    Error:
    Code (Text):

    [00:20:29 INFO]: LightDarkness issued server command: /setwarp Hmm
    [00:20:29 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'setwarp' in plugin MultiUtil v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchCommand(CraftServer.java:650) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.PlayerConnection.handleCommand(PlayerConnection.java:1354) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1189) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8] at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_112] at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_112]
            at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:747) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at java.lang.Thread.run(Thread.java:745) [?:1.8.0_112]
    Caused by: java.lang.IllegalArgumentException: Cannot set to an empty path at org.apache.commons.lang.Validate.notEmpty(Validate.java:321) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at org.bukkit.configuration.MemorySection.set(MemorySection.java:167) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at org.bukkit.configuration.MemorySection.set(MemorySection.java:201) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8] at me.multigen.util.main.onCommand(main.java:58) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8] ... 15 more
     
     
  2. Mind posting in a gist/pastebin/hastebin? That way we can see the line numbers correctly.

    Just for the sake of pointing out the error, this line tells you you're issue:
    at org.bukkit.configuration.MemorySection.set(MemorySection.java:201) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8] at me.multigen.util.main.onCommand(main.java:58) ~[?:?]

    So we can see that the method "me.multigen.util.main.onCommand(...)" is causing an issue. More specifically on line 58 of the file.

    Off-topic: I would like to suggest you look over Java naming conventions and their rationale. Just to help better your code's cleanliness and overall agnostic-ness(that's a word, right?) in terms of developers reading your code.
     

  3. Sure thing, and yea, I am aware of that, just not the best at doing it haha, here is the hastebin link.
    https://hastebin.com/exuyarihar.java
     
  4. Here, you have a few errors:
    } else if (args.length == 1) {

    warpName = "";
    If args lenght is 1, this loop its innecesary
    for (int i = 1; i < args.length; i++) Also, for looping through an array/list you need to start always at 0 index
    {
    warpName += args + " ";
    }
    warpName = warpName.trim();

    getConfig().set("warps." + warpName, p.getLocation());
    saveConfig();
     

  5. So if I do it like this, would it work then?

    Code (Text):
                if (args.length == 0) {
                       
                    p.sendMessage(translate("&c&lError: &fYou must set a name when making a warp!"));
                   
                }else if (args.length == 1) {
                       
                    args[0] = warpName;
                               
                    getConfig().set("warps." + warpName, p.getLocation());
                    saveConfig();
                       
                }else {
                       
                    p.sendMessage(translate("&c&lError: &fIncorrect usage! &6/setwarp &c{name}"));
                       
                }
     
  6. You have it at wrong order, warpName = args[0];

    Also, here you have your code with some code and changes, hope you understand it!

    https://hastebin.com/xebolequli.java
     
  7. unless you try, we won't know either.
     
  8. Yeah, try the new code always and if you have another error come here to ask, not ask for every little change at your code!