Search in sources :

Example 1 with SendChannelMessageEvent

use of br.net.fabiozumbi12.UltimateChat.Bukkit.API.SendChannelMessageEvent in project UltimateChat by FabioZumbi12.

the class UCMessages method sendFancyMessage.

protected static boolean sendFancyMessage(String[] format, String msg, UCChannel channel, CommandSender sender, CommandSender tellReceiver) {
    // Execute listener:
    HashMap<String, String> tags = new HashMap<>();
    for (String str : UChat.get().getUCConfig().getStringList("api.legendchat-tags")) {
        tags.put(str, str);
    }
    SendChannelMessageEvent event = new SendChannelMessageEvent(tags, format, sender, channel, msg);
    Bukkit.getPluginManager().callEvent(event);
    if (event.isCancelled()) {
        return event.getCancelIncomingChat();
    }
    boolean cancel = event.getCancelIncomingChat();
    // String toConsole = "";
    registeredReplacers = event.getResgisteredTags();
    defFormat = event.getDefFormat();
    String evmsg = event.getMessage();
    HashMap<CommandSender, UltimateFancy> msgPlayers = new HashMap<>();
    evmsg = composeColor(sender, evmsg);
    if (event.getChannel() != null) {
        UCChannel ch = event.getChannel();
        if (sender instanceof Player && !ch.availableWorlds().isEmpty() && !ch.availableInWorld(((Player) sender).getWorld())) {
            UChat.get().getLang().sendMessage(sender, UChat.get().getLang().get("channel.notavailable").replace("{channel}", ch.getName()));
            return cancel;
        }
        if (!UCPerms.channelWritePerm(sender, ch)) {
            UChat.get().getLang().sendMessage(sender, UChat.get().getLang().get("channel.nopermission").replace("{channel}", ch.getName()));
            return cancel;
        }
        if (ch.isMuted(sender.getName())) {
            UChat.get().getLang().sendMessage(sender, "channel.muted");
            return cancel;
        }
        if (!UCPerms.hasPerm(sender, "bypass.cost") && UChat.get().getVaultEco() != null && sender instanceof Player && ch.getCost() > 0) {
            if (UChat.get().getVaultEco().getBalance((Player) sender, ((Player) sender).getWorld().getName()) < ch.getCost()) {
                UChat.get().getLang().sendMessage(sender, UChat.get().getLang().get("channel.cost").replace("{value}", "" + ch.getCost()));
                return cancel;
            } else {
                UChat.get().getVaultEco().withdrawPlayer((Player) sender, ((Player) sender).getWorld().getName(), ch.getCost());
            }
        }
        List<Player> receivers = new ArrayList<>();
        int noWorldReceived = 0;
        int vanish = 0;
        // put sender
        msgPlayers.put(sender, sendMessage(sender, sender, evmsg, ch, false));
        if (ch.getDistance() > 0 && sender instanceof Player) {
            for (Player play : ((Player) sender).getNearbyEntities(ch.getDistance(), ch.getDistance(), ch.getDistance()).stream().filter(ent -> ent instanceof Player).map(ent -> (Player) ent).collect(Collectors.toList())) {
                if (UCPerms.channelReadPerm(play, ch)) {
                    if (!ch.availableWorlds().isEmpty() && !ch.availableInWorld(play.getWorld())) {
                        continue;
                    }
                    if (ch.isIgnoring(play.getName())) {
                        continue;
                    }
                    if (isIgnoringPlayers(play.getName(), sender.getName())) {
                        noWorldReceived++;
                        continue;
                    }
                    if (!((Player) sender).canSee(play)) {
                        vanish++;
                    }
                    if (!ch.neeFocus() || ch.isMember(play)) {
                        msgPlayers.put(play, sendMessage(sender, play, evmsg, ch, false));
                        receivers.add(play);
                    }
                }
            }
        } else {
            for (Player receiver : UChat.get().getServer().getOnlinePlayers()) {
                if (receiver.equals(sender) || !UCPerms.channelReadPerm(receiver, ch) || (!ch.crossWorlds() && (sender instanceof Player && !receiver.getWorld().equals(((Player) sender).getWorld())))) {
                    continue;
                }
                if (!ch.availableWorlds().isEmpty() && !ch.availableInWorld(receiver.getWorld())) {
                    continue;
                }
                if (ch.isIgnoring(receiver.getName())) {
                    continue;
                }
                if (isIgnoringPlayers(receiver.getName(), sender.getName())) {
                    noWorldReceived++;
                    continue;
                }
                if (sender instanceof Player && (!((Player) sender).canSee(receiver))) {
                    vanish++;
                } else {
                    noWorldReceived++;
                }
                if (!ch.neeFocus() || ch.isMember(receiver)) {
                    msgPlayers.put(receiver, sendMessage(sender, receiver, evmsg, ch, false));
                    receivers.add(receiver);
                }
            }
        }
        // chat spy
        if (!UCPerms.hasPermission(sender, "uchat.chat-spy.bypass")) {
            for (Player receiver : UChat.get().getServer().getOnlinePlayers()) {
                if (!receiver.equals(sender) && !receivers.contains(receiver) && !receivers.contains(sender) && UChat.get().isSpy.contains(receiver.getName()) && UCPerms.hasSpyPerm(receiver, ch.getName())) {
                    String spyformat = UChat.get().getUCConfig().getString("general.spy-format");
                    spyformat = spyformat.replace("{output}", ChatColor.stripColor(sendMessage(sender, receiver, evmsg, ch, true).toOldFormat()));
                    receiver.sendMessage(ChatColor.translateAlternateColorCodes('&', spyformat));
                }
            }
        }
        if (ch.getDistance() == 0 && noWorldReceived <= 0) {
            if (ch.getReceiversMsg()) {
                UChat.get().getLang().sendMessage(sender, "channel.noplayer.world");
            }
        }
        if ((receivers.size() - vanish) <= 0) {
            if (ch.getReceiversMsg()) {
                UChat.get().getLang().sendMessage(sender, "channel.noplayer.near");
            }
        }
    } else {
        if (UChat.get().msgTogglePlayers.contains(tellReceiver.getName()) && !sender.hasPermission("uchat.msgtoggle.exempt")) {
            UChat.get().getLang().sendMessage(sender, "cmd.msgtoggle.msgdisabled");
            return cancel;
        }
        channel = new UCChannel("tell");
        // send spy
        if (!UCPerms.hasPermission(sender, "uchat.chat-spy.bypass")) {
            for (Player receiver : UChat.get().getServer().getOnlinePlayers()) {
                if (!receiver.equals(tellReceiver) && !receiver.equals(sender) && UChat.get().isSpy.contains(receiver.getName()) && UCPerms.hasSpyPerm(receiver, "private")) {
                    String spyformat = UChat.get().getUCConfig().getString("general.spy-format");
                    if (isIgnoringPlayers(tellReceiver.getName(), sender.getName())) {
                        spyformat = UChat.get().getLang().get("chat.ignored") + spyformat;
                    }
                    spyformat = spyformat.replace("{output}", ChatColor.stripColor(sendMessage(sender, tellReceiver, evmsg, channel, true).toOldFormat()));
                    receiver.sendMessage(ChatColor.translateAlternateColorCodes('&', spyformat));
                }
            }
        }
        msgPlayers.put(sender, sendMessage(sender, tellReceiver, evmsg, channel, false));
        if (!isIgnoringPlayers(tellReceiver.getName(), sender.getName())) {
            msgPlayers.put(tellReceiver, sendMessage(sender, tellReceiver, evmsg, channel, false));
        }
        if (isIgnoringPlayers(tellReceiver.getName(), sender.getName())) {
            msgPlayers.put(UChat.get().getServer().getConsoleSender(), new UltimateFancy(UChat.get().getLang().get("chat.ignored") + msgPlayers.get(sender).toOldFormat()));
        }
    }
    if (!msgPlayers.keySet().contains(UChat.get().getServer().getConsoleSender())) {
        msgPlayers.put(UChat.get().getServer().getConsoleSender(), msgPlayers.get(sender));
    }
    // fire post event
    PostFormatChatMessageEvent postEvent = new PostFormatChatMessageEvent(sender, msgPlayers, channel, msg);
    Bukkit.getPluginManager().callEvent(postEvent);
    if (postEvent.isCancelled()) {
        return cancel;
    }
    if (channel != null && !channel.isTell() && channel.isBungee()) {
        UChatBungee.sendBungee(channel, msgPlayers.get(sender));
    } else {
        msgPlayers.forEach((send, text) -> {
            UChat.get().getUCLogger().timings(timingType.END, "UCMessages#send()|before send");
            text.send(send);
            UChat.get().getUCLogger().timings(timingType.END, "UCMessages#send()|after send");
        });
    }
    if (channel != null) {
        // send to jedis
        if (!channel.isTell() && UChat.get().getJedis() != null) {
            UChat.get().getJedis().sendMessage(channel.getName().toLowerCase(), msgPlayers.get(sender));
        }
        // send to jda
        if (UChat.get().getUCJDA() != null) {
            if (channel.isTell()) {
                UChat.get().getUCJDA().sendTellToDiscord(msgPlayers.get(sender).toOldFormat());
            } else {
                UChat.get().getUCJDA().sendToDiscord(sender, evmsg, channel);
            }
        }
    }
    return cancel;
}
Also used : PostFormatChatMessageEvent(br.net.fabiozumbi12.UltimateChat.Bukkit.API.PostFormatChatMessageEvent) UCLogger.timingType(br.net.fabiozumbi12.UltimateChat.Bukkit.UCLogger.timingType) StringUtils(org.apache.commons.lang.StringUtils) CommandSender(org.bukkit.command.CommandSender) java.util(java.util) Entity(org.bukkit.entity.Entity) Enchantment(org.bukkit.enchantments.Enchantment) ItemMeta(org.bukkit.inventory.meta.ItemMeta) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) SimpleDateFormat(java.text.SimpleDateFormat) ClanPlayer(net.sacredlabyrinth.phaed.simpleclans.ClanPlayer) Sound(org.bukkit.Sound) Player(org.bukkit.entity.Player) PlaceholderAPI(me.clip.placeholderapi.PlaceholderAPI) Collectors(java.util.stream.Collectors) ItemStack(org.bukkit.inventory.ItemStack) UChatBungee(br.net.fabiozumbi12.UltimateChat.Bukkit.Bungee.UChatBungee) SettingsManager(net.sacredlabyrinth.phaed.simpleclans.managers.SettingsManager) Entry(java.util.Map.Entry) SendChannelMessageEvent(br.net.fabiozumbi12.UltimateChat.Bukkit.API.SendChannelMessageEvent) ChatColor(org.bukkit.ChatColor) Material(org.bukkit.Material) Bukkit(org.bukkit.Bukkit) ClanPlayer(net.sacredlabyrinth.phaed.simpleclans.ClanPlayer) Player(org.bukkit.entity.Player) PostFormatChatMessageEvent(br.net.fabiozumbi12.UltimateChat.Bukkit.API.PostFormatChatMessageEvent) CommandSender(org.bukkit.command.CommandSender) SendChannelMessageEvent(br.net.fabiozumbi12.UltimateChat.Bukkit.API.SendChannelMessageEvent)

