Having trouble with a chat plugin

Discussion in 'Plugin Development' started by mysterypotatoguy, Nov 5, 2014.

Thread Status:
Not open for further replies.
  1. Hey, so I'm trying to develop a plugin for a sort of RP chat, and it is giving me an internal error. I think I know why this is happening but my question is can I fix it? is there another way to write this?
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    2. if (cmd.getName().equalsIgnoreCase("s")) {}
    3. Player Sender = (Player) sender;
    4. Player Receiver = (Player) Sender.getNearbyEntities(10, 10, 10);
    5.  
    6. if(args.length == 0)
    7. Sender.sendMessage(Color.RED + "Usage: /s <Message>");
    8.  
    9. if(args.length > 0)
    10. Sender.sendMessage(Sender + ":" + args[0]);
    11. Receiver.sendMessage(Sender + ":" + args[0]);
    12. return true;}}

    Error being here: Player Receiver = (Player) Sender.getNearbyEntities(10, 10, 10);
     
  2. Offline

    Barinade

    Entities is plural, Player is not
     
  3. I don't follow...
     
  4. Offline

    leon3001

    The getNearbyEntities method returns multiple entities, you are assuming there's only one player in the given radius. I would use a for-loop and then send the message.
     
  5. Ah, I tried that one before but I couldn't get it to work
     
  6. Offline

    leon3001

    Post what you did and we could help you fix it. ;)
     
  7. Offline

    Barinade

    Player receiver = null;
    for (Entity en : Sender.getNearbyEntities(10,10,10)) {
    if (en instanceof Player) {
    receiver = (Player) en;
    break;
    }
    }
    if (Receiver != null) {
    //receiver found
    }
     
  8. Thank you very much, both of you :)
     
  9. Offline

    leon3001

    Please don't spoonfeed! >.< It's the worst thing you can do when trying to really help someome else.
     
  10. Offline

    Barinade

    Only if they take the code and don't analyze it. It's all on them.
     
  11. Offline

    Zupsub

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    2. if (cmd.getName().equalsIgnoreCase("s")) {}

    Your if is empty, making it useless.

    Code:text
    1.  
    2. Player Sender = (Player) sender;


    NEVER EVER cast if you aren't sure you can actually cast the object. If you're command is run from the console or from rcon, you're getting a ClassCastException here.
    Use instanceof to check whether the sender is a player.

    Also please use lowerCamelCase for variable names!

    Code:text
    1.  
    2. Player Receiver = (Player) Sender.getNearbyEntities(10, 10, 10);
    3.  

    Already said. Don't you use an IDE? That should have told you you're mistake. Also look at the docs: http://jd.bukkit.org

    Code:text
    1.  
    2. if(args.length == 0)
    3. Sender.sendMessage(Color.RED + "Usage: /s <Message>");
    4.  

    Great, you check whether all required arguments have been passed. But you dont return; the method if there weren't any arguments send, which will cause an ArrayIndexOutOfBoundsException later.

    Code:text
    1.  
    2. if(args.length > 0)
    3. Sender.sendMessage(Sender + ":" + args[0]);
    4.  
    just for your information: if a user inputs multiple words, they will be treated as multiple arguments. So if I type
    /yourCommand Hi there!
    You're getting two arguments. You'd probably want to send them all.
     
    leon3001 likes this.
  12. Yes, I am using Eclipse, for some reason it didn't show that as an error :/
    also, what would I put for sending all arguments? I haven't come across that before
     
  13. Offline

    Barinade

    String msg = "";
    for (String s : args) {
    msg += s + " ";
    }

    Basically.
     
  14. I think I understand this. Thank you :)
     
Thread Status:
Not open for further replies.

Share This Page