Chat event not canceling?

Discussion in 'Plugin Development' started by GoogleMaps, Dec 19, 2015.

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

    GoogleMaps

    Its not cancelling for some reason
    I also Tried PlayerChatEvent
    As well as AsyncPlayerChatEvent
    Code:
    package com.GoogleMaps.Chat;
    
    import java.util.ArrayList;
    
    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.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    public class Main extends JavaPlugin implements Listener {
        ArrayList<String> staffChat = new ArrayList<String>();
       
        @Override
        public void onEnable() {
            staffChat.clear();
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
    
        }
        @Override
        public void onDisable() {
    
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player p = (Player) sender;
            if(label.equalsIgnoreCase("sc"))
            {
                if(!p.hasPermission("googlemaps.staffchat"))
                {
                    p.sendMessage("§4 Error: §cNo Permission");
                   
                    return true;
                }
                if(args.length > 0)
                {
                    p.sendMessage("§4 Error: §Usage /sc");
                    return true;
                }
                if(args.length == 0)
                {
                    if(staffChat.contains(p.getName()))
                    {
                        staffChat.remove(p.getName());
                        p.sendMessage("§b You have been §cremoved §bfrom StaffChat");
                        return true;
                   
                    }
                    if(!staffChat.contains(p.getName()))
                    {
                        staffChat.add(p.getName());
                        p.sendMessage("§b You have been §aadded §bfrom StaffChat");
                        return true;
                    }
                }
               
            }
            return false;
    
        }
       
        public void onChat(AsyncPlayerChatEvent e)
        {
            Player p = e.getPlayer();
            if(staffChat.contains(p.getName()))
            {
                e.setCancelled(true);
                //Debug
                p.sendMessage("Canceled");
                for(Player ps : Bukkit.getServer().getOnlinePlayers())
                {
                    if(ps.hasPermission("googlemaps.staffchat"))
                    {
                        ps.sendMessage(p.getDisplayName() + ChatColor.DARK_RED + " : " + e.getMessage());
                       
                    }
                }
               
            }
           
        }
       
       
    
    }
    
     
  2. Offline

    Zombie_Striker

    Not only is this bad due to the fact that you do not own that domain, but you have Capital letters in your package name. Everything should be lowercase
    DON'T NAME MAIN CLASSES "MAIN"
    staffChat was just created, you don;t need to clear it.
    This does nothing. Remove it.

    BLIND CASTING! Check if the sender is actually a player by using if(sender instanceof Player)

    Use cmd.getName() because it works with aliases and label is no longer recommended.

    You may not be using the returns correctly. return true when the command worked as it was supposed to. return false when the command failed at something.

    Use "else" instead of checking for staff.

    Hey, you need the @EventHandler tag in order for commands to work.
     
  3. Offline

    iAmGuus

    @Zombie_Striker , these guys are too freakin lazy to just do a 10 second Google search, they think we are here 24/7 on the forums. Well, we are not. I think I am going to make a post about this because these people need to learn something. By the way, the whole post you made up here, wasn't needed. You should've redirected them to the wiki and Oracle's JavaDocs 'cause it doesn't seem like they've learned Java very well.
     
    Zombie_Striker and Nibbit like this.
  4. Offline

    Nibbit

    @GoogleMaps
    It's just too funny that you used googlemaps domain... :p Before you set your "fake" domain please check if it is available xD
     
  5. Offline

    Zombie_Striker

    @iAmGuus
    I have to write that huge post because if I said "hey, you don't have that much knowledge in what you're doing. Please learn the language you are trying to write in. Click this <link>", they will either be insulted or just simply not do anything.
     
  6. Offline

    Irantwomiles

    @Zombie_Striker I'll do it for you. @GoogleMaps Please learn the basics of the Java Language before attempting to learn Bukkit, Or atleast do a 10 min google search and all of your problems would have been solved!
     
    Zombie_Striker likes this.
Thread Status:
Not open for further replies.

Share This Page