Example 2 with SendChannelMessageEvent

use of br.net.fabiozumbi12.UltimateChat.Bukkit.API.SendChannelMessageEvent in project UltimateChat by FabioZumbi12.

the class UCMessages method sendFancyMessage.

static MutableMessageChannel sendFancyMessage(String[] format, Text msg, UCChannel channel, CommandSource sender, CommandSource tellReceiver) {
    // Execute listener:
    HashMap<String, String> tags = new HashMap<>();
    for (String str : UChat.get().getConfig().root().general.custom_tags) {
        tags.put(str, str);
    }
    SendChannelMessageEvent event = new SendChannelMessageEvent(tags, format, sender, channel, msg, true);
    Sponge.getEventManager().post(event);
    if (event.isCancelled()) {
        return null;
    }
    registeredReplacers = event.getResgisteredTags();
    defFormat = event.getDefFormat();
    String evmsg = event.getMessage().toPlain();
    // send to event
    MutableMessageChannel msgCh = MessageChannel.TO_CONSOLE.asMutable();
    evmsg = UCChatProtection.filterChatMessage(sender, evmsg, event.getChannel());
    if (evmsg == null) {
        return null;
    }
    HashMap<CommandSource, Text> msgPlayers = new HashMap<>();
    evmsg = composeColor(sender, evmsg);
    Text srcText = Text.builder(event.getMessage(), evmsg).build();
    if (event.getChannel() != null) {
        UCChannel ch = event.getChannel();
        if (sender instanceof Player && !ch.availableWorlds().isEmpty() && !ch.availableInWorld(((Player) sender).getWorld())) {
            UChat.get().getLang().sendMessage(sender, UChat.get().getLang().get("channel.notavailable").replace("{channel}", ch.getName()));
            return null;
        }
        if (!UChat.get().getPerms().channelWritePerm(sender, ch)) {
            UChat.get().getLang().sendMessage(sender, UChat.get().getLang().get("channel.nopermission").replace("{channel}", ch.getName()));
            return null;
        }
        if (!UChat.get().getPerms().hasPerm(sender, "bypass.cost") && UChat.get().getEco() != null && sender instanceof Player && ch.getCost() > 0) {
            UniqueAccount acc = UChat.get().getEco().getOrCreateAccount(((Player) sender).getUniqueId()).get();
            if (acc.getBalance(UChat.get().getEco().getDefaultCurrency()).doubleValue() < ch.getCost()) {
                sender.sendMessage(UCUtil.toText(UChat.get().getLang().get("channel.cost").replace("{value}", "" + ch.getCost())));
                return null;
            } else {
                acc.withdraw(UChat.get().getEco().getDefaultCurrency(), BigDecimal.valueOf(ch.getCost()), UChat.get().getVHelper().getCause(UChat.get().instance()));
            }
        }
        int noWorldReceived = 0;
        int vanish = 0;
        List<Player> receivers = new ArrayList<>();
        // put sender
        msgPlayers.put(sender, sendMessage(sender, sender, srcText, ch, false));
        msgCh.addMember(sender);
        if (ch.getDistance() > 0 && sender instanceof Player) {
            for (Player play : ((Player) sender).getNearbyEntities(ch.getDistance()).stream().filter(ent -> ent instanceof Player).map(p -> (Player) p).collect(Collectors.toList())) {
                if (UChat.get().getPerms().channelReadPerm(play, ch)) {
                    if (sender.equals(play)) {
                        continue;
                    }
                    if (!ch.availableWorlds().isEmpty() && !ch.availableInWorld(play.getWorld())) {
                        continue;
                    }
                    if (ch.isIgnoring(play.getName())) {
                        continue;
                    }
                    if (isIgnoringPlayers(play.getName(), sender.getName())) {
                        noWorldReceived++;
                        continue;
                    }
                    if (!((Player) sender).canSee(play)) {
                        vanish++;
                    }
                    if (!ch.neeFocus() || ch.isMember(play)) {
                        msgPlayers.put(play, sendMessage(sender, play, srcText, ch, false));
                        receivers.add(play);
                        msgCh.addMember(play);
                    }
                }
            }
        } else {
            for (Player receiver : Sponge.getServer().getOnlinePlayers()) {
                if (receiver.equals(sender) || !UChat.get().getPerms().channelReadPerm(receiver, ch) || (!ch.crossWorlds() && (sender instanceof Player && !receiver.getWorld().equals(((Player) sender).getWorld())))) {
                    continue;
                }
                if (!ch.availableWorlds().isEmpty() && !ch.availableInWorld(receiver.getWorld())) {
                    continue;
                }
                if (ch.isIgnoring(receiver.getName())) {
                    continue;
                }
                if (isIgnoringPlayers(receiver.getName(), sender.getName())) {
                    noWorldReceived++;
                    continue;
                }
                if (sender instanceof Player && !((Player) sender).canSee(receiver)) {
                    vanish++;
                } else {
                    noWorldReceived++;
                }
                if (!ch.neeFocus() || ch.isMember(receiver)) {
                    msgPlayers.put(receiver, sendMessage(sender, receiver, srcText, ch, false));
                    receivers.add(receiver);
                    msgCh.addMember(receiver);
                }
            }
        }
        // chat spy
        if (!sender.hasPermission("uchat.chat-spy.bypass")) {
            for (Player receiver : Sponge.getServer().getOnlinePlayers()) {
                if (!receiver.equals(sender) && !receivers.contains(receiver) && !receivers.contains(sender) && UChat.get().isSpy.contains(receiver.getName()) && UChat.get().getPerms().hasSpyPerm(receiver, ch.getName())) {
                    String spyformat = UChat.get().getConfig().root().general.spy_format;
                    spyformat = spyformat.replace("{output}", UCUtil.stripColor(sendMessage(sender, receiver, srcText, ch, true).toPlain()));
                    receiver.sendMessage(UCUtil.toText(spyformat));
                }
            }
        }
        if (ch.getDistance() == 0 && noWorldReceived <= 0) {
            if (ch.getReceiversMsg()) {
                UChat.get().getLang().sendMessage(sender, "channel.noplayer.world");
            }
        }
        if ((receivers.size() - vanish) <= 0) {
            if (ch.getReceiversMsg()) {
                UChat.get().getLang().sendMessage(sender, "channel.noplayer.near");
            }
        }
    } else {
        if (UChat.get().msgTogglePlayers.contains(tellReceiver.getName()) && !sender.hasPermission("uchat.msgtoggle.exempt")) {
            UChat.get().getLang().sendMessage(sender, "cmd.msgtoggle.msgdisabled");
            return null;
        }
        channel = new UCChannel("tell");
        // send spy
        if (!sender.hasPermission("uchat.chat-spy.bypass")) {
            for (Player receiver : Sponge.getServer().getOnlinePlayers()) {
                if (!receiver.equals(tellReceiver) && !receiver.equals(sender) && UChat.get().isSpy.contains(receiver.getName()) && UChat.get().getPerms().hasSpyPerm(receiver, "private")) {
                    String spyformat = UChat.get().getConfig().root().general.spy_format;
                    if (isIgnoringPlayers(tellReceiver.getName(), sender.getName())) {
                        spyformat = UChat.get().getLang().get("chat.ignored") + spyformat;
                    }
                    spyformat = spyformat.replace("{output}", UCUtil.stripColor(sendMessage(sender, tellReceiver, srcText, channel, true).toPlain()));
                    receiver.sendMessage(UCUtil.toText(spyformat));
                }
            }
        }
        Text to = sendMessage(sender, tellReceiver, srcText, channel, false);
        msgPlayers.put(tellReceiver, to);
        msgPlayers.put(sender, to);
        if (isIgnoringPlayers(tellReceiver.getName(), sender.getName())) {
            to = Text.of(UCUtil.toText(UChat.get().getLang().get("chat.ignored")), to);
        }
        msgPlayers.put(Sponge.getServer().getConsole(), to);
    }
    if (!msgPlayers.keySet().contains(Sponge.getServer().getConsole())) {
        msgPlayers.put(Sponge.getServer().getConsole(), msgPlayers.values().stream().findAny().get());
    }
    PostFormatChatMessageEvent postEvent = new PostFormatChatMessageEvent(sender, msgPlayers, channel);
    Sponge.getEventManager().post(postEvent);
    if (postEvent.isCancelled()) {
        return null;
    }
    msgPlayers.forEach((send, text) -> {
        UChat.get().getLogger().timings(timingType.END, "UCMessages#send()|before send");
        send.sendMessage(text);
        UChat.get().getLogger().timings(timingType.END, "UCMessages#send()|after send");
    });
    // send to jedis
    if (channel != null && !channel.isTell() && UChat.get().getJedis() != null) {
        UChat.get().getJedis().sendMessage(channel.getName().toLowerCase(), msgPlayers.get(sender));
    }
    // send to jda
    if (channel != null && UChat.get().getUCJDA() != null) {
        if (channel.isTell()) {
            UChat.get().getUCJDA().sendTellToDiscord(msgPlayers.get(sender).toPlain());
        } else {
            UChat.get().getUCJDA().sendToDiscord(sender, evmsg, channel);
        }
    }
    return msgCh;
}
Also used : java.util(java.util) Keys(org.spongepowered.api.data.key.Keys) URL(java.net.URL) TextTemplate(org.spongepowered.api.text.TextTemplate) ItemTypes(org.spongepowered.api.item.ItemTypes) SimpleDateFormat(java.text.SimpleDateFormat) StringUtils(org.apache.commons.lang3.StringUtils) BigDecimal(java.math.BigDecimal) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Matcher(java.util.regex.Matcher) Text(org.spongepowered.api.text.Text) Subject(org.spongepowered.api.service.permission.Subject) UCLogger.timingType(br.net.fabiozumbi12.UltimateChat.Sponge.UCLogger.timingType) TextUtils(me.rojo8399.placeholderapi.impl.utils.TextUtils) MutableMessageChannel(org.spongepowered.api.text.channel.MutableMessageChannel) PlaceholderService(me.rojo8399.placeholderapi.PlaceholderService) TextActions(org.spongepowered.api.text.action.TextActions) SendChannelMessageEvent(br.net.fabiozumbi12.UltimateChat.Sponge.API.SendChannelMessageEvent) CommandSource(org.spongepowered.api.command.CommandSource) MalformedURLException(java.net.MalformedURLException) SoundType(org.spongepowered.api.effect.sound.SoundType) Builder(org.spongepowered.api.text.Text.Builder) Sponge(org.spongepowered.api.Sponge) PostFormatChatMessageEvent(br.net.fabiozumbi12.UltimateChat.Sponge.API.PostFormatChatMessageEvent) Entity(org.spongepowered.api.entity.Entity) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) TextSerializers(org.spongepowered.api.text.serializer.TextSerializers) ClanService(nl.riebie.mcclans.api.ClanService) NucleusAPI(io.github.nucleuspowered.nucleus.api.NucleusAPI) HandTypes(org.spongepowered.api.data.type.HandTypes) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) Player(org.spongepowered.api.entity.living.player.Player) Pattern(java.util.regex.Pattern) ClanPlayer(nl.riebie.mcclans.api.ClanPlayer) UniqueAccount(org.spongepowered.api.service.economy.account.UniqueAccount) Player(org.spongepowered.api.entity.living.player.Player) ClanPlayer(nl.riebie.mcclans.api.ClanPlayer) UniqueAccount(org.spongepowered.api.service.economy.account.UniqueAccount) Text(org.spongepowered.api.text.Text) CommandSource(org.spongepowered.api.command.CommandSource) PostFormatChatMessageEvent(br.net.fabiozumbi12.UltimateChat.Sponge.API.PostFormatChatMessageEvent) SendChannelMessageEvent(br.net.fabiozumbi12.UltimateChat.Sponge.API.SendChannelMessageEvent) MutableMessageChannel(org.spongepowered.api.text.channel.MutableMessageChannel)

