Java Code Examples for org.bukkit.event.player.AsyncPlayerChatEvent#setMessage()

The following examples show how to use org.bukkit.event.player.AsyncPlayerChatEvent#setMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NickNamerPlugin.java    From NickNamer with MIT License 6 votes vote down vote up
@EventHandler(priority = EventPriority.NORMAL)
public void on(final AsyncPlayerChatEvent event) {
	if (ChatReplacementEvent.getHandlerList().getRegisteredListeners().length > 0) {
		final String message = event.getMessage();
		Set<String> nickedPlayerNames = NickNamerAPI.getNickedPlayerNames();
		String replacedMessage = NickNamerAPI.replaceNames(message, nickedPlayerNames, new NameReplacer() {
			@Override
			public String replace(String original) {
				Player player = Bukkit.getPlayer(original);
				if (player != null) {
					boolean async = !getServer().isPrimaryThread();
					NameReplacementEvent replacementEvent = new ChatReplacementEvent(player, event.getRecipients(), message, original, original, async);
					Bukkit.getPluginManager().callEvent(replacementEvent);
					if (replacementEvent.isCancelled()) { return original; }
					return replacementEvent.getReplacement();
				}
				return original;
			}
		}, true);
		event.setMessage(replacedMessage);
	}
}
 
Example 2
Source File: GameListeners.java    From AnnihilationPro with MIT License 6 votes vote down vote up
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void AnniPlayersInit(AsyncPlayerChatEvent event)
{
	if(event.isAsynchronous())
	{
		AnniPlayer p = AnniPlayer.getPlayer(event.getPlayer().getUniqueId());
		if(p.getTeam() == null)
			event.setFormat(g+"(All) ["+ChatColor.DARK_PURPLE+"Lobby"+g+"] %s"+ChatColor.WHITE+": %s");
		else if(event.getMessage().startsWith("!"))
		{
			event.setMessage(event.getMessage().substring(1));
			event.setFormat(g+"(All) ["+p.getTeam().getColor()+p.getTeam().toString()+g+"] %s"+ChatColor.WHITE+": %s");
		}
		else
		{
			event.setFormat(g+"(Team) ["+p.getTeam().getColor()+p.getTeam().toString()+g+"] %s"+ChatColor.WHITE+": %s");
			event.getRecipients().clear();
			for(AnniPlayer pl : p.getTeam().getPlayers())
				if(pl.isOnline())
					event.getRecipients().add(pl.getPlayer());
		}
	}
}
 
Example 3
Source File: PlayerChatListener.java    From UhcCore with GNU General Public License v3.0 5 votes vote down vote up
private boolean isTeamMessage(MainConfiguration cfg, AsyncPlayerChatEvent e, UhcPlayer uhcPlayer){
	if (cfg.getEnableChatPrefix()){
		if (e.getMessage().startsWith(cfg.getTeamChatPrefix())){
			e.setMessage(e.getMessage().replaceFirst(cfg.getTeamChatPrefix(), ""));
			return true;
		}
		if (e.getMessage().startsWith(cfg.getGlobalChatPrefix())){
			e.setMessage(e.getMessage().replaceFirst(cfg.getGlobalChatPrefix(), ""));
			return false;
		}
	}

	return !uhcPlayer.isGlobalChat();
}
 
Example 4
Source File: AsyncPlayerChat.java    From StaffPlus with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onChat(AsyncPlayerChatEvent event)
{
	Player player = event.getPlayer();
	String message = event.getMessage();
	
	if(shouldCancel(player, message))
	{
		event.setCancelled(true);
		return;
	}
	
	List<User> mentioned = getMentioned(message);
	
	if(!mentioned.isEmpty())
	{
		for(User user : mentioned)
		{
			alertCoordinator.onMention(user, player.getName());
		}
	}
	
	if(options.chatBlacklistEnabled && options.chatEnabled)
	{
		BlacklistFactory blacklistFactory = new BlacklistFactory(message);
		
		if(blacklistFactory.runCheck().hasChanged()&& !permission.has(player, options.permissionBlacklist))
		{
			event.setMessage(blacklistFactory.getResult());
			
			if(options.chatBlacklistHoverable)
			{
				Set<Player> staffPlayers = new HashSet<Player>();
				
				for(Player p : Bukkit.getOnlinePlayers())
				{
					if(permission.has(p, options.permissionBlacklist))
					{
						event.getRecipients().remove(p);
						staffPlayers.add(p);
					}
				}
				
				versionProtocol.sendHoverableJsonMessage(staffPlayers, messages.blacklistChatFormat.replace("%player%", player.getName()).replace("%message%", blacklistFactory.getResult()), message);
			}
		}
	}
}
 
