Solved Command Parameters

Discussion in 'Spigot Plugin Development' started by coreXtream, Mar 8, 2018.

  1. Hey guys i wanted to know if you can help me with a "standard" feedback if a command wasnt executed right.

    I want to send the player a message if he doesnt write a player name behind the command:

    /duel playername

    for the first i put it to the top before anything other happends but so far this seemed to be false:


    When i use /duel

    When i use /duel myownname

    Hope you guys can help me ;D
  2. After sending the message, it tries to get index 0 of the array ("Bukkit.getPlayer(args[0])") - which does not exist because you did not specify any arguments.
    You can get the length of the array via args.length
    If you see "internal error", the console will show you what and where the error is :)
  3. And what's this internal error?
  4. that i dont wrote an name behind /duel
    but i want to have the custom message there:

    cs.sendMessage(prefix + "You need to duel an other Player [ /duel §bplayer§r§7 ]");
  5. the internal error is not my problem ... i just want the custom message instead of the error which is caused by not writing a name behind /duel:

    cs.sendMessage(prefix + "You need to duel an other Player [ /duel §bplayer§r§7 ]");
  6. It actually is the problem, it can show us/you where it goes wrong and why. Which makes helping a lot easier. Instead of a guessing game and referring to 'make sure you use the right syntax and compare against the correct argument'
  7. Code (Text):
    [21:08:24 INFO]: coreXtream issued server command: /duel Enderisss
    [21:08:24 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'duel' in plugin AuroraRealmsFactions v1.0
            at org.bukkit.command.PluginCommand.execute( ~[spigot.jar:git-Spigot-5f38d38-18fbb24]
            at org.bukkit.command.SimpleCommandMap.dispatch( ~[spigot.jar:git-Spigot-5f38d38-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand( ~[spigot.jar:git-Spigot-5f38d38-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand( [spigot.jar:git-Spigot-5f38d38-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a( [spigot.jar:git-Spigot-5f38d38-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a( [spigot.jar:git-Spigot-5f38d38-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a( [spigot.jar:git-Spigot-5f38d38-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$ [spigot.jar:git-Spigot-5f38d38-18fbb24]
            at java.util.concurrent.Executors$ Source) [?:1.8.0_161]
            at Source) [?:1.8.0_161]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-5f38d38-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B( [spigot.jar:git-Spigot-5f38d38-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B( [spigot.jar:git-Spigot-5f38d38-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A( [spigot.jar:git-Spigot-5f38d38-18fbb24]
            at [spigot.jar:git-Spigot-5f38d38-18fbb24]
            at Source) [?:1.8.0_161]
    Caused by: java.lang.NullPointerException
            at ~[?:?]
            at org.bukkit.command.PluginCommand.execute( ~[spigot.jar:git-Spigot-5f38d38-18fbb24]
            ... 15 more
    but the line 36 has nothing to do with that ... it is just an "unused" String that is replace with the enemy player name which i uses for a message in the
  8. the internal error is because you're accessing the args without checking length first in the `cs == Bukkit.getPlayer(args[0])` statement
    you're always getting the message because you send it without anything further

    this is a classic case of not knowing java..
  9. Guys i know why i get the internal error and i dont mind it .... i just want to know how i can give the standart feedback of an command ( how i can give a feedback from the command when no parameter is used )
  10. As the others have mentioned, this is the problem. To return the default usage of a command registered on the plugin.yml, return false to indicate that the command was executed unsuccessfully.

    There are a whole lot of errors in your code. Right now, if the console were to execute this command, it will tell the console it can't duel itself, even if it specified another player. For usability purposes, this is very weird.

    Also, what is "Bukkit.getPlayer(args[0]) == Bukkit.getOnlinePlayers()" supposed to do? Not only do you compare two objects using == instead of .equals(), you also are trying to compare 1 player with a collection of all online players. This is not possible, it's like trying to compare one apple with a collection of apples.

    Another problem is the cast of CommandSender to Player without checking if the cs is actually a Player. When the console executes this command, an error is thrown.

    Another problem is the line under that. Bukkit.getPlayer() can return null, since your previous check isn't sufficient, this might be null if the player is online. You then use .getName() on a possible null object which will throw an NPE.

    Please try to get more familiar with Java, you are making a lot of beginner mistakes that you might not realize that can cause unexpected behaviour that might frustrate you, whereas when you spend some more time learning Java these problems should be clear to you.
  11. When i write a player name in it my console didnt give me any errors

    This was just a try and i know this is wrong but i didnt find a other method myself how to check the players correctly and this would be my next job after i finished the messages

    I dont need to check that cause i want to try the command with the console to look if i get the message from the pl cause i have only 1 mc account at the time...

    Thx but even as the other things i just want to finish the message first before i am doing the other things ...

    Yes i know that i am not good at coding but this is why i ask you ... and i think this forum is there to ask other people that are more familiar with the API's and Java... but thx that you wrote this all for me i will make a list and correct all the things you said ^^
  12. I don't know the scope of the project you are working on (that is, whether it is a private personal-use plugin or a public resource). Either way, prevention is better than having to patch up afterwards. Even though during normal operation, everything works fine, this doesn't mean that anything unexpected shouldn't be handled correctly. Developers can detect what went wrong when trying to do something with a plugin, but server owners who are less experienced with reading stacktraces or coding in general might not.

    For example, a server owner might not know that a command can only be executed by a player (though clearly that's the case, such as a TP command). You can choose to either ignore this problem as it's odd behaviour, not the expected flow. However, it will throw an error in the face of the server owner. This makes the server owner think that the plugin is broken. Sending some additional information why the server owner did a stupid move is a better solution than throwing a stacktrace in their face.

    Now obviously, you already solved the problem as you marked the thread as solved. I merely wanted to add this little message to this thread to explain why prevention of errors is better than simply throwing them in the face of someone who isn't too experienced with it. Even if the plugin is for private use, I personally find it a lot easier to deal with unexpected problems because if I visit my plugin after a few months after writing it again and it throws internal exceptions, I get really confused even if I myself made a mistake when executing a command.

Share This Page