Solved Simply.. /message | Please help.

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

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

    SleepyDog

    I am trying to make my server fully custom, except essentialds and worldedit ect...
    This is the user plugin i am creating and the problem is i need it to work...
    I have been looking around and although it is one of the most used commands i still cant find anything about it. What i need to do is:
    -Change the string to a player.
    -Get the message that the player want's to send.
    -Check if the receiver is online.
    -Check if they have entered a receiver.
    -Check if they have entered a message.
    The command will be: /msg <player> <message>
    The layout for sender will be: You --> Receiver >> message
    The layout for receiver will be: Sender --> You >> message

    Now that you know what i want to make :p

    This is my attempt. I would like to know how to do some of the aboth, and fix my attemped code already. (Personal use, will NOT be sent to anyone.) (Not a public server(yet))

    See what you can do with this, it really helps out, Thanks!

    Code:java
    1. package me.sleepydog935;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class User extends JavaPlugin {
    10.  
    11. @Override
    12. public void onEnable(){
    13. Bukkit.getLogger().info("");
    14. Bukkit.getLogger().warning("This is a warning");
    15.  
    16.  
    17. }
    18. @Override
    19. public void onDisable(){
    20.  
    21.  
    22. }
    23.  
    24. @SuppressWarnings("deprecation")
    25. public boolean OnCommand(CommandSender sender, Command cmd, String label, String[] args){
    26.  
    27. if (cmd.getName().equalsIgnoreCase("msg")){
    28. if (args[0].length() > 0){
    29. Player reciver = Bukkit.getPlayer(args[0]);
    30. if (args[1].length() > 0){
    31.  
    32. reciver.sendMessage(args[1]);
    33. sender.sendMessage("you sent the message:" + args[1]);
    34. }
    35. else {
    36. sender.sendMessage("You need to enter a message");
    37.  
    38. }
    39.  
    40.  
    41.  
    42. }
    43. else {
    44.  
    45. sender.sendMessage("You need to enter a playername");
    46.  
    47. }
    48. }
    49.  
    50.  
    51.  
    52.  
    53.  
    54.  
    55.  
    56.  
    57.  
    58. return false;
    59. }
    60.  
    61. }
    62.  
     
  2. Offline

    Unica

    -Change the string to a player.
    -Get the message that the player want's to send.
    -Check if the receiver is online.
    -Check if they have entered a receiver.
    -Check if they have entered a message.

    • Bukkit.getPlayer(String)
    • StringBuilder
    • if(thatPlayer != null)
    • if(args.lenght > 2), basically checks if the second argument is existing. So the person entered a receiver
    • if(args.length == 2), means they entered a receiver, but didn't put anything behind it, thus no message
     
  3. Offline

    Barinade

  4. Offline

    SleepyDog

    When i get the strings player format will it get an error if i check if they are online?
     
  5. Offline

    leon3001

    Actually, args.length == 2 means the message only consists out of one word. Argument one is the receiver's name, argument two is the message. :)
     
  6. Offline

    SleepyDog


    Could i be cheeky and ask that you implement this into my code? I am a n00b at java at this time. I am attempting to learn by looking at the formatting of other plugins and seeing how they work.

    Thankyou if you can!

    So i use that on the receiver string, and the args[2] on the message to send?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  7. Offline

    Barinade

    if (args.length > 1) {
    Player rec = getServer().getPlayer(args[0]);
    if (rec != null) {
    String msg = "";
    for (int i = 1; i < args.length; i++) {
    msg += args + " ";
    }
    rec.sendMessage(sender.getDisplayName() + ": " + msg);
    }
    } else {
    //no message given
    }

    Something along those lines. Written outside of IDE.
     
  8. Offline

    leon3001

    SleepyDog Yes, you would get the player using args[0] and then use a StringBuilder (for this use, a simple for-loop would be fine as well) to form the message with the following arguments. :)
     
  9. Offline

    Cheesepro

    SleepyDog
    I thought Essentials already included command /msg
     
  10. Offline

    SleepyDog

    I dont like their message arrangement, i want it to fit my server theme. And i want to block people from receiving messages.

    Thankyou! I will try it!

    That code has no errors but does nothing ingame.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  11. SleepyDog Dude, you can change the layout of ALL of essentials using en.message properties
     
  12. Offline

    leon3001

    Well, someone spoonfed you with wrong code. That's why spoonfeeding is literally the worst thing you can possibly do. Try looking at the loop again, maybe you can spot the mistake yourself …? ;)
     
  13. Offline

    Barinade

    Code:
    args[i]
    Sorry, bb code messed it up
     
  14. Offline

    Watto

    Barinade

    You're spoonfeeding this guy and he isn't really learning anything :s
    You could of atleast let him try and figure the error out himself..
     
    leon3001 likes this.
  15. Offline

    Konato_K

    Your command method is called OnCommand instead of onCommand
     
    teej107 and Skionz like this.
  16. Offline

    teej107

    Perfect example of why @Override is a great annotation.
     
    Konato_K likes this.
  17. Offline

    SleepyDog

    Thankyou!



    Thankyou! I am starting to avoid copy and paste. I'll remember next time!

    I learn from others code. Many find that 'speenFeeding' is a bad thing. I am not trying to become a 'perfect' coder. Just to be able to say i can edit and know now my plugins work on my server.



    Works fine now! thanks everyone!
     
Thread Status:
Not open for further replies.

Share This Page