Solved Simple Question about, "if(target == null)"

Discussion in 'Plugin Development' started by Firefox365, Oct 5, 2012.

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

    Firefox365

    Ok so I am getting my plugin working and stuff and all seems perfect but then as I type a command I screw it up and I find an error or at least i think its one.

    Since I can can have someone to sit on my server to help me bug test I set up a pvt. server and set it to run in offline mode. Then I log my account in the log a offline client in so that the name for that client is Player. Then I can easily test my plugin.

    So no that I defined how I test it I'm now going define what the issue is:

    (Fyi I am going to use "_" in place of spaces to more easily show you where the spaces are.)

    My plugin can blow up other players so if I type: /blow _ <player> _ [ExplosionPower] it works.

    I have in my code a part that says if(target == null) then it give an error message to the player notifying the player is invalid, Just like it suppost to.

    But if I type: /blow_ _[ExplosionPower]
    with a space rather then a name it automatically thinks the space = player and blow him up.

    Now my question is: Does anyone know if maybe its just like this because there is an issue with player not being an actual account. Or do I need to somehow define that if args[0] is = to "_" then do then give error message?

    Thanks! =P
     
  2. Offline

    Courier

    The tokenizer for command arguments probably considers "arg1__arg2" as this list: {"arg1", "", "arg2"} because it includes every substring that is surrounded by whitespace. There is an empty string surrounded by whitespace in between "arg1" and "arg2". You could write your script to skip empty arguments, but it shouldn't be a big issue. People should know to only put one space between their arguments.
     
    kroltan likes this.
  3. Offline

    Firefox365

    Yes, True but my I'm not to worried about that but my question is if somewhere to slip up.. would this like explode everyone on the server? cuz that could be disastrous.
     
  4. Offline

    Courier

    You're fine. If you call Bukkit.getPlayerExact("") it should return null.
     
    Firefox365 likes this.
  5. Offline

    Firefox365

    Thanks so much. Fixed the error right away.

    Hey Just of the top of your head do you know how to do a float maximum? So my ExplosionPower has a max?

    Here is the thing I use to grab the power from the command:

    Code:
    try {power = Float.parseFloat(args[1]);}


    Dont really want to make another topic for it. But if you don't thats fine.

    Never mind I figured it out!
    Thanks anyways! =D
     
  6. Offline

    Courier

    You mean clamp the value so it can't be too high?
    Code:java
    1. if(power > MAX_POWER)
    2. power = MAX_POWER;
    Where MAX_POWER is a float constant:
    Code:java
    1. private static final float MAX_POWER = 10f;
     
  7. Offline

    Firefox365

    Yes Thanks. Lol i just figured it out.. hah then I saw you where viewing my post. But thanks!
     
  8. Offline

    kroltan

    Remember, using getPlayerExact() can be boring for users. What if someone is called therandomdudeofawshumness15874? They'd have to write all that into the command for it to work.
     
  9. Offline

    Firefox365

    Lol I just noticed that. Do you know how i can make it not exact and not have in issues with the spaces?
    Thanks!
     
  10. Offline

    kroltan

    Code:java
    1. if (target.equals("")) {
    2. //Your stuff here
    3. Player player = server.getPlayer(target);
    4. //Your stuff here
    5. }
     
    Firefox365 likes this.
  11. Offline

    Firefox365

    Thanks! Worked like a charm.
     
  12. Offline

    Courier

    I'm pretty sure usernames cannot be more than 16 characters long, for one thing.


    It would be better to use a TabCommandExecutor, so users can hit tab to autocomplete from a list of online players. With this, they know exactly which player is going to be selected.
     
  13. Offline

    Firefox365

    Thanks But I like the ability to just type the first couple letters of the name really fast. What kroltan suggested I actulty didn't get it to work. I initaly thought I did but I guess i have to fiddle with it some more. If you have any other better methods that can fix the space Then please do tell!
    TY
     
  14. Offline

    kroltan

    If you send me that part of the code (Here or via PM), I can make it work for you, and document the changes so you know why it works.
     
  15. Offline

    Firefox365

    Thanks very much but I got it to work I did this:


    Code:Java
    1. if(args[0].equalsIgnoreCase("")){
    2. sender.sendMessage(ChatColor.RED + "No Player Indicated!");
    3. return false;}


    But thanks very much!
     
    kroltan likes this.
  16. Offline

    kroltan

Thread Status:
Not open for further replies.

Share This Page