Custom Message Command.

Discussion in 'Plugin Development' started by BeefyPlays, Jan 25, 2014.

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

    BeefyPlays

    I am wondering how you would edit the /msg command and add the /r command. I've been searching for a little while but I cant find it.
     
  2. Offline

    Freelix2000

    Could you be more specific about what you are trying to accomplish? Are you trying to configure Essentials, edit Essentials, or are you trying to make your own plugin with /msg and /r?
     
  3. Offline

    BeefyPlays

    I am trying to make my own /msg and /r
     
  4. Offline

    Freelix2000

    Okay. First, make a public HashMap<String, String> that will store data about who the player has last messaged. For the /msg command, the code inside your onCommand method will look something like this:
    Code:java
    1. if(cmd.getName().equalsIgnoreCase("msg"){
    2. Player p = Bukkit.getPlayer(sender.getName());
    3. if(args.length < 2){
    4. p.sendMessage(ChatColor.RED + "Too few arguments");
    5. p.sendMessage(ChatColor.RED + "/msg <player> <message>");
    6. return true;
    7. }
    8. if(Bukkit.getPlayer(args[0]) == null){
    9. p.sendMessage(ChatColor.RED + "That player is not online");
    10. return true;
    11. }
    12. String msg = "";
    13. for(String s : args){
    14. msg = msg + " " + s;
    15. }
    16. Bukkit.getPlayer(args[0]).sendMessage(p.getName() + " -> me: " + msg.replaceFirst(" " + args[0], "");
    17. p.sendMessage("me -> " + args[0] + ": " + msg.replaceFirst(" " + args[0], "");
    18. YOUR_HASHMAP_NAME.put(p.getName(), args[0]);
    19. YOUR_HASHAMP_NAME.put(args[0], p.getName());
    20. return true;
    21. }

    Next, for the /reply command, you will use the HashMap data created with the /msg command.
    Code:java
    1. if(cmd.getName().equalsIgnoreCase("r"){
    2. if(args.length < 1){
    3. p.sendMessage(ChatColor.RED + "Too few arguments.");
    4. p.sendMessage(ChatColor.RED + "/r <message>");
    5. return true;
    6. }
    7. if(!YOUR_HASHMAP_NAME.containsKey(p.getName()){
    8. p.sendMessage(ChatColor.RED + "You do not have anyone to reply to.");
    9. return true;
    10. }
    11. if(Bukkit.getPlayer(YOUR_HASHMAP_NAME.get(p.getName())) == null){
    12. p.sendMessage(ChatColor.RED + "That player is not online.");
    13. return true;
    14. }
    15. String msg = "";
    16. for(String s : args){
    17. msg = msg + " " + args;
    18. }
    19. Bukkit.getPlayer(YOUR_HASHMAP_NAME.get(p.getName())).sendMessage(p.getName() + " -> me:" + msg);
    20. p.sendMessage("me -> " + YOUR_HASHMAP_NAME.get(p.getName()) + ":" + msg);
    21. }

    Then you can probably figure out how to edit those messages. Although I just gave you all the code you need for this, please go over it, research loops and HashMaps, and try to understand it before just using it. =)
     
  5. Offline

    BeefyPlays

    I tried that but I got error's for GetPlayer and Put. Heres my code:
    Code:java
    1. package me.ItsBeefyYT.com;
    2.  
    3. import java.sql.Array;
    4. import java.util.ArrayList;
    5. import java.util.HashMap;
    6.  
    7. import net.minecraft.server.v1_7_R1.Block;
    8.  
    9. import org.apache.logging.log4j.core.config.plugins.Plugin;
    10. import org.bukkit.Bukkit;
    11. import org.bukkit.ChatColor;
    12. import org.bukkit.command.CommandSender;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.material.Command;
    16. import org.bukkit.plugin.java.JavaPlugin;
    17.  
    18. public class Whisper extends JavaPlugin implements Listener {
    19. public final HashMap<Player, ArrayList<Block>> hashmap = new HashMap<Player, ArrayList<Block>>();
    20. public Plugin plugin;
    21.  
    22. public void onDisable() {
    23. System.out.println("[MineVoltz Whisper] has been enabled");
    24.  
    25. }
    26.  
    27. public void onEnable () {
    28. System.out.println("[MineVoltz Whisper] has been enabled");
    29. }
    30.  
    31. public boolean onCommand(CommandSender sender, Command cmd,
    32. String commandLabel, String[] args) {
    33. Player player = (Player) sender;
    34. if(((CommandSender) cmd).getName().equalsIgnoreCase("msg")){
    35. Player p = Bukkit.getPlayer(sender.getName());
    36. if(args.length < 2){
    37. p.sendMessage(ChatColor.RED + "Too few arguments");
    38. p.sendMessage(ChatColor.RED + "/msg <player> <message>");
    39. return true;
    40. }
    41. if(Bukkit.getPlayer(args[0]) == null){
    42. p.sendMessage(ChatColor.RED + "That player is not online");
    43. return true;
    44. }
    45. String msg = "";
    46. for(String s : args){
    47. msg = msg + " " + s;
    48. }
    49. Bukkit.getPlayer(args[0]).sendMessage(p.getName() + " -> me: " + msg.replaceFirst(" " + args[0], ""));
    50. p.sendMessage("me -> " + args[0] + ": " + msg.replaceFirst(" " + args[0], ""));
    51. hashmap.put(p.getName(), args[0]);
    52. hashmap.put(args[0], p.getName());
    53. return true;
    54. }
    55. return false;
    56. }
    57.  
    58. public boolean onCommand1(CommandSender sender, Command cmd,
    59. String commandLabel, String[] args) {
    60. Player player = (Player) sender;
    61. if(((CommandSender) cmd).getName().equalsIgnoreCase("r"){
    62. if(args.length < 1){
    63. player.sendMessage(ChatColor.RED + "Too few arguments.");
    64. player.sendMessage(ChatColor.RED + "/r <message>");
    65. return true;
    66. }
    67. if(!hashmap.containsKey(player.getName())){
    68. player.sendMessage(ChatColor.RED + "You do not have anyone to reply to.");
    69. return true;
    70. }
    71. if(Bukkit.getPlayer(hashmap.get(player.getName())) == null){
    72. player.sendMessage(ChatColor.RED + "That player is not online.");
    73. return true;
    74. }
    75. String msg = "";
    76. for(String s : args){
    77. msg = msg + " " + args;
    78. }
    79. Bukkit.getPlayer(hashmap.get(player.getName())).sendMessage(p.getName() + " -> me:" + msg);
    80. player.sendMessage("me -> " + hashmap.get(player.getName()) + ":" + msg);
    81. }
    82. }
    83. }
    84.  
     
  6. Offline

    Freelix2000

    BeefyPlays
    ...Why is the HashMap an array list of blocks? It should be <String, String>. The first string is the player's name, then the next one is the last player they replied to. Change the HashMap to
    Code:java
    1. public final HashMap<String, String> hashmap = new HashMap<String, String>();
     
  7. Offline

    BeefyPlays


    The plugin is working but I cant message anyone
     
  8. Offline

    ItsLeandro

    Ehh if you can't message anyone than the plugin isn't working, lol.
     
    AoH_Ruthless likes this.
  9. Offline

    Th3Br1x

    BeefyPlays
    Code:java
    1. if(((CommandSender) cmd).getName().equalsIgnoreCase("msg")){ ... }


    What are you trying to do there? It seems you shoud learn a bit more about writing a plugin, or just read the wiki article about how to write an onCommand().
     
  10. Offline

    BeefyPlays


    That was making the command /msg
     
  11. Offline

    Th3Br1x

    Wrong. You try to cast cmd (of the type Command) to the type of a CommandSender.
    It's like converting a Player into an Item. Simply doesn't work.

    Try this and please read the wiki article: http://wiki.bukkit.org/Plugin_Tutorial
    Code:java
    1. if(cmd.getName().equalsIgnoreCase("msg")){ ... }
     
  12. Offline

    Freelix2000

    BeefyPlays
    Did you change the HashMap to <String, String> like I asked? The plugin should work even if you don't, but for the commands to do anything you need to change that. Just replace
    Code:java
    1. public final HashMap<Player, ArrayList<Block>> hashmap = new HashMap<Player, ArrayList<Block>>();

    with
    Code:java
    1. public final HashMap<String, String> hashmap = new HashMap<String, String>();

    and it should work fine.
     
  13. Offline

    BeefyPlays


    Yep I've did that but it does not work. Also I get no errors in Eclipse and its loads fine
     
  14. Offline

    Wizehh

    Watch this:
    Don't waste our time​
     
    Konkz likes this.
  15. Offline

    Freelix2000

    BeefyPlays
    Could you show me the plugin's code, and the error it creates in console if there is one?
     
  16. Offline

    BeefyPlays

    Code:java
    1. package me.DrGamerYT.com;
    2.  
    3. import java.sql.Array;
    4. import java.util.ArrayList;
    5. import java.util.HashMap;
    6.  
    7. import net.minecraft.server.v1_7_R1.Block;
    8.  
    9. import org.apache.logging.log4j.core.config.plugins.Plugin;
    10. import org.bukkit.Bukkit;
    11. import org.bukkit.ChatColor;
    12. import org.bukkit.command.CommandSender;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.material.Command;
    16. import org.bukkit.plugin.java.JavaPlugin;
    17.  
    18. public class Main extends JavaPlugin implements Listener {
    19. public final HashMap<String, String> hashmap = new HashMap<String, String>();
    20. public Plugin plugin;
    21.  
    22. public void onDisable() {
    23. System.out.println("[MineVoltz Whisper] has been enabled");
    24.  
    25. }
    26.  
    27. public void onEnable () {
    28. System.out.println("[MineVoltz Whisper] has been enabled");
    29. }
    30.  
    31. public boolean onCommand(CommandSender sender, Command cmd,
    32. String commandLabel, String[] args) {
    33. Player player = (Player) sender;
    34. if(((CommandSender) cmd).getName().equalsIgnoreCase("msg")){
    35. Player p = Bukkit.getPlayer(sender.getName());
    36. if(args.length < 2){
    37. p.sendMessage(ChatColor.RED + "Too few arguments");
    38. p.sendMessage(ChatColor.RED + "/msg <player> <message>");
    39. return true;
    40. }
    41. if(Bukkit.getPlayer(args[0]) == null){
    42. p.sendMessage(ChatColor.RED + "That player is not online");
    43. return true;
    44. }
    45. String msg = "";
    46. for(String s : args){
    47. msg = msg + " " + s;
    48. }
    49. Bukkit.getPlayer(args[0]).sendMessage(p.getName() + " -> me: " + msg.replaceFirst(" " + args[0], ""));
    50. p.sendMessage("me -> " + args[0] + ": " + msg.replaceFirst(" " + args[0], ""));
    51. hashmap.put(p.getName(), args[0]);
    52. hashmap.put(args[0], p.getName());
    53. return true;
    54. }
    55. return false;
    56. }
    57.  
    58. public boolean onCommand1(CommandSender sender, Command cmd,
    59. String commandLabel, String[] args) {
    60. Player player = (Player) sender;
    61. if(((CommandSender) cmd).getName().equalsIgnoreCase("r")){
    62. if(args.length < 1){
    63. player.sendMessage(ChatColor.RED + "Too few arguments.");
    64. player.sendMessage(ChatColor.RED + "/r <message>");
    65. return true;
    66. }
    67. if(!hashmap.containsKey(player.getName())){
    68. player.sendMessage(ChatColor.RED + "You do not have anyone to reply to.");
    69. return true;
    70. }
    71. if(Bukkit.getPlayer(hashmap.get(player.getName())) == null){
    72. player.sendMessage(ChatColor.RED + "That player is not online.");
    73. return true;
    74. }
    75. String msg = "";
    76. for(String s : args){
    77. msg = msg + " " + args;
    78. }
    79. Bukkit.getPlayer(hashmap.get(player.getName())).sendMessage(player.getName() + " -> me:" + msg);
    80. player.sendMessage("me -> " + hashmap.get(player.getName()) + ":" + msg);
    81. }
    82. return false;
    83. }
    84. }
    85.  
     
  17. Offline

    Freelix2000

    BeefyPlays
    And what is the problem with it? Does it throw an error when you use one of the commands?
     
  18. Offline

    BeefyPlays

  19. Offline

    Th3Br1x

  20. Offline

    BeefyPlays

    Ok here is my plugin.yml
    Code:
    name: Message
    version: V1.53
    main: me.DrGamerYT.com.Main
    author: Team Myste
    description: This is a test plugin
     
    commands:
        msg:
            usage: /<command>
            aliases: [message, sendmessage]
            description: Whisper to someone.
          r:
            usage: /<command>
            aliases: [message, reply]
            description: Reply to someone.
     
  21. Offline

    Th3Br1x

    BeefyPlays This doesn't solve your problem, but why both commands have the same alias?
     
  22. Offline

    BeefyPlays

    Oops sorry here is the new one
    Code:
    name: MineVoltz Test Plugin
    version: V1.53
    main: me.DrGamerYT.com.Main
    author: Team Myste
    description: This is a test plugin
     
    commands:
        msg:
            usage: /<command>
            aliases: [msg, whispher]
            description: Whisper to someone.
          r:
            usage: /<command>
            aliases: [r, reply]
            description: Reply to someone
     
  23. Offline

    Th3Br1x

    BeefyPlays Another question: What is the name of the JAR-Filf of this plugin? The error says that the plugin.yml of the "UHC 1.jar" is invalid.
     
  24. Offline

    BeefyPlays

    UHC 1 Also, I think its a problem with
    description: This is a test plugin

    commands:
    msg:
    usage: /<command>
    aliases: [msg, whispher]
    description: Whisper to someone.
    r:
    usage: /<command>
    aliases: [r, reply]
    description: Reply to someone
    NEW*** I Fixed the plugin.yml it was a spacing issue but when I type /msg or /r it just sends a message back saying /r or /msg
     
  25. Offline

    Th3Br1x

    BeefyPlays
    Code:java
    1. if(((CommandSender) cmd).getName().equalsIgnoreCase("msg")){ ... }

    this part is still wrong. you can not cast "CommandSender" to a Command.

    Why you still didn't corrected this issue? i already mentioned it before.
    Here is, how it should look like.

    And please read the wiki article, how to write a plugin, espacially how to add commands: http://wiki.bukkit.org/Plugin_Tutorial#Commands

    Code:java
    1. if(cmd.getName().equalsIgnoreCase("msg")){ ... }
     
  26. Offline

    BeefyPlays

    When i try that I get an error and the plugin wont work.
     
  27. Offline

    Th3Br1x

    BeefyPlays What error do you get?

    Edit: I see, whats wrong :)
    You just imported the wrong Command.
    Instead of org.bukkit.Material.Command you need
    org.bukkit.Command.Command (or similiar, don't know the exact import now, i'm online with my phone)
     
  28. Offline

    xMrPoi

    You can just use this to get what the command was:
    Code:java
    1. if(commandLabel.equalsIgnoreCase("msg"){}

    I don't understand why people do it other ways. Isn't that the whole point of that part of the onCommand method?
     
  29. Offline

    AoH_Ruthless

    BeefyPlays
    You have to say more than "it didn't work". You have to tell us what didn't work (what happened), provide a stacktrace if applicable, and maybe give us an updated code. Otherwise we can't help you. Along the lines of what Th3Br1x was saying, you cast Player p to the player's name. That's wrong. That's like casting an apple to a banana. A name is a string, cast the player to a player.
     
  30. Offline

    BeefyPlays

    Thank you all for helping! I've got the plugin working :)

    Now I have a problem.... How do I override the normal minecraft /msg command
    **NEW** Never mind I just used the @Override

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
Thread Status:
Not open for further replies.

Share This Page