Search in sources :

Example 1 with PlayerMentionPlayerEvent

use of com.loohp.interactivechat.api.events.PlayerMentionPlayerEvent in project InteractiveChat by LOOHP.

the class MentionDisplay method process.

public static Component process(Component component, Player receiver, ICPlayer sender, long unix, boolean async) {
    Optional<MentionPair> optPair;
    synchronized (InteractiveChat.mentionPair) {
        optPair = InteractiveChat.mentionPair.stream().filter(each -> each.getReciever().equals(receiver.getUniqueId())).findFirst();
    }
    if (optPair.isPresent()) {
        MentionPair pair = optPair.get();
        if (pair.getSender().equals(sender.getUniqueId())) {
            String title = PlaceholderParser.parse(sender, InteractiveChat.mentionTitle);
            String subtitle = PlaceholderParser.parse(sender, InteractiveChat.mentionSubtitle);
            String actionbar = PlaceholderParser.parse(sender, InteractiveChat.mentionActionbar);
            String toast = PlaceholderParser.parse(sender, InteractiveChat.mentionToast);
            String bossBarText = PlaceholderParser.parse(sender, InteractiveChat.mentionBossBarText);
            String bossBarColorName = InteractiveChat.mentionBossBarColorName;
            String bossBarOverlayName = InteractiveChat.mentionBossBarOverlayName;
            Optional<BossBar> optBossBar;
            if (bossBarText.isEmpty()) {
                optBossBar = Optional.empty();
            } else {
                optBossBar = Optional.of(BossBar.bossBar(LegacyComponentSerializer.legacySection().deserialize(bossBarText), 1, Color.valueOf(bossBarColorName.toUpperCase()), Overlay.valueOf(bossBarOverlayName.toUpperCase())));
            }
            String settings = InteractiveChat.mentionSound;
            Either<Sound, String> sound;
            float volume = 3.0F;
            float pitch = 1.0F;
            String[] settingsArgs = settings.split(":");
            if (settingsArgs.length >= 3) {
                settings = String.join("", Arrays.copyOfRange(settingsArgs, 0, settingsArgs.length - 2)).toUpperCase();
                try {
                    volume = Float.parseFloat(settingsArgs[settingsArgs.length - 2]);
                } catch (Exception ignore) {
                }
                try {
                    pitch = Float.parseFloat(settingsArgs[settingsArgs.length - 1]);
                } catch (Exception ignore) {
                }
            } else {
                settings = settings.toUpperCase();
            }
            Sound bukkitSound = SoundUtils.parseSound(settings);
            if (bukkitSound == null) {
                sound = Either.right(settings);
            } else {
                sound = Either.left(bukkitSound);
            }
            boolean silent = false;
            Map<UUID, Long> lastMentionMapping = InteractiveChat.lastNonSilentMentionTime.get(receiver.getUniqueId());
            if (lastMentionMapping != null) {
                Long lastMention = lastMentionMapping.get(sender.getUniqueId());
                silent = lastMention != null && unix - lastMention < InteractiveChat.mentionCooldown;
            }
            PlayerMentionPlayerEvent mentionEvent = new PlayerMentionPlayerEvent(async, receiver, sender.getUniqueId(), title, subtitle, actionbar, toast, optBossBar, sound, silent, false);
            Bukkit.getPluginManager().callEvent(mentionEvent);
            if (!mentionEvent.isCancelled()) {
                if (!mentionEvent.isSilent()) {
                    if (lastMentionMapping != null) {
                        lastMentionMapping.put(sender.getUniqueId(), unix);
                    }
                    title = mentionEvent.getTitle();
                    subtitle = mentionEvent.getSubtitle();
                    actionbar = mentionEvent.getActionbar();
                    int time = InteractiveChat.mentionTitleDuration;
                    TitleUtils.sendTitle(receiver, title, subtitle, actionbar, 10, Math.max(time, 1), 20);
                    if (sound != null) {
                        if (sound.isLeft()) {
                            receiver.playSound(receiver.getLocation(), sound.getLeft(), volume, pitch);
                        } else {
                            String soundLocation = sound.getRight();
                            if (!soundLocation.contains(":")) {
                                soundLocation = "minecraft:" + soundLocation;
                            }
                            receiver.playSound(receiver.getLocation(), soundLocation.toLowerCase(), volume, pitch);
                        }
                    }
                    if (!mentionEvent.getToast().isEmpty() && InteractiveChat.version.isNewerOrEqualTo(MCVersion.V1_12)) {
                        ToastUtils.mention(sender, receiver, toast, WRITABLE_BOOK.clone());
                    }
                    int bossBarTime = InteractiveChat.mentionBossBarDuration;
                    int bossBarRemoveDelay = InteractiveChat.mentionBossBarRemoveDelay;
                    if (mentionEvent.getBossBar().isPresent() && !InteractiveChat.version.isOld()) {
                        BossBarUpdater updater = BossBarUpdater.update(mentionEvent.getBossBar().get(), receiver);
                        BossBarUtils.countdownBossBar(updater, Math.max(bossBarTime, 1), Math.max(bossBarRemoveDelay, 0));
                    }
                }
                List<String> names = new ArrayList<>();
                names.add(ChatColorUtils.stripColor(receiver.getName()));
                if (InteractiveChat.useBukkitDisplayName && !ChatColorUtils.stripColor(receiver.getName()).equals(ChatColorUtils.stripColor(receiver.getDisplayName()))) {
                    names.add(ChatColorUtils.stripColor(receiver.getDisplayName()));
                }
                List<String> list = InteractiveChatAPI.getNicknames(receiver.getUniqueId());
                for (String name : list) {
                    names.add(ChatColorUtils.stripColor(name));
                }
                if (!InteractiveChat.disableHere) {
                    names.add("here");
                }
                if (!InteractiveChat.disableEveryone) {
                    names.add("everyone");
                }
                for (String name : names) {
                    component = processPlayer(InteractiveChat.mentionPrefix + name, receiver, sender, component, unix);
                }
                pair.remove();
            }
        }
    }
    return component;
}
Also used : ArrayList(java.util.ArrayList) BossBar(net.kyori.adventure.bossbar.BossBar) BossBarUpdater(com.loohp.interactivechat.utils.bossbar.BossBarUpdater) Sound(org.bukkit.Sound) PlayerMentionPlayerEvent(com.loohp.interactivechat.api.events.PlayerMentionPlayerEvent) MentionPair(com.loohp.interactivechat.objectholders.MentionPair) UUID(java.util.UUID)

Aggregations

PlayerMentionPlayerEvent (com.loohp.interactivechat.api.events.PlayerMentionPlayerEvent)1 MentionPair (com.loohp.interactivechat.objectholders.MentionPair)1 BossBarUpdater (com.loohp.interactivechat.utils.bossbar.BossBarUpdater)1 ArrayList (java.util.ArrayList)1 UUID (java.util.UUID)1 BossBar (net.kyori.adventure.bossbar.BossBar)1 Sound (org.bukkit.Sound)1