Aggregations

MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 SimpleDateFormat (java.text.SimpleDateFormat)2 java.util (java.util)2 Collectors (java.util.stream.Collectors)2 PostFormatChatMessageEvent (br.net.fabiozumbi12.UltimateChat.Bukkit.API.PostFormatChatMessageEvent)1 SendChannelMessageEvent (br.net.fabiozumbi12.UltimateChat.Bukkit.API.SendChannelMessageEvent)1 UChatBungee (br.net.fabiozumbi12.UltimateChat.Bukkit.Bungee.UChatBungee)1 UCLogger.timingType (br.net.fabiozumbi12.UltimateChat.Bukkit.UCLogger.timingType)1 PostFormatChatMessageEvent (br.net.fabiozumbi12.UltimateChat.Sponge.API.PostFormatChatMessageEvent)1 SendChannelMessageEvent (br.net.fabiozumbi12.UltimateChat.Sponge.API.SendChannelMessageEvent)1 UCLogger.timingType (br.net.fabiozumbi12.UltimateChat.Sponge.UCLogger.timingType)1 NucleusAPI (io.github.nucleuspowered.nucleus.api.NucleusAPI)1 BigDecimal (java.math.BigDecimal)1 Entry (java.util.Map.Entry)1 ExecutionException (java.util.concurrent.ExecutionException)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 PlaceholderAPI (me.clip.placeholderapi.PlaceholderAPI)1 PlaceholderService (me.rojo8399.placeholderapi.PlaceholderService)1