[URGENT]Have a message be sent to an admin

Discussion in 'Plugin Development' started by XgXXSnipz, Oct 12, 2014.

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

    XgXXSnipz

    So im coding a report plugin, and I need to be able to have a message added to my command and have the message be sent, so far i made it so you type in the player you wanna report but I also need to make it so you have to add a message that is less than 100 characters

    Code:java
    1. Player target = Bukkit.getServer().getPlayer(args[0]);
    2. if (target == null) {
    3. p.sendMessage(ChatColor.YELLOW + "[" + ChatColor.BLUE + "Harassment" + ChatColor.YELLOW + "]" + ChatColor.RED + "Error: could not find " + args[0] + "!");
    4. return true;
    5. }
    6.  
    7. p.sendMessage(ChatColor.YELLOW + "[" + ChatColor.BLUE + "Harrasment" + ChatColor.YELLOW + "]" + ChatColor.GREEN + "Sucess, You have reported " + ChatColor.RED + target.getName());
    8. target.playSound(target.getLocation(), Sound.BAT_DEATH, 1, 1);
    9. target.sendMessage(ChatColor.RED + sender.getName() + ChatColor.GREEN + "Just reported you for harrasing him/her");
    10.  


    What I have so far


    EDIT: I also need to make it so it will go to an admin, what I have so far:
    Code:java
    1. for(World w : getServer().getWorlds()){
    2. for(Player player: w.getPlayers()){
    3. if(p.hasPermission("admin.harrass")){
    4. Bukkit.broadcast(sender.getName() + ChatColor.AQUA + "Was harrased by" + ChatColor.RED + target.getName() + ChatColor.YELLOW + " Reason:" , "admin.harass");
    5. }


    I dont think the sender.getName() and the target.getName() will work when sending it to another player, I need to know how to get the person's name and the person that the player reported name then send it to an admin

    Also, how can I make it so the guy who got reported cant see the guy that reported him or have any certain interaction with that player

    Help!

    @GameCube, I hate to tahg you but... can you help me

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

    rbrick

    XgXXSnipz Please only bump once 24 hours. Also Bukkit#broadcast, it takes a String(the string to broadcast) and a String(the permission node). As for the message here is some pseudo code
    Code:
    for(int i = 1; i < args.length; i++) {
      // Code here...
    }
     
  3. Offline

    XgXXSnipz

    rbrick can you help me?

    teej107 The problem is trying to add a message when reporting someone /harrasment <player> <reason> then sending it to an admin

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 27, 2017
  4. Offline

    teej107

    Here


    You have the target:
    Code:java
    1. Player target = Bukkit.getServer().getPlayer(args[0]);

    You have the CommandSender in the onCommand() which could be a player.
    Also Bukkit.broadcast() automatically sends it to everybody (if with the specified permission). There is no need to loop through every player and send it. Players with the permission will get spammed as many times as there are players.

    As far as I see, you have all the information you need. What seems to be the problem?
     
    glasseater and pookeythekid like this.
  5. Offline

    pookeythekid

    XgXXSnipz Other than what teej107 said, I can't find anything wrong with your code.
    Have you tried it yet?

    Edit: Lol, you ninja'd me.
     
  6. Offline

    teej107

    XgXXSnipz Loops through all the args, except the first one since that is your <player> argument and concatenate the strings together.
     
  7. Offline

    pookeythekid

    What teej107 said. I'm going to do my best not to spoon feed you here...
    Code:java
    1. String message = "";
    2. for (int i = 0; i < args.length; i++) {
    3. if (i >= 0) {
    4. if (i != (args.length - 1) // Checks if it's at the final argument. This way you won't have a space at the end of your reason.
    5. message = message + args[i] + " ";
    6. else
    7. message = message + args[i];
    8. }
    9. }[/i][/i]

    Ignore the two
    s at the end.


    *sigh* The post wouldn't stop seeing my args indexes as italicization. At the end I meant to say "Ignore the two <insert those tags> at the end."

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

    rbrick


    Code:java
    1. for(int i = 1; i < args.length; i++) {
    2. // Code here...
    3. }

    :)
     
  9. Offline

    pookeythekid

    rbrick Lol I did see that, but he kept asking for an answer so I thought I'd restate it.
     
  10. XgXXSnipz This isn't urgent. Please don't mark it as such, it's quite rude to other people. :)

    pookeythekid Don't append Strings in a loop, use StringBuilder. It's more efficient. :)
     
    pookeythekid and Hawktasard like this.
  11. Offline

    pookeythekid

    AdamQpzm Ah, yes. I actually tend to use StringBuffer. I hear StringBuilder is much more efficient as far as speed, but my plugins don't handle such massive data that they must operate any faster than they already do.
     
    AdamQpzm likes this.
  12. Offline

    XgXXSnipz

    pookeythekid
    Code:java
    1. package me.XgXxSnipz.Harrasment;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Sound;
    6. import org.bukkit.World;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class Harrasment extends JavaPlugin implements Listener {
    14.  
    15. public void onEnable(){
    16. getLogger().info(ChatColor.GREEN + "Harrassment enabled");
    17. }
    18.  
    19. @SuppressWarnings({ "deprecation", "unused" })
    20. @Override
    21. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    22.  
    23. final Player p = (Player)sender;
    24.  
    25. if ((!(sender instanceof Player)))
    26. return true;
    27. {
    28.  
    29. if(cmd.getName().equalsIgnoreCase("harras"))
    30. if (args.length == 0) {
    31. p.sendMessage(ChatColor.YELLOW + "[" + ChatColor.BLUE + "Harrasment" + ChatColor.YELLOW + "]" + ChatColor.RED + "Error: You must specifiy a player and your reason for reporting him");
    32. return true;
    33.  
    34.  
    35. }
    36. Player target = Bukkit.getServer().getPlayer(args[0]);
    37. if (target == null) {
    38. p.sendMessage(ChatColor.YELLOW + "[" + ChatColor.BLUE + "Harassment" + ChatColor.YELLOW + "]" + ChatColor.RED + "Error: could not find " + args[0] + "!");
    39. return true;
    40. }
    41.  
    42. String message = "";
    43. for (int i = 0; i < args.length; i++) {
    44. if (i >= 0) {
    45. if (i != (args.length - 1)) // Checks if it's at the final argument. This way you won't have a space at the end of your reason.
    46. message = message + args[i] + " ";
    47. else
    48. message = message + args[i];
    49. }
    50. }
    51.  
    52. if (message == null) {
    53. p.sendMessage(ChatColor.YELLOW + "[" + ChatColor.BLUE + "Harassment" + ChatColor.YELLOW + "]" + ChatColor.RED + "Error: Must include a reason why!");
    54. return true;
    55. }
    56.  
    57. p.sendMessage(ChatColor.YELLOW + "[" + ChatColor.BLUE + "Harrasment" + ChatColor.YELLOW + "]" + ChatColor.GREEN + "Sucess, You have reported " + ChatColor.RED + target.getName());
    58. target.playSound(target.getLocation(), Sound.BAT_DEATH, 1, 1);
    59. target.sendMessage(ChatColor.RED + sender.getName() + ChatColor.GREEN + " Just reported you for harrasing him/her");
    60. for(World w : getServer().getWorlds()){
    61. for(Player player: w.getPlayers()){
    62. if(p.hasPermission("admin.harrass")){
    63. p.sendMessage(sender.getName() + ChatColor.AQUA + "Was harrased by" + ChatColor.RED + target.getName() + ChatColor.YELLOW + " Reason:" + ChatColor.RED + message);
    64. }
    65. }
    66. }
    67. return false;
    68. }
    69. }
    70. }
    71. [/i][/i]


    So this is what I have so far, for some reason I cant put a reason with it. when I test in game
     
  13. Offline

    pookeythekid

    XgXXSnipz Read what Adam and I were saying. You should use StringBuilder or StringBuffer, don't append to a regular String as I did in my example.
     
    glasseater likes this.
  14. Offline

    XgXXSnipz

    pookeythekid O OK ill look into that, thanks :D

    pookeythekid yea ima need more help on this
    Code:java
    1.  
    2. StringBuilder message = new StringBuilder("");
    3. for (String part : args) {
    4. if (!message.toString().equals(""))
    5. message.append(" ");
    6. message.append(part);
    7. }
    8.  
    9. if(message == null){
    10. p.sendMessage(ChatColor.RED + "You must specify a message");
    11. return true;
    12. }
    13. p.sendMessage(ChatColor.YELLOW + "[" + ChatColor.BLUE + "Harrasment" + ChatColor.YELLOW + "]" + ChatColor.GREEN + "Sucess, You have reported " + ChatColor.RED + target.getName());
    14. target.playSound(target.getLocation(), Sound.BAT_DEATH, 1, 1);
    15. target.sendMessage(ChatColor.RED + sender.getName() + ChatColor.GREEN + " Just reported you for harrasing him/her");
    16. for(World w : getServer().getWorlds()){
    17. for(Player player: w.getPlayers()){
    18. if(p.hasPermission("admin.harrass")){
    19. p.sendMessage(sender.getName() + ChatColor.AQUA + "Was harrased by" + ChatColor.RED + target.getName() + ChatColor.YELLOW + " Reason:" + ChatColor.RED + message);
    20. }
    21. }
    22. }
    23. return false;
    24. }
    25. }
    26. }


    plugin doesnt seen to care if I have a reason or not when i tell the admin i've been harassed

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

    tcvs

    XgXXSnipz the message will never be null because you have made a new StringBuilder when you defined it. to check if it is empty check the length of the message if it is less than 1 it is empty...
     
  16. Offline

    XgXXSnipz

    tcvs how do I check for that?? I tried using args[] but it doesnt work
     
  17. Offline

    tcvs

    XgXXSnipz Use args.length check if it equals 0.
     
  18. Offline

    XgXXSnipz

    tcvs
    Code:java
    1. if(message == args.length[0]
    I get an error on the args.length[0]
     
  19. Offline

    TheCodingCat

    its probably a N.P.E check if the args.length is equal to 1 before you go accessing it
     
  20. Offline

    pookeythekid

    XgXXSnipz I'm sorry... I almost have to laugh... It's not "if (message = args.length[0])". That's not how you check the length of "args". The way you check the length of the args is "if (args.length == 0)".
     
    ChipDev, VG.Developments and tcvs like this.
  21. Offline

    tcvs

    XgXXSnipz Ya that is a an error.@pookeythekid is correct.
     
  22. Offline

    XgXXSnipz

    tcvs pookeythekid but that makes no sense, im checking to see if the player included a reason... not to see if he put who harassed him... look at my code, youll see I use args.length for something else
     
  23. Offline

    tcvs

    XgXXSnipz you can still use args.length. So if you say have the command as /report [name] [reason] then check if the args length equal or greater than 2.
     
    pookeythekid likes this.
  24. Offline

    pookeythekid

    XgXXSnipz What Tcvs means is that you can still count how many arguments there are to check if they put a reason in. If the args count is at least two, then that means they put at least one word in the [reason] argument.
     
  25. Offline

    tcvs

  26. Offline

    pookeythekid

    tcvs No problem. :p
     
  27. XgXXSnipz You get the error since args.length doesn't return an array, it returns an int [number] is how to access an index in an array, so you cannot use it on the length. This is a pretty basic Java thing, and if you don't know it, I recommend you (re)learn the basics of Java either from the Oracle tutorials or a Java book. :)
     
  28. Offline

    XgXXSnipz

    AdamQpzm I wish I could just block you
     
  29. Offline

    drpk

    XgXXSnipz Just as many people wish they could block you...
     
  30. Offline

    FerusGrim

    XgXXSnipz
    Why are you being so hostile towards the people trying to help you? Why come on here in the first place, if you cannot accept the help that you're asking for? =/
     
Thread Status:
Not open for further replies.

Share This Page