Muting chat with a command

Discussion in 'Plugin Development' started by BJCxMC, Jul 2, 2014.

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

    BJCxMC

    This may seem very easy to most of you but I just cant figure it out. I am trying to have a command which obviously uses an event and I can't seem to figure it out with my little understanding of coding. This may seem like a plugin request, but its not, I just need some help with cancelling chat when someone speaks when they are NOT op and do not have a bypass permission. Any help is grealty appreciated.
     
  2. Listen to AsyncPlayerChatEvent.
    Code:
    if (!event.getPlayer().isOp() && !event.getPlayer().hasPermission(bypassPermission)) event.setCancelled(true);
    Optional: if (isMuted && <therest>)
     
  3. Offline

    BJCxMC

    How would I make this work with a command to toggle whether it is cancelled or not?
     
  4. Offline

    Flamedek

    BJCxMC Have a public boolean that stands for if chat is cancelled or not. You could change that boolean with the command and just check for that boolean in the event aswell
     
  5. Offline

    BJCxMC

    I don't want to sound like an idiot, but, I need help haha. This is exactly why I came over to Bukkit, I don't know how haha I created the public boolean by doing "public boolean chat = true;" but I'm not too sure where to go after that and how to use it in the AsyncPlayerChatEvent
     
  6. Offline

    fefe2008

    BJCxMC I usually make a toggle by adding something to an ArrayList on a command / removing sth on a command. To get if its enabled/disabled i check if the ArrayList contains what I added before / doesnt contain what i added before. Not the best way but it works pretty well.
     
  7. Offline

    Flamedek

    BJCxMC I would suggest watching some java tutorials and doing some practice coding. Jumping right into making a plugin might face you with many difficulties.

    Anyway you will need to use the if statement. An if statement checks if whatever you put in it is true, and if it is then executes the code inside of it, or if it's false skip it. Checking a boolean is easy, you could simply do if(chat) { code here}. That means if the boolean 'chat' is true, then do this.
    An exclemation mark means the negative. So 'if(!chat) { return; }' would make it stop the method if chat is false.

    Just put that last line infront of everything inside your event listener and it will do nothing at all if chat is false

    Edit: btw assuming you will just use one class for a small plugin you can remove the public keyword
     
  8. Offline

    Heirteir

    BJCxMC
    In the class with your commands just create a boolean toggle example
    Code:java
    1. public boolean yourboolean = true;
    2.  
    3. public void onEnable(){
    4. Bukkit.getPluginManager().registerEvents(this, this);
    5. }
    6.  
    7. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    8. if (cmd.getName().equalsIgnoreCase("togglechat"){
    9. if (yourboolean)
    10. yourboolean = false;
    11. else
    12. yourboolean = true;
    13. }
    14. return true;
    15. }
    16.  
    17. @EventHandler
    18. public void onChat(AsyncPlayerChatEvent e){
    19. if (!chat && !e.getPlayer().isOp()){
    20. e.setCancelled(true);
    21. e.getPlayer.sendMessage(ChatColor.RED + "Chat is disabled!");
    22. }
    23. }


    Sorry if I misspelled anything I didn't use eclipse :p
     
  9. Offline

    BJCxMC

    This is probably so wrong but this is what I have done by myself, It's probably no where near correct, any help you could give from this would mean a ton.
    Code:java
    1. package com.MrBJC412.Basic;
    2.  
    3. import org.bukkit.command.Command;
    4. import org.bukkit.command.CommandExecutor;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.player.AsyncPlayerChatEvent;
    9.  
    10. public class Mutechat implements CommandExecutor {
    11. Main plugin;
    12.  
    13. public Mutechat(Main passedPlugin)
    14. {
    15. this.plugin = passedPlugin;
    16. }
    17.  
    18. public boolean chat = true;
    19.  
    20. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String [] args) {
    21. Player p = (Player) sender;
    22. if(cmd.getName().equalsIgnoreCase("mutechat")) {
    23. if(p.hasPermission("Basic.mutechat")) {
    24. chat = false;
    25. } else if(p.hasPermission("Basic.bypass")) {
    26. chat = true;
    27. }
    28. }
    29. return chat;
    30. }
    31.  
    32. @EventHandler
    33. public void onChat(AsyncPlayerChatEvent e) {
    34. if(chat = false) {
    35. e.setCancelled(true);
    36. } else {
    37. if(chat = true) {
    38. e.setCancelled(false);
    39. }
    40. }
    41. }
    42.  
    43. }
    44.  


    That is kind of what I did, take a look at my code I just posted?

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

    Heirteir

    BJCxMC
    it seems you forgot implements Listener at the top of your code and it's only my guess you didn't register the events in your main with Bukkit.getPluginManager.registersEvent(new Mutechat(this), this);
     
  11. Offline

    BJCxMC


    Code:java
    1.  
    2. @Override
    3. public void onEnable()
    4. {
    5. Bukkit.getPluginManager.registersEvent(new Mutechat(this), this);
    6. this.getCommand("admin").setExecutor(new Admin(this));
    7. this.getCommand("heal").setExecutor(new Heal(this));
    8. this.getCommand("feed").setExecutor(new Feed(this));
    9. this.getCommand("help").setExecutor(new Help(this));
    10. this.getCommand("broadcast").setExecutor(new Broadcast(this));
    11. this.getCommand("clearchat").setExecutor(new ClearChat(this));
    12. this.getCommand("creative").setExecutor(new Creative(this));
    13. this.getCommand("survival").setExecutor(new Survival(this));
    14. this.getCommand("fly").setExecutor(new Fly(this));
    15. this.getCommand("kick").setExecutor(new Kick(this));
    16. this.getCommand("day").setExecutor(new Day(this));
    17. this.getCommand("night").setExecutor(new Night(this));
    18. this.getCommand("report").setExecutor(new Report(this));
    19. this.getCommand("warn").setExecutor(new Warn(this));
    20. }


    My Main class, Im getting an error on the getPluginManager part, it says it cannot be resolved or is not a field. Any suggestions?
     
  12. Offline

    Flamedek

    BJCxMC getPluginManager is a method of the server. use getServer().getPluginManager()...

    Also you can remove all those this-es, you are inside your class, and thus have access to those methods. The extra 'this' call realy is useless
     
  13. Offline

    BJCxMC

    Im getting an error os the registersEvent part. Any ideas?
    Code:java
    1. Bukkit.getServer().getPluginManager().registersEvent(new Mutechat(this), this);
     
  14. Offline

    Heirteir

    Flamedek
    Not true it's that he is mis spelling registerEvents (My fault :p)
    BJCxMC
    registerEvents
     
  15. Offline

    BJCxMC

    Great! That fixed it, but now back to my code, I posted it already and have changed nothing since, take a look at it?
     
  16. Offline

    L33m4n123

    Check your if statements. you assign a value in there even though you have to check for a value.
    second
    why passing the main instance into your listener class yet you don't use it? thats redundant so get rid of it
     
  17. Offline

    BJCxMC

    Im linking to to my main class because of the command to either toggle the chat to be muted or to enable talking again and whats wrong with the if statements? Im not understanding, like I said im fairly new, I just need some guidance here because I can't figure it out
     
  18. Offline

    Drkmaster83

    Code:
    if(chat = false) {
        e.setCancelled(true);
    } else {
        if(chat = true) {
            e.setCancelled(false);
        }
    }
    
    ^ Here's your problem. In Java (which, if you're constructing a plugin, you should have at least a basic understanding of), there are things called relational operators. Even though, on Oracle's site, the operator that you should be using '==', is called an equality operator, it functions the same more or less as a much more specific relational operator. Your problem is that you are using an assignment operator ('=') in an if-statement. Instead of using an if-statement for a boolean return value, you are assigning something.

    Edit: Also, what L33m4n123 was referring to is your construction of the Listener object. You're passing in the plugin's instance using 'this' when you don't use the plugin instance in the Listener object. This is object-oriented programming for Java, which is more or less an advanced programming concept.
     
  19. Offline

    BJCxMC

    So, If I'm understanding right, get rid of the if statements and use (chat == true) { ? Im so sorry, I just want this to work, so I will say it again haha what like actual part should I change in my code if that isn't asking for too much
     
  20. Offline

    Drkmaster83

    Well, what I'm meaning is that you have confused the two operators.
    This:
    Code:
    if(chat = false) {
        e.setCancelled(true);
    } else {
        if(chat = true) {
            e.setCancelled(false);
        }
    }
    
    Should be changed to this:
    Code:
    if(!chat) {
        e.setCancelled(true);
        //Perhaps send the player a message saying "You can't speak!"?
    } else {
        //if(chat) { //Redundant if statement, remove it, because booleans are one of two things, and we just checked for half of them above.
            e.setCancelled(false); //Be careful, this will allow people to bypass being muted by other plugins
        //}
    }
    
    Edit: Also, it's considered good practice to compare booleans without the use of the 'true' and 'false' values - use the not operator to check if something's false, otherwise use the variable name of the boolean value you're comparing for the true value.
     
  21. Offline

    mmiillkkaa

    Drkmaster83 There is no need for an if statement in this case.
    Code:java
    1. e.setCancelled(!chat);
    This is a better replacement.
     
  22. Offline

    Drkmaster83

    While this is true, like I said, that could also mess with the muting system of other plugins, such as Essentials. I also believe that having the if statement can help with understanding the logic, as this person seems to be relatively new at Java programming.
     
  23. Offline

    mmiillkkaa

    Drkmaster83 If the person is relatively new, it should be better to explain that
    Code:java
    1. if(code == false)...
    Is a bad practice.
     
  24. Offline

    Drkmaster83

    In this case, however, nothing should really need to be done if the 'chat' boolean is true, so it's either he sets the event's cancellation to false or he can return, which is a good practice.
     
  25. Offline

    mmiillkkaa

    Drkmaster83 I don't think you know what I mean. Comparing a boolean to 'true' or 'false' is bad practice.
     
  26. Offline

    Drkmaster83

    Then do you mean that if(boolean) or if(!boolean) is a better practice?
     
  27. Offline

    mmiillkkaa

    Yes.
     
  28. Offline

    Drkmaster83

    I'll edit my code, then. xD
     
Thread Status:
Not open for further replies.

Share This Page