PrisonEssentials

Discussion in 'Plugin Development' started by HazePvPHD, Dec 16, 2014.

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

    HazePvPHD

    Hi, so I am currently working on a plugin and I am stuck. I have asked all around I have tried everything. But I cannot find a way to do it. I would like to know how to make a class called mutechat. It is toggleable and only people with pe.admin can use it and talk whilist it is enabled. I know it has something to do with:

    public void MuteChat(AsyncPlayerChatEvent e) {
    }

    But I cannot figure out how to make it toggleable. I need it to implement the following messages:
    - Chat muted
    - Chat unmuted
    - NoChatDuringMute

    Please help me or teach me or something! Thanks !
     
    ChipDev and Funergy like this.
  2. Offline

    Funergy

    For the chat muted/unmuted use a boolean

    public boolean chatMuted = true; // they cant chat now if its false they can.

    now in your onCommand() add
    if(chatMuted){
    chatMuted = false;
    }else{
    chatMuted = true;
    }
    this will toggle it.

    then in the AsyncPlayerChatEvent
    use a check if the chatMuted boolean equals true if yes cancel the event
     
  3. Offline

    Feindbild

    To toggle a boolean you can just do
    Code:
    chatMuted=!chatMuted;
     
    leon3001 likes this.
  4. Offline

    HazePvPHD

    This really really helps! But how would I add a message so when they try to talk they get that message and there message doesn't go through

    And how do I make it check if the chatMuted = true/false; in the AsyncPlayerChatEvent?

    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Dec 16, 2014
    Feindbild likes this.
  5. Offline

    Funergy

    @HazePvPHD
    if(chatMuted){

    }else{
    //Chat is not muted
    }
    and its really not hard to send a message
    get the player object and you'll see
     
  6. Offline

    HazePvPHD

    Is this the correct way of doing it?
    Code:
        public void MutedChat(AsyncPlayerChatEvent e) {
            if(chatMuted = true) {
                e.setCancelled(true);
            }
        }
     
  7. Offline

    Funergy

    @HazePvPHD Yeah but add the @EventHandler tag and you can delete the '= true'
    it just works with this 'if(chatMuted){}'

    @Feindbild You can do that but he probably wants a message
    'Chat has been enabled' or disabled

    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Dec 16, 2014
    ChipDev likes this.
  8. Offline

    ChipDev

    Code:
    on Chat{
        if(chat) {
             //chat enabled
       }else{
            //chat disabled
       }
    }
    Peusdo @HazePvPHD
     
    Last edited: Dec 16, 2014
  9. Offline

    Feindbild

    Carefull, there is a huge difference between = and ==
    This is an easy beginner mistake to make, but not hard to remember once you get the hang of it.
    Single = means you assign a value, == means you're comparing to values.
     
  10. Offline

    Funergy

  11. Offline

    ChipDev

    Ok :p Meh, I like to help :3
    ^ Even though I must of not xD
     
    Funergy likes this.
  12. Offline

    HazePvPHD

    Ok thanks for all your replys and support !! @ChipDev @Funergy

    I would just like to leave with what I have got that works. This supports being able to mute your own chat and the global chat!
    Code:
    public class MuteChat implements Listener, CommandExecutor {
        List<String> toggled;
        PrisonEssentials plugin;
        public MuteChat(PrisonEssentials instance) {
            plugin = instance;
            chatMuted = false;
            toggled = new ArrayList<String>();
        }
        public static boolean chatMuted; //They cant chat now if its false they can.
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
                if(!(sender instanceof Player)) {
                    sender.sendMessage(ChatColor.RED + "You must be a player to use this command!");
                    return false;
                }
                Player player = (Player) sender;
               
                if(args.length == 0) {
                    if(!player.hasPermission("pe.mutechat")) { //!player.hasPermission = Does NOT have permission
                        plugin.msg(player, plugin.getConfig().getString("Messages.NoPerms"));
                        return true;
                    }
                    //Player DOES has permission
                    if(toggled == null) {
                        toggled = new ArrayList<String>();
                        toggled.add(player.getName());
                        plugin.msg(player, plugin.getConfig().getString("Mute.PrivateMuteOn"));
                        return true;
                    }
                    if(toggled.contains(player.getName())) {
                        toggled.remove(player.getName());
                        plugin.msg(player, plugin.getConfig().getString("Mute.PrivateMuteOff"));
                        return true;
                    } else {
                        toggled.add(player.getName());
                        plugin.msg(player, plugin.getConfig().getString("Mute.PrivateMuteOn"));
                        return true;
                    }
                }   
                else if(args.length > 0 && args[0].equalsIgnoreCase("all")) {
                    if(!player.hasPermission("pe.chat")) { //!player.hasPermission = Does NOT have permission
                        plugin.msg(player, plugin.getConfig().getString("Messages.NoPerms"));
                        return true;
                    }
                    //Player HAS permission.
                    if(chatMuted) {
                        chatMuted = false;
                        plugin.bcast(plugin.getConfig().getString("Mute.MuteOff"), player.getName());
                    } else {
                        chatMuted = true;
                        plugin.bcast(plugin.getConfig().getString("Mute.MuteOn"), player.getName());
                    }
                    return true;
                } else {
                    plugin.msg(player, "&cIncorrect usage! Do /mutechat <all>");
                }
            return true;
        }
        @EventHandler
        public void MutedChat(AsyncPlayerChatEvent e) {
            Player player = e.getPlayer();
            if(chatMuted) {
                if(player.hasPermission("pe.chat")) {
                    return;
                }
                plugin.msg(player, plugin.getConfig().getString("Mute.NoTalk"));
                e.setCancelled(true);
                return;
            }
            else {
               
                if(e.getRecipients() == null || e.getRecipients().isEmpty()) {
                    return;
                }
                if(toggled == null || toggled.isEmpty()) {
                    return;
                }       
                List<Player> off = new ArrayList<Player>();
                for(Player listening : e.getRecipients()) {
                    if(toggled.contains(listening.getName())) {
                        off.add(listening);
                    }
                }
                if(off.isEmpty()) {
                    return;
                }
                e.getRecipients().removeAll(off);
            }
        }
    }
     
    ChipDev likes this.
  13. Offline

    ChipDev

    Oh and yes, why is boolean chatMuted static if not accessed by another class?
     
  14. Offline

    Europia79

    You could try a polymorphic solution. Here's an example of a polymorphic solution that is toggle-able:
    https://github.com/Europia79/Demolition/tree/master/src/main/java/mc/euro/demolition/debug
    https://github.com/Europia79/Demoli...molition/commands/BombExecutor.java#L259-L279

    Basically, you define an interface ChatListener with one method, onChat(AsyncPlayerChatEvent e). Then you define two implementations of ChatListener: ChatOn & ChatOff ....(for example, or you can use different names if you prefer, like ChatMuted/ChatNotMuted).

    Of course, the registration process will be slightly different than normal:
    Code:
    public class MyPlugin extends JavaPlugin {
      public ChatListener chatListener;
      @Override
      public void onEnable() {
        this.chatListener = new ChatOn();
        getServer().getPluginManager().registerEvents(chatListener, this);
      }
    Normally, for registerEvents(), you'd just new-up a class inside the parenthesis... But here, we're holding a reference via the field, chatListener. So... in your command class, you can do something similar to what I did in my cmd class:

    Code:
    if (plugin.chatListener instanceof ChatOn) {
                plugin.chatListener = new ChatOff();
                sender.sendMessage("Chat has been turned off for non-Admins.");
                return true;
            } else if (plugin.chatListener instanceof ChatOff) {
                plugin.chatListener = new ChatOn();
                sender.sendMessage("Chat has been turned on for everyone.");
                return true;
            }
            return false;
    The idea here is that you registered the chatListener field, and now you're updating the reference. So in theory, it should get update for Craftbukkit too. But I would have to test it out... to make sure nothing weird is going on.

    This is ChatOn:
    Code:
    @EventHandler
    public void MuteChat(AsyncPlayerChatEvent e) {
    }
    So, the advantage... is that when chat is "ON"... no extra code is being run. ChatOff just checks for the permission "pe.admin".

    Good luck!
     
  15. Offline

    Funergy

    @Europia79 Read the whole thread before you post anything. He said it was ALL fixed.
     
  16. Offline

    Europia79

    @Funergy Posting alternative solutions is not against the rules. Please don't give people grief for trying to help. And I did read the whole thread before posting. Also...

    1. The point of having a forum discussion is that people with similar problems can perform a search first... So my post is applicable to more readers than just the original poster.
    2. My solution was DIFFERENT than other posters... The OP may decide to employ my suggested polymorhpic approach... Or he (or another reader) might store it in his repertoire for future use to solve another problem.

    Also, eventho we're talking specifically about muting... The solutions in this thread are actually applicable to other problems. For example, anytime you want something to be toggle-able: you can use boolean flags, or you can use polymorphism to change behavior between MANY different states... not just two.

    Here's an example where you can toggle between many different states (three in this case):
    https://github.com/Europia79/Demolition/tree/master/src/main/java/mc/euro/demolition/holograms

    Me personally, I love contemplating & analyzing design and reading about alternative solutions. Hopefully this helps someone. Good luck!
     
Thread Status:
Not open for further replies.

Share This Page