Cancelling a player's message if it meets a certain condition.

Discussion in 'Plugin Development' started by ThatGamingRiot, Dec 23, 2014.

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

    ThatGamingRiot

    Hello there, everyone.

    I am working on making a command that disabled the chat. When the command to disable the chat is sent, it creates a hashmap with the value of 1. My listener then checks if the value for that hashmap is 1 every time a player sends a message. If it equals 1, it is supposed to cancel their message.

    It isn't working, and it may just be a silly mistake of mine, but I am unable to find the mistake myself.

    All help is appreciated!

    My code:

    Code:
    package us.riotnetwork.chatcontrol;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    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.AsyncPlayerChatEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import java.util.HashMap;
    
    public class ChatControl extends JavaPlugin implements Listener {
        @Override
        public void onEnable() {
            getLogger().info("ChatControl has been successfully enabled.");
        }
    
        @Override
        public void onDisable() {
            getLogger().info("ChatControl has been successfully disabled.");
        }
    
        HashMap<String, Integer> map = new HashMap<String, Integer>();
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("cchat")) {
                for (int i = 0; i < 100; i++) {
                    Bukkit.broadcastMessage("                                                                            ");
                }
                Bukkit.broadcastMessage(ChatColor.RED + "Chat was cleared.");
            }
            if (cmd.getName().equalsIgnoreCase("dchat")) {
                Player p = (Player) sender;
                map.put("dchat", 1);
                p.sendMessage(ChatColor.DARK_BLUE + "You disabled the chat!");
                Bukkit.broadcastMessage(ChatColor.RED + "Chat has been disabled.");
            }
            return true;
        }
        @EventHandler
        public void onChatDisable(AsyncPlayerChatEvent e) {
            if (map.get("dchat").equals("1")) {
                Player p = (Player) e.getPlayer();
                e.setCancelled(true);
                p.sendMessage(ChatColor.RED + "Chat is disabled!");
            }
        }
    }
    
     
  2. Offline

    mythbusterma

    @ThatGamingRiot

    Erm, why are you using a HashMap to do the job of a boolean? That's about the worst thing I've seen all day.
     
  3. Offline

    ThatGamingRiot

    I'm really new to coding, and I wasn't sure how I would be able to do that.
     
  4. Offline

    mythbusterma

    @ThatGamingRiot

    Instead of using a HashMap, simply have a boolean...much simpler.
     
  5. Offline

    SuperOriginal

    You should at least attempt learning Java before trying this... Booleans are super basic
     
  6. Offline

    nverdier

    @ThatGamingRiot This. To help you out, here's a link to the JavaDocs.
     
  7. Offline

    mine-care

    @ThatGamingRiot on top of what was said above I would like to add:
    Line 31 on the code above, it's pointless it is like a non closing statement broadcastmessage("
    Is not closing
    You don't need to print enable or disable message
    As said by @mythbusterma
    In your dchat command you are casting sender to player without checking. Try doing dchat from your console and you'll see the error.
    Also you're not registering your listeners so it won't work.

    Please proceed to java world before bukkit.
     
  8. Offline

    stoneminer02

    Code:
    JavaNoobie yetAnotherNoobie = new JavaNoobie("ThatGamingRiot");
    if(yetAnotherNoobie.getLearnedJava() == JavaKnownledge.ABSOLUTENOOBIE)
        yetAnotherNoobie.learnJavaAndProceedToBukkitAfterwards();
    else
        useLogic();
     
  9. Offline

    SuperOriginal

    @mine-care The broadcast message is closed it's just a massive space :p
     
    mine-care likes this.
  10. Offline

    Dragonphase

    Read this thread. Why does nobody else do this? It's common sense. Stop teaching people how to fly before they have wings.
     
  11. Offline

    mine-care

    SuperOriginal likes this.
Thread Status:
Not open for further replies.

Share This Page