Example 5
Source File: ChatEventListener.java    From ChatItem with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)  //We need to have lowest priority in order to get to the event before DeluxeChat or other plugins do
public void onChat(final AsyncPlayerChatEvent e) {
    if(e.getMessage().indexOf(SEPARATOR)!=-1){  //If the BELL character is found, we have to remove it
        String msg = e.getMessage().replace(Character.toString(SEPARATOR), "");
        e.setMessage(msg);
    }
    boolean found = false;

    for (String rep : c.PLACEHOLDERS)
        if (e.getMessage().contains(rep)) {
            found = true;
            break;
        }

    if (!found) {
        return;
    }

    Player p = e.getPlayer();

    if (!p.hasPermission("chatitem.use")) {
        if(!c.LET_MESSAGE_THROUGH) {
            e.setCancelled(true);
        }
        if(!c.NO_PERMISSION_MESSAGE.isEmpty() && c.SHOW_NO_PERM_NORMAL){
            p.sendMessage(c.NO_PERMISSION_MESSAGE);
        }
        return;
    }
    if (p.getItemInHand().getType().equals(Material.AIR)) {
        if (c.DENY_IF_NO_ITEM) {
            e.setCancelled(true);
            if (!c.DENY_MESSAGE.isEmpty())
                e.getPlayer().sendMessage(c.DENY_MESSAGE);
            return;
        }
        if(c.HAND_DISABLED) {
            return;
        }
    }
    if(c.COOLDOWN > 0 && !p.hasPermission("chatitem.ignore-cooldown")){
        if(COOLDOWNS.containsKey(p.getName())){
            long start = COOLDOWNS.get(p.getName());
            long current = System.currentTimeMillis()/1000;
            long elapsed = current - start;
            if(elapsed >= c.COOLDOWN){
                COOLDOWNS.remove(p.getName());
            }else{
                if(!c.LET_MESSAGE_THROUGH) {
                    e.setCancelled(true);
                }
                if(!c.COOLDOWN_MESSAGE.isEmpty()){
                    long left = (start + c.COOLDOWN) - current;
                    p.sendMessage(c.COOLDOWN_MESSAGE.replace(LEFT, calculateTime(left)));
                }
                return;
            }
        }
    }
    String s = e.getMessage();
    for(String placeholder : c.PLACEHOLDERS){
        s = s.replace(placeholder, c.PLACEHOLDERS.get(0));
    }
    int occurrences = countOccurrences(c.PLACEHOLDERS.get(0), s);

    if(occurrences>c.LIMIT){
        e.setCancelled(true);
        if(c.LIMIT_MESSAGE.isEmpty()){
            return;
        }
        e.getPlayer().sendMessage(c.LIMIT_MESSAGE);
        return;
    }

    String oldmsg = e.getMessage();
    StringBuilder sb = new StringBuilder(oldmsg);
    sb.append(SEPARATOR).append(e.getPlayer().getName());
    e.setMessage(sb.toString());
    Bukkit.getConsoleSender().sendMessage(String.format(e.getFormat(), e.getPlayer().getDisplayName(), oldmsg));
    if(!p.hasPermission("chatitem.ignore-cooldown")) {
        COOLDOWNS.put(p.getName(), System.currentTimeMillis() / 1000);
    }
}
 
Example 6
Source File: ChatPlaceholder.java    From uSkyBlock with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler
public void onChatEvent(AsyncPlayerChatEvent e) {
    Player player = e.getPlayer();
    e.setFormat(PlaceholderHandler.replacePlaceholders(player, e.getFormat()));
    e.setMessage(PlaceholderHandler.replacePlaceholders(player, e.getMessage()));
}