Solved Need HELP making my first custom plugin for a server!!

Discussion in 'Plugin Development' started by Neymar11, Jul 16, 2014.

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

    Neymar11

    Hello, I am making my first official plugin.
    I have some trouble making a command to have multiple words, like /maincommand help or simmilar.
    When I test it in game it saids that the command does not exist.
    Here's the onCommand() Code:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command command,
    2. String label, String[] args) {
    3. Player player = (Player) sender;
    4. if (label.equalsIgnoreCase("modreq")){
    5. if(args.length==0) {
    6. //sender.sendMessage("modreq send <req>");
    7. return false;
    8. } else {
    9. if (args[0].equals("send")){
    10. String msg = "";
    11. for (int i = 0;i<args.length; i++)
    12. msg += args[i] + " ";
    13. msg = msg.trim();
    14. reqs.put(reqs.size()+1, msg);
    15. sender.sendMessage("Request sent");
    16. return true;
    17. }
    18. }
    19. }
    20. if (args[0].equals("list")) {
    21. for (int i = 1; i<=reqs.size(); i++)
    22. sender.sendMessage(i+" : " + reqs.get(i));
    23. }
    24. if (args[0].equals("clear")) {
    25. reqs.clear();
    26. sender.sendMessage("Cleared the Requests list!");
    27. }
    28. if (args[0].equals("delete")) {
    29. if (args.length <=1)
    30. {
    31. player.sendMessage(ChatColor.RED + "Usage: /modreq delete <ID>!");
    32. }
    33. else
    34. {
    35. for (int i = 1; i<=reqs.size(); i++)
    36. sender.sendMessage(i+" : " + reqs.remove(i));
    37. }
    38. }
    39. if (args[0].equals("respond"))
    40. {
    41. if (args.length <=1)
    42. {
    43. player.sendMessage(ChatColor.RED + "Usage: /modreq <ID> respond"); }
    44. else
    45. {
    46. Player target = (Bukkit.getServer().getPlayer(args[0]));
    47. target.sendMessage(target.getName());
    48. }
    49. }
    50.  
    51.  
    52. return true;
    53.  
    54.  
    55. }[/i]


    I have tried making the first command (send) to work, while I was doing that I just tried coping the part of that command to the others so thats how far I am now.. Any help would be appreciated! :D
     
  2. Offline

    FabeGabeMC

    Watch where you are finishing the if(label.equalsIgnoreCase("")) statement.
    You are finishing it before the args[0] statement.
    Ah and make sure to replace equals with equalsIgnoreCase.
    Hope this helps
    ~Gabe
     
  3. Offline

    Neymar11



    FabeGabeMC Thanks a lot, that fixed the current problem..
    Now I got a 2 more problems, and if you can help me then that would be great.

    One of the problems is when i use the command /modreq send <text>, that works just fine but, later when I type /modreq list, it also lists the "send" as it was a part of the message.
    Here is an example of the problem (the "0:null" is not from the list command): http://prntscr.com/43abe7
    The other is in the delete command. How do I read the user input? Like if he types /modreq delete ID <- How do I get that ID in the code?


    Could you maybe look into the code and tell me what is wrong, and thanks again for replying? :)
     
  4. Offline

    EcMiner

    In the for loop for(i = ... You need to make i = 1 by default cause if you start with 0 the result would be args[0], args[1], args[2] ... But args[0] is part of the command, in this case args[0] = send

    And for your second problem you should check if args[1] is an int, and if it is you need to make the string that was filled in as the ID argument an int, and then check if it's in the hashmap
     
  5. Offline

    Neymar11

    EcMiner, thanks for replying and also thanks for explaining/solving my first problem.




    Now about the second, args[1] is an int. Could you maybe explain that second part a little better, im not sure i understand!
     
  6. Well, if you want to remove a certain item from a List by its index, just attempt to parse the argument to an Int and remove it from the List if the integer is valid and the List contains such an index.

    Code:java
    1. try {
    2. int index = Integer.parseInt(args[1]);
    3. //remove the item from the List, you know the index! just remember to handle an out of array bounds value!
    4. } catch (NumberFormatException nfe) {
    5. sender.sendMessage(ChatColor.DARK_RED + "Not a valid integer!");
    6. }
     
  7. Offline

    Neymar11

    bruno.paschoalinoto Well im having a problem saving the counter/id to a list..
    So I would have to save the interger "i" in the following code, into a list and then later match it with the user input and delete that message?

    Or do you mean something else by your last reply?
    Code:java
    1. for (int i = 1;i<args.length; i++) {
    2. msg += args[I] + " ";[/I]
    3. [I] msg = msg.trim();[/I]
    4.  
    5. [I] }[/I]
     
  8. No. Integer.parseInt() turns a String into an int if applicable. I meant that, if you want to delete a certain message from the message list using /modreq delete [message], you'd have to:

    1. Try and parse the string.
    2a. Failed to parse? Warn user. Failure. End.
    2b. Parsed to an int correctly? Check if the message list's size is smaller or equal to the just-parsed integer, and if the integer is bigger or equal to one (1 <= i <= yourlist.size()). Basically, check if there is such a message in the list.
    3a. No such message? Warn user. Failure. End.
    3b. Message found? Remove it from the list. Success. End.
     
  9. Offline

    Neymar11

    bruno.paschoalinoto Ok so I have finished the delete command, but it doesnt work 100% as it is supposed to.
    Example: if I send 5 requests, and then try to delete the third. It does remove it but it also removes the 5th and I dont know why :(.
    And one more question, when I do delete a request it replaces it with "null" (screenshot below), so how do I make it to like move the following request up by 1 so it replaces the deleted request?
    http://prntscr.com/43tb1q

    Code of the delete command:
    Code:java
    1. if (args[0].equalsIgnoreCase("delete")) {
    2. int i = 0;
    3. int index = Integer.parseInt(args[1]);
    4. if (args.length <=1)
    5. {
    6. sender.sendMessage(ChatColor.RED + "Usage: /modreq delete <ID>!");
    7.  
    8. }
    9. try {
    10. if (1<=index && index <= reqs.size())
    11. {
    12. sender.sendMessage("Request under the following ID has been removed: " + reqs.remove(index));
    13. }
    14.  
    15. else
    16. {
    17. sender.sendMessage(ChatColor.DARK_RED + "Request under that ID does not exist!");
    18. }
    19.  
    20. }
    21. catch (NumberFormatException nfe) {
    22. sender.sendMessage(ChatColor.DARK_RED + "Not a valid integer!");
    23. }
    24. }
     
  10. Offline

    Neymar11

    bruno.paschoalinoto
    I guess fixed code is all of the code (sorry im new to bukkit forums)

    Code:java
    1. package me.neymar11.testPlugin;
    2.  
    3. import java.io.File;
    4. import java.io.FileInputStream;
    5. import java.io.FileOutputStream;
    6. import java.io.ObjectInputStream;
    7. import java.io.ObjectOutputStream;
    8. import java.util.HashMap;
    9.  
    10. import org.bukkit.Bukkit;
    11. import org.bukkit.ChatColor;
    12. import org.bukkit.command.Command;
    13. import org.bukkit.command.CommandSender;
    14. import org.bukkit.entity.Player;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17.  
    18. public class testPlugin extends JavaPlugin{
    19.  
    20. HashMap <Integer, String> reqs;
    21. @SuppressWarnings("unchecked")
    22. public void onEnable(){
    23.  
    24. File dir = getDataFolder();
    25.  
    26. if (!dir.exists())
    27. if (!dir.mkdir())
    28. System.out.println("Could not create directory for plugin: "
    29. + getDescription().getName());
    30.  
    31. reqs = (HashMap<Integer, String>) load(new File(getDataFolder(), "reqs.dat"));
    32. if (reqs == null)
    33. reqs = new HashMap<Integer, String>();
    34.  
    35. }
    36. public void onDisable() {
    37. save(reqs, new File(getDataFolder(),"reqs.dat"));
    38. }
    39.  
    40. public boolean onCommand(CommandSender sender, Command command,
    41. String label, String[] args) {
    42.  
    43. Player player = (Player) sender;
    44. if (label.equalsIgnoreCase("modreq")){
    45. if(args.length==0) {
    46. sender.sendMessage(ChatColor.BLUE + "modreq send <req>");
    47. return false;
    48. } else {
    49. if (args[0].equalsIgnoreCase("send")){
    50.  
    51.  
    52.  
    53. String msg = "";
    54. for (int i = 1;i<args.length; i++) {
    55. msg += args[i] + " ";
    56.  
    57.  
    58. }
    59. reqs.put(reqs.size()+1, msg);
    60. sender.sendMessage(ChatColor.YELLOW + "Request sent");
    61. return true;
    62. }
    63. }
    64.  
    65. if (args[0].equalsIgnoreCase("list")) {
    66. if (reqs.size() !=0)
    67. {
    68. for (int i = 1; i<=reqs.size(); i++)
    69. sender.sendMessage(i+" : " + ChatColor.YELLOW + reqs.get(i));
    70. }
    71. else if (reqs.size() ==0)
    72. {
    73. sender.sendMessage(ChatColor.YELLOW + "There are no Requests at the moment!" );
    74. }
    75. }
    76. if (args[0].equalsIgnoreCase("clear")) {
    77. reqs.clear();
    78. sender.sendMessage(ChatColor.DARK_GREEN + "Cleared the Requests list!");
    79. }
    80. if (args[0].equalsIgnoreCase("delete")) {
    81. int i = 0;
    82. int index = Integer.parseInt(args[1]);
    83. if (args.length <=1)
    84. {
    85. sender.sendMessage(ChatColor.RED + "Usage: /modreq delete <ID>!");
    86.  
    87. }
    88. try {
    89. if (1<=index && index <= reqs.size())
    90. {
    91. sender.sendMessage("Request under the following ID has been removed: " + reqs.remove(index));
    92. }
    93.  
    94. else
    95. {
    96. sender.sendMessage(ChatColor.DARK_RED + "Request under that ID does not exist!");
    97. }
    98.  
    99. }
    100. catch (NumberFormatException nfe) {
    101. sender.sendMessage(ChatColor.DARK_RED + "Not a valid integer!");
    102. }
    103. }
    104. }
    105.  
    106. /*if (args[1].equalsIgnoreCase("respond"))
    107.   {
    108.   if (args.length <=1)
    109.   {
    110.   player.sendMessage(ChatColor.RED + "Usage: /modreq <ID> respond");
    111.  
    112.   }
    113.   else
    114.   {
    115.   Player target = (Bukkit.getServer().getPlayer(args[0]));
    116.   target.sendMessage(target.getName());
    117.   }
    118.   }
    119.   */
    120.  
    121. return true;
    122.  
    123.  
    124. }
    125.  
    126. public void save (Object o, File f) {
    127. if (!f.exists())
    128. try {
    129. f.createNewFile();
    130.  
    131. oos.writeObject(o);
    132. oos.flush();
    133. oos.close();
    134.  
    135. } catch (Exception e) {
    136. e.printStackTrace();
    137. }
    138. }
    139.  
    140. public Object load(File f) {
    141. try{
    142. new FileInputStream(f));
    143.  
    144. Object result = ois.readObject();
    145. ois.close();
    146. return result;
    147. }catch(Exception e) {
    148. return null;
    149. }
    150.  
    151. }
    152. }
    153.  
    154.  
    155. [/i]
     
  11. Firstly, the Integer.parseInt() call should be inside the try block, because it is there where the exception may be thrown.
    Secondly, you have to subtract 1 from the argument because arrays start at 0.
    Finally, what is the class of the variable "reqs"?

    Store the requests in an ArrayList, mapping a value to an index in a HashMap is just like a worsened array. If you remove it from a HashMap, it doesn't reorganize itself. Look up the Javadocs on ArrayLists.

    TheSpherret I know reqs is a map, I'm telling him to use ArrayLists because they reorganize themselves upon removal of an item, unlike HashMaps.

    //edit: you're following me o_o

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

    _Filip

  13. Offline

    pluginsbyjason

    Okay, so you need to shift the elements of the array. Here's a piece of code that can do that for you:
    Code:java
    1. public void shiftElements(String[] messages, int index) {
    2. messages[index] = "";
    3. for (int x = index; x <= messages.length-1; x++) {
    4. if ( x == messages.length-1 )
    5. messages = "";
    6. else
    7. messages = messages[x+1];
    8. }
    9. }

    How it works is, let's say you have an array:
    Code:java
    1. String[] array = {"0","1","2","3","4"};

    and you want to remove index 3, you do:
    Code:java
    1. shiftElements(array, 3);

    And when you display the list of messages you can do:
    Code:java
    1. for( int d = 0; d < array.length; d++ ) {
    2. if ( array.length() == 0 )
    3. break;
    4. System.out.println(array[d]);
    5. }


    Hope that fixes your problem! Unfortunately, arrays have a fixed length (the length you initialize them with). To make life easier, I recommend you use ArrayLists to hold the list of messages, and to remove a message in an ArrayList all you need to do is:
    Code:java
    1. arraylistname.remove(index);

    Way easier..!
     
  14. Offline

    Neymar11

    pluginsbyjason, TheSpherret, Thanks for replying :D
    bruno.paschoalinoto
    Ok so I have changed it to ArraysList now and I have a few new problems.
    If it type /modreq send 1, then all numbers until 5, later I type /modreq list and it lists the numbers from 2 to 5, without the 1st number

    The updated code:
    Code:java
    1. package me.neymar11.testPlugin;
    2.  
    3. import java.io.File;
    4. import java.io.FileInputStream;
    5. import java.io.FileOutputStream;
    6. import java.io.ObjectInputStream;
    7. import java.io.ObjectOutputStream;
    8. import java.util.ArrayList;
    9.  
    10. import org.bukkit.Bukkit;
    11. import org.bukkit.ChatColor;
    12. import org.bukkit.command.Command;
    13. import org.bukkit.command.CommandSender;
    14. import org.bukkit.entity.Player;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17.  
    18. public class testPlugin extends JavaPlugin{
    19.  
    20.  
    21. ArrayList<String> reqs = new ArrayList<String>();
    22. public void onEnable(){
    23.  
    24. File dir = getDataFolder();
    25.  
    26. if (!dir.exists())
    27. if (!dir.mkdir())
    28. try {
    29. /*FileOutputStream fos = new FileOutputStream("reqs");
    30.   ObjectOutputStream oos = new ObjectOutputStream(fos);
    31.   oos.writeObject(reqs);
    32.   oos.close();*/
    33. }catch(Exception ex){
    34. ex.printStackTrace();
    35. }
    36. System.out.println("Could not create directory for plugin: "
    37. + getDescription().getName());
    38.  
    39. //reqs = (HashMap<Integer, String>) load(new File(getDataFolder(), "reqs.dat"));
    40. if (reqs == null)
    41. reqs = new ArrayList<String>();
    42.  
    43. }
    44. public void onDisable() {
    45. save(reqs, new File(getDataFolder(),"reqs.dat"));
    46. }
    47.  
    48. public boolean onCommand(CommandSender sender, Command command,
    49. String label, String[] args) {
    50.  
    51. Player player = (Player) sender;
    52. if (label.equalsIgnoreCase("modreq")){
    53. if(args.length==0) {
    54. sender.sendMessage(ChatColor.BLUE + "modreq send <req>");
    55. return false;
    56. } else {
    57. if (args[0].equalsIgnoreCase("send")){
    58.  
    59. String msg = "";
    60. for (int i = 1;i<args.length; i++) {
    61. msg += args[i] + " ";
    62.  
    63. }
    64. reqs.add(msg);
    65. sender.sendMessage(ChatColor.YELLOW + "Request sent");
    66. return true;
    67. }
    68. }
    69.  
    70. if (args[0].equalsIgnoreCase("list")) {
    71. if (reqs.size() !=0)
    72. {
    73. for (int i = 1; i<=reqs.size(); i++)
    74. sender.sendMessage(i+" : " + ChatColor.YELLOW + reqs.get(i));
    75. }
    76. else if (reqs.size() ==0)
    77. {
    78. sender.sendMessage(ChatColor.YELLOW + "There are no Requests at the moment!" );
    79. }
    80. }
    81. if (args[0].equalsIgnoreCase("clear")) {
    82. reqs.clear();
    83. sender.sendMessage(ChatColor.DARK_GREEN + "Cleared the Requests list!");
    84. }
    85. if (args[0].equalsIgnoreCase("delete")) {
    86. int i = 0;
    87.  
    88. if (args.length <=1)
    89. {
    90. sender.sendMessage(ChatColor.RED + "Usage: /modreq delete <ID>!");
    91.  
    92. }
    93. try {
    94. int index = Integer.parseInt(args[1]);
    95.  
    96. if (1<=index && index <= reqs.size())
    97. {
    98. sender.sendMessage("Request under the following ID has been removed: " + reqs.remove(index));
    99. }
    100.  
    101. else
    102. {
    103. sender.sendMessage(ChatColor.DARK_RED + "Request under that ID does not exist!");
    104. }
    105.  
    106. }
    107. catch (NumberFormatException nfe) {
    108. sender.sendMessage(ChatColor.DARK_RED + "Not a valid integer!");
    109. }
    110. }
    111. }
    112.  
    113. /*if (args[1].equalsIgnoreCase("respond"))
    114.   {
    115.   if (args.length <=1)
    116.   {
    117.   player.sendMessage(ChatColor.RED + "Usage: /modreq <ID> respond");
    118.  
    119.   }
    120.   else
    121.   {
    122.   Player target = (Bukkit.getServer().getPlayer(args[0]));
    123.   target.sendMessage(target.getName());
    124.   }
    125.   }
    126.   */
    127.  
    128. return true;
    129.  
    130.  
    131. }
    132.  
    133. public void save (Object o, File f) {
    134. if (!f.exists())
    135. try {
    136. f.createNewFile();
    137.  
    138. oos.writeObject(o);
    139. oos.flush();
    140. oos.close();
    141.  
    142. } catch (Exception e) {
    143. e.printStackTrace();
    144. }
    145. }
    146.  
    147. public Object load(File f) {
    148. try{
    149. new FileInputStream(f));
    150.  
    151. Object result = ois.readObject();
    152. ois.close();
    153. return result;
    154. }catch(Exception e) {
    155. return null;
    156. }
    157.  
    158. }
    159. }
    160.  
    161.  
    162. [/i]
     
  15. Offline

    pluginsbyjason

    I cleaned up this part of the code for you,
    Code:java
    1. if ( command.getName().equalsIgnoreCase("modreq") ) {
    2. if(args.length==0) {
    3. sender.sendMessage(ChatColor.BLUE + "modreq send <req>");
    4. } else if( args[0].equalsIgnoreCase("send") && args.length > 1 ) {
    5. String msg = "";
    6. for (int j = 1; j < args.length; j++)
    7. msg += args[j] + " ";
    8. reqs.add(msg);
    9. sender.sendMessage(ChatColor.YELLOW + "Request sent");
    10. }


    Also is "1" a message? Can you post some in-game output so I can see how your plugin works exactly.
     
  16. Offline

    Neymar11

    pluginsbyjason
    Ok so the send command should store the text/numbers which are placed after it. Which it does but as I said it kind of moves 1 up.
    http://imgur.com/HWrMMKV,BFtVgwR
    As you can see here, I sent 5 "requests". The requests are numbers 1 to 5. They are the message in the request.
    http://imgur.com/HWrMMKV,BFtVgwR#1
    In the 2nd photo you can see the /modreq list command, it lists them but number 1 is missing. So there are numbers from 2 -5.

    I used the numbers just for testing btw :3
     
  17. Offline

    pluginsbyjason

    There you go, problem fixed.
    Code:java
    1. if ( args[0].equalsIgnoreCase("list") ) {
    2. if (reqs.size() > 0) {
    3. for (int n = 0; n < reqs.size(); n++)
    4. sender.sendMessage( n + " : " + ChatColor.YELLOW + reqs.get(n) );
    5. } else {
    6. sender.sendMessage(ChatColor.YELLOW + "There are no Requests at the moment!" );
    7. }
    8. }
    9.  
     
  18. Offline

    Neymar11

    pluginsbyjason Thanks a lot :D
    Although, 1 more question! :rolleyes:
    So I would like to add a respond command, could you maybe explain to me how I could get the username of the "sender", the one who issued this command: /modreq send.
    Later, would I have to make another arrayList so I can send the reply.. That means I want a way to notify them about the reply. So maybe the 2nd command would be /modreq info, where it lists the replies?

    pluginsbyjason TheSpherret bruno.paschoalinoto EcMiner FabeGabeMC
    Well I have finished the plugin thanks to you guys! :D
    It seems to work, but if one of you could just look at the code and maybe tell me if I can improve it in any way. :)

    Code:java
    1. ArrayList<String> reqs = new ArrayList<String>();
    2. ArrayList<String> reqsname = new ArrayList<String>();
    3. String respond = "";
    4. String username = "";
    5. String name = "";
    6. int questionCounter = 0;
    7. public void onEnable(){
    8.  
    9. File dir = getDataFolder();
    10.  
    11. if (!dir.exists())
    12. if (!dir.mkdir())
    13. try {
    14. /*FileOutputStream fos = new FileOutputStream("reqs");
    15. ObjectOutputStream oos = new ObjectOutputStream(fos);
    16. oos.writeObject(reqs);
    17. oos.close();*/
    18. }catch(Exception ex){
    19. ex.printStackTrace();
    20. }
    21. System.out.println("Could not create directory for plugin: "
    22. + getDescription().getName());
    23.  
    24. if (reqs == null)
    25. reqs = new ArrayList<String>();
    26.  
    27. }
    28. public void onDisable() {
    29. save(reqs, new File(getDataFolder(),"reqs.dat"));
    30. }
    31.  
    32. public boolean onCommand(CommandSender sender, Command command,
    33. String label, String[] args) {
    34.  
    35.  
    36. if (label.equalsIgnoreCase("modreq")){
    37. if(args.length==0) {
    38. sender.sendMessage(ChatColor.BLUE + "modreq send <req>");
    39. return false;
    40. } else if (args[0].equalsIgnoreCase("send") && args.length > 1) {
    41.  
    42. String msg = "";
    43. name += sender.getName() + " ";
    44. for (int i = 1;i<args.length; i++) {
    45. msg += args[I] + " ";[/I]
    46.  
    47.  
    48.  
    49. [I]}[/I]
    50. [I]reqs.add(msg);[/I]
    51. [I]reqsname.add(name);[/I]
    52. [I]sender.sendMessage(ChatColor.GREEN + "Request sent");[/I]
    53. [I]return true;[/I]
    54.  
    55. [I]}[/I]
    56.  
    57. [I]if (args[0].equalsIgnoreCase("list")) {[/I]
    58. [I]if (reqs.size() > 0)[/I]
    59. [I]{[/I]
    60. [I]for (int n = 0; n < reqs.size(); n++)[/I]
    61. [I]sender.sendMessage(ChatColor.RED + "[" + ChatColor.GOLD + n +[/I]
    62.  
    63. [I]ChatColor.RED + "]" + " " + ChatColor.GRAY + reqs.get(n) +[/I]
    64. [I]ChatColor.GOLD + " \nBy: " + " " + reqsname.get(n));[/I]
    65.  
    66. [I]}[/I]
    67. [I]else {[/I]
    68. [I]sender.sendMessage(ChatColor.RED + "There are no Requests at the moment!" );[/I]
    69. [I]}[/I]
    70. [I]}[/I]
    71. [I]if (args[0].equalsIgnoreCase("clear")) {[/I]
    72. [I]reqs.clear();[/I]
    73. [I]reqsname.clear();[/I]
    74. [I]sender.sendMessage(ChatColor.DARK_GREEN + "Cleared the Requests list!");[/I]
    75. [I]}[/I]
    76. [I]if (args[0].equalsIgnoreCase("delete")) {[/I]
    77.  
    78. [I]if (args.length <=1)[/I]
    79. [I]{[/I]
    80. [I]sender.sendMessage(ChatColor.RED + "Usage: /modreq delete <ID>!");[/I]
    81.  
    82. [I]}[/I]
    83. [I]try {[/I]
    84. [I]int index = Integer.parseInt(args[1]);[/I]
    85. [I]//remove the item from the List, you know the index! just remember to handle an out of array bounds value![/I]
    86. [I]if (0<=index && index <= reqs.size())[/I]
    87. [I]{[/I]
    88. [I]sender.sendMessage(ChatColor.DARK_AQUA + "This Request has been removed: "+[/I]
    89. [I]ChatColor.WHITE + reqs.remove(index) + reqsname.remove(index));[/I]
    90. [I]}[/I]
    91.  
    92. [I]else[/I]
    93. [I]{[/I]
    94. [I]sender.sendMessage(ChatColor.DARK_RED + "Request under that ID does not exist!");[/I]
    95. [I]}[/I]
    96.  
    97. [I]}[/I]
    98. [I]catch (NumberFormatException nfe) {[/I]
    99. [I]sender.sendMessage(ChatColor.DARK_RED + "Not a valid integer!");[/I]
    100. [I]}[/I]
    101. [I]}[/I]
    102. [I]}[/I]
    103.  
    104. [I]if(args.length==0) {[/I]
    105. [I]sender.sendMessage(ChatColor.RED + "Usage: " + ChatColor.YELLOW + "/modreq respond <USERNAME> <Message>" );[/I]
    106. [I]return false;[/I]
    107. [I]}[/I]
    108.  
    109.  
    110. [I]else if (args[0].equalsIgnoreCase("respond") && args.length > 1)[/I]
    111. [I]{[/I]
    112.  
    113. [I]if ( name == username)[/I]
    114. [I]{[/I]
    115. [I]questionCounter++;[/I]
    116. [I]}[/I]
    117. [I]}[/I]
    118. [I]for (int i = 1;i<args.length; i++) {[/I]
    119. [I]if (questionCounter >= 1)[/I]
    120. [I]{[/I]
    121. [I]respond += args[2] + " ";[/I]
    122. [I]username = args[1];[/I]
    123. [I]}[/I]
    124. [I]else if (questionCounter < 1)[/I]
    125. [I]{[/I]
    126. [I]respond += args[2] + " ";[/I]
    127. [I]username = args[1] + " Times asked: " + questionCounter;[/I]
    128.  
    129. [I]}[/I]
    130.  
    131.  
    132. [I]sender.sendMessage(ChatColor.GREEN + "Response sent!");[/I]
    133. [I]return true;[/I]
    134. [I]}[/I]
    135.  
    136.  
    137. [I]if (args[0].equalsIgnoreCase("read")) {[/I]
    138.  
    139. [I]if (args.length==0)[/I]
    140. [I]{[/I]
    141. [I]sender.sendMessage(ChatColor.RED + "Usage: /modreq read");[/I]
    142. [I]}[/I]
    143. [I]String name = sender.getName();[/I]
    144.  
    145. [I]if ( name == username);[/I]
    146. [I]{[/I]
    147. [I]if (respond != "")[/I]
    148. [I]{[/I]
    149. [I]sender.sendMessage(ChatColor.GOLD + "Answer: " + ChatColor.WHITE + respond);[/I]
    150. [I]}[/I]
    151. [I]else[/I]
    152. [I]{[/I]
    153. [I]sender.sendMessage(ChatColor.GOLD + "You have not asked any questions yet!");[/I]
    154. [I]}[/I]
    155. [I]}[/I]
    156.  
    157.  
    158.  
    159.  
    160. [I]}[/I]
    161. [I]respond = "";[/I]
    162. [I]username = "";[/I]
    163. [I]return true;[/I]
    164.  
    165.  
    166. [I]}[/I]


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  19. Alrighty then, now add the [SOLVED] tag to your post's title so people know it's been solved.
     
  20. Offline

    pluginsbyjason

    Repost the code on pastebin and post the link. This forum has some issues with the tags. Once you do that I'll take a look and try to give pointers for improvement.



    To get the username of the sender, in your onCommand() method you do:
    Code:java
    1. if ( sender instanceof Player ) {
    2. Player player = (Player) sender;
    3. player.getName();
    4. }
     
  21. Offline

    Neymar11

    pluginsbyjason
    Thanks a lot! Here is the link: http://pastebin.com/xYJKtntL
    Ow I think I have done the respond command, but I did not test it yet, and please can you take a closer look in this part..
    Btw I am not sure what I tried to do with questionCounter, it had something to do with how many requests players can send!
     
Thread Status:
Not open for further replies.

Share This Page