How to ignore messages from commands?

Discussion in 'Plugin Development' started by kreashenz, Mar 28, 2013.

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

    kreashenz

    I want to know how to be able to ignore messages from commands, like /broadcast, /say, and other server broadcasting things. Would I need to check if the message equals something, or starts with something, because I don't know if that'd work, coz I haven't tried this before.. To be honest, is this actually easy, or am I just stupid, and can't think of better ways to do this. Thanks..
     
  2. Offline

    Nitnelave

    You need the playerCommandPreprocessingEvent, to check if it is the command you want to block or not, and to act consequently.
     
  3. Offline

    kreashenz

    How do you check the command, it only checks messages.. ( I haven't used this before, and I'm reading the javadoc now )
     
  4. Offline

    Nitnelave

    Well, check if the message starts with "/"+commandname
     
  5. Offline

    kreashenz

    Ahhh, but how would it catch the senders name, not the person who's trying to block the commands.. I am kinda new to this, so explain a little more vague.
     
  6. Offline

    Nitnelave

    event.getPlayer is the player who sent the message/command.
     
  7. Offline

    kreashenz

    Ahh, LOLOLOL, I'm stupid :D Ok I will finish the command and get back to you.

    Nitnelave Sorry, but I need an example one, I got lost on what I was doing while I was AFK..

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

    ZeusAllMighty11

    Code:
    @EventHandler
    public void onPreCommand(PlayerCommandPreprocessEvent e){
        Player sender = e.getPlayer();
        if(e.getMessage().startsWith("/spawn")){
            sender.sendMessage("There is no spawn! muahaha");
            sender.setHealth(2);
        }
    }
    
     
  9. Offline

    kreashenz

    @ZuesAllMighty11 Ahaha, Ok, but why set their health to 1 heart.. That's just mean :) Thanks ! If I add a if(e.getPlayer().isOp())e.setCancelled(true); will that cancel out command, or just that one?
     
  10. Offline

    x0pk1n

    Explain what you are trying to do, im confused?
     
  11. Offline

    kreashenz

    x0pk1n Basically if a player types the /onduty or /offduty command, they will/will not receive /report messages. I guess I coulda said this before, but my explaining skills are retarded, aren't they?
     
  12. Offline

    Nitnelave

    If you add that line, you will basically prevent op from using any command or talking to anyone...
     
  13. Offline

    x0pk1n

    kreashenz So what code have you got? As this isn't a request forum, it is the develpment forum.
     
  14. Offline

    GodzOfMadness

    x0pk1n If this was the request forum then there wouldn't be much of java talk at all
     
  15. Offline

    x0pk1n

    GodzOfMadness If this was a development forum, he would have show us what he has and is trying.
     
  16. Offline

    GodzOfMadness

    x0pk1n Well if you read the thread you would know that he didn't know how to cover a certain aspect so he came on the "development forum" for help
     
  17. Offline

    x0pk1n

    GodzOfMadness Then where is the code he has tried for this aspect?
     
  18. Offline

    GodzOfMadness

    x0pk1n
     
  19. Offline

    kreashenz

    x0pk1n GodzOfMadness I was trying, and I think I got it, but I can't test it, because.. Well, I don't know why its not working, but I can just type /duty and it doesn't actually do anything.. Here is my whole Duty ( /onduty | /offduty ) code, that I have made so far.
    Code:java
    1.  
    2. package me.kreashenz.minecraftrp.Commands;
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandExecutor;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    12.  
    13. import me.kreashenz.minecraftrp.MCRP;
    14. import me.kreashenz.minecraftrp.Utilities.Functions;
    15.  
    16. public class Duty implements Listener, CommandExecutor {
    17.  
    18. public MCRP plugin;
    19. public Duty(MCRP plugin){this.plugin=plugin;}
    20. public static Boolean onDuty = Boolean.valueOf(false);
    21.  
    22. @Override
    23. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    24. if(cmd.getName().equalsIgnoreCase("duty")){
    25. if(sender instanceof Player){
    26. if(sender.hasPermission("mcrp.duty")){
    27. if(args.length==0){
    28. if(Boolean.valueOf(false)){
    29. Boolean.valueOf(true);
    30. sender.sendMessage("§eYou are now §con-duty§e, and will recieve Reports.");
    31. Bukkit.broadcastMessage("§c" + sender.getName() + "§e is now §con-duty§e, and will be taking reports!");
    32. } else if(Boolean.valueOf(true)){
    33. Boolean.valueOf(false);
    34. sender.sendMessage("§eYou are now §coff-duty§6, and will not recieve Reports.");
    35. Bukkit.broadcastMessage("§c" + sender.getName() + "§e is now §coff-duty§e, and will be taking reports!");
    36. }
    37. }
    38. } else sender.sendMessage(Functions.noPerm());
    39. } else sender.sendMessage("§çYou must be player to use this command.");
    40. }
    41. return true;
    42. }
    43. @EventHandler
    44. public void onPreCommand(PlayerCommandPreprocessEvent e){
    45. if(e.getMessage().startsWith("/report")){
    46. if(Boolean.valueOf(true)){
    47. e.setMessage(null);
    48. } else if(Boolean.valueOf(false)){ e.setMessage(e.getMessage());}
    49. }
    50. }
    51. }
     
  20. Offline

    Nitnelave

    Boolean.valueof(true) is the exact same thing as simply true, only more confusing. So when you do if(Boolean.valueof(false)), this code will never be executed... I think you're missing something like if player.isOnDuty(), player.setOnDuty(true); etc...
    Did you register the command in your plugin.yml? And did you register the commandExecutor in your main onEnable ?
     
  21. Offline

    kreashenz

    Nitnelave Yes, and yes. I just get confused when using the if(Boolean.valueOf(true)){..
     
  22. Offline

    Nitnelave

    Well, if(true) is always executed. It's not really a test. I wouldn't be surprised if the compiler removed it.
     
  23. Offline

    kreashenz

    Nitnelave So basically what you're saying, ( or what I'm interpreting ) is that I should replace if(Boolean.valueOf(true)){ with if(true) ?
     
  24. Offline

    molenzwiebel

    Code:
    package me.kreashenz.minecraftrp.Commands;
     
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
     
    import me.kreashenz.minecraftrp.MCRP;
    import me.kreashenz.minecraftrp.Utilities.Functions;
     
    public class Duty implements Listener, CommandExecutor {
     
        public MCRP plugin;
        public Duty(MCRP plugin){this.plugin=plugin;}
        public List<String> onDuty = new List<String>();
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if(cmd.getName().equalsIgnoreCase("duty")){
                if(sender instanceof Player){
                    if(sender.hasPermission("mcrp.duty")){
                        if(args.length==0){
                            if(!onDuty.contains(sender.getName()){
                                onDuty.add(sender.getName());
                                sender.sendMessage("§eYou are now §con-duty§e, and will recieve Reports.");
                                Bukkit.broadcastMessage("§c" + sender.getName() + "§e is now §con-duty§e, and will be taking reports!");
                            } else {
                               onDuty.remove(sender.getName());
                                sender.sendMessage("§eYou are now §coff-duty§6, and will not recieve Reports.");
                                Bukkit.broadcastMessage("§c" + sender.getName() + "§e is now §coff-duty§e, and will not be taking reports!");
                            }
                        }
                    } else sender.sendMessage(Functions.noPerm());
                } else sender.sendMessage("§çYou must be player to use this command.");
            }
            return true;
        }
        @EventHandler
        public void onPreCommand(PlayerCommandPreprocessEvent e){
            if(e.getMessage().startsWith("/report")){
            for (Player p : Bukkit.getOnlinePlayers()) {
             if (p.isOp() && onDuty.contains(p.getName()) {
             p.sendMessage("[Report] "+e.getMessage().replace("/report", ""));
             }
            }
        }
        }
    }
    
    As always, not tested
     
  25. Offline

    kreashenz

    molenzwiebel Uhm, da hell is this? I got an error on public List.. Cannot instantiate the type List<String>
     
  26. Offline

    molenzwiebel

    kreashenz This is the right code, and it should be
    Code:
    public List<String> onDuty = new ArrayList<String>();
    
     
  27. Offline

    kreashenz

    molenzwiebel Ok, /duty turns you on/off but the messages aren't getting blocked.. I've haven't tried while Deopped, because I don't even think permissions are working.. How do I test if permissions are working?
     
  28. Offline

    molenzwiebel

    add e.setCancelled(true); to the preprocessevent after you make sure the message is /report
     
  29. Offline

    hockeygoalie5

    It looks like you're trying to take unnecessary steps here. You want a player's /report to only go to on duty OPs? Why don't you keep a list of on duty OPs and add/remove OPs to the list when they use /onduty or /offduty, then in the /report command listener just send the message to on duty OPs. Something like this for /report:
    Code:
    // Where onDuty is a List of on duty OPs, and message is the report.
    for(Player p : onDuty) {
        p.sendMessage(message);
    }
    
    That's it. It only on duty OPs will receive the message, and there's no need to take extra steps to make sure off duty OPs don't.
     
  30. Offline

    kreashenz

    hockeygoalie5 What would this onDuty list be.. Something like, List<Player> duty = onDuty.????
     
Thread Status:
Not open for further replies.

Share This Page