Search in sources :

Example 1 with ShareCommandEvent

use of com.github.crashdemons.displayitem_spigot.events.ShareCommandEvent in project DisplayItem-Spigot by crashdemons.

the class ChatEventExecutor method onChatInternal.

public void onChatInternal(@NotNull ChatEvent event) {
    if (event instanceof ReplacedChatEvent)
        return;
    boolean isShareCommand = event instanceof ShareCommandEvent;
    boolean isPrivateMessage = false;
    if (isShareCommand) {
        ShareCommandEvent shareEvent = (ShareCommandEvent) event;
        isPrivateMessage = shareEvent.isPrivateMessage();
    }
    Player player = event.getPlayer();
    String format = event.getFormat();
    String message = event.getMessage();
    String replacestr = DisplayItem.plugin.getConfig().getString("displayitem.replacement");
    String metareplacestr = DisplayItem.plugin.getConfig().getString("displayitem.metareplacement");
    boolean itemNeedsReplacing = message.contains(replacestr);
    if (!isShareCommand && !player.hasPermission("displayitem.replace"))
        itemNeedsReplacing = false;
    if (itemNeedsReplacing && checkSpam(player, message))
        itemNeedsReplacing = false;
    if (itemNeedsReplacing) {
        event.setCancelled(true);
        SplitChatMessage chatLineSplit = replaceItem(player, format, message, isShareCommand);
        ReplacedChatEvent replacementEvent = new ReplacedChatEvent(event);
        String legacyMessage = "";
        for (BaseComponent component : chatLineSplit.content) {
            legacyMessage += component.toLegacyText();
        }
        // DisplayItem.plugin.getLogger().info("debug: <"+legacyMessage+">");
        replacementEvent.setMessage(legacyMessage);
        replacementEvent.setMessageComponents(chatLineSplit.content);
        if (sendModifiedChatEvent && (!isPrivateMessage || !blockPrivateChatEvent)) {
            Bukkit.getServer().getPluginManager().callEvent(replacementEvent);
            if (replacementEvent.isCancelled())
                return;
        }
        for (Player p : event.getRecipients()) {
            p.spigot().sendMessage(chatLineSplit.toComponents());
        }
    }
    // make sure we replace [I]->[i] on the original event, regardless of it being cancelled.
    if ((!metareplacestr.isEmpty()) && message.contains(metareplacestr)) {
        message = message.replace(metareplacestr, replacestr);
        event.setMessage(message);
    }
}
Also used : Player(org.bukkit.entity.Player) BaseComponent(net.md_5.bungee.api.chat.BaseComponent) ShareCommandEvent(com.github.crashdemons.displayitem_spigot.events.ShareCommandEvent) ReplacedChatEvent(com.github.crashdemons.displayitem_spigot.events.ReplacedChatEvent)

Example 2 with ShareCommandEvent

use of com.github.crashdemons.displayitem_spigot.events.ShareCommandEvent in project DisplayItem-Spigot by crashdemons.

the class DisplayItem method shareItem.

private void shareItem(Player sender, Player target) {
    ShareCommandEvent event;
    if (target == null) {
        event = new ShareCommandEvent(sender);
    } else {
        event = new ShareCommandEvent(sender, target);
    }
    syncOperation(() -> {
        listener.forceEvent(event);
    }, 1L);
}
Also used : ShareCommandEvent(com.github.crashdemons.displayitem_spigot.events.ShareCommandEvent)

Aggregations

ShareCommandEvent (com.github.crashdemons.displayitem_spigot.events.ShareCommandEvent)2 ReplacedChatEvent (com.github.crashdemons.displayitem_spigot.events.ReplacedChatEvent)1 BaseComponent (net.md_5.bungee.api.chat.BaseComponent)1 Player (org.bukkit.entity.Player)1