All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.handyplus.lib.util.MessageUtil Maven / Gradle / Ivy

package cn.handyplus.lib.util;

import cn.handyplus.lib.InitApi;
import cn.handyplus.lib.constants.BaseConstants;
import cn.handyplus.lib.constants.VersionCheckEnum;
import cn.handyplus.lib.core.CollUtil;
import cn.handyplus.lib.core.StrUtil;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

/**
 * 发送消息
 *
 * @author handy
 */
public class MessageUtil {
    private MessageUtil() {
    }

    /**
     * 发送消息
     *
     * @param playerUuid 玩家uuid
     * @param msg        消息
     * @since 2.2.9
     */
    public static void sendMessage(UUID playerUuid, String msg) {
        Optional playerOpt = BaseUtil.getOnlinePlayer(playerUuid);
        playerOpt.ifPresent(player -> sendMessage(player, msg));
    }

    /**
     * 发送消息
     *
     * @param playerName 玩家名称
     * @param msg        消息
     * @since 2.2.9
     */
    public static void sendMessage(String playerName, String msg) {
        Optional playerOpt = BaseUtil.getOnlinePlayer(playerName);
        playerOpt.ifPresent(player -> sendMessage(player, msg));
    }

    /**
     * 发送消息
     *
     * @param player 玩家
     * @param msg    消息
     */
    public static void sendMessage(Player player, String msg) {
        if (StrUtil.isEmpty(msg)) {
            return;
        }
        player.sendMessage(BaseUtil.replaceChatColor(msg));
    }

    /**
     * 发送消息
     *
     * @param player 玩家
     * @param msg    消息
     */
    public static void sendDebugMessage(Player player, String msg) {
        if (BaseConstants.DEBUG) {
            sendMessage(player, msg);
        }
    }

    /**
     * 发送消息
     *
     * @param player 玩家
     * @param msg    消息
     */
    public static void sendMessage(Player player, TextComponent msg) {
        if (BaseConstants.VERSION_ID < VersionCheckEnum.V_1_9.getVersionId()) {
            player.spigot().sendMessage(msg);
            return;
        }
        player.spigot().sendMessage(ChatMessageType.CHAT, msg);
    }

    /**
     * 发送消息
     *
     * @param player 玩家
     * @param msg    消息
     * @since 3.7.1
     */
    public static void sendMessage(Player player, BaseComponent[] msg) {
        if (BaseConstants.VERSION_ID < VersionCheckEnum.V_1_9.getVersionId()) {
            player.spigot().sendMessage(msg);
            return;
        }
        player.spigot().sendMessage(ChatMessageType.CHAT, msg);
    }

    /**
     * 发送消息
     *
     * @param player 玩家
     * @param msg    消息
     */
    public static void sendDebugMessage(Player player, TextComponent msg) {
        if (BaseConstants.DEBUG) {
            sendMessage(player, msg);
        }
    }

    /**
     * 发送消息
     *
     * @param playerNameList 玩家
     * @param msg            消息
     * @since 3.3.9
     */
    public static void sendMessage(List playerNameList, TextComponent msg) {
        if (CollUtil.isEmpty(playerNameList)) {
            return;
        }
        for (String playerName : playerNameList) {
            Optional playerOpt = BaseUtil.getOnlinePlayer(playerName);
            playerOpt.ifPresent(player -> sendMessage(player, msg));
        }
    }

    /**
     * 发送消息
     *
     * @param sender 玩家
     * @param msg    消息
     */
    public static void sendMessage(CommandSender sender, String msg) {
        if (StrUtil.isEmpty(msg)) {
            return;
        }
        sender.sendMessage(BaseUtil.replaceChatColor(msg));
    }

    /**
     * 发送全服消息
     *
     * @param msg 消息
     */
    public static void sendAllMessage(String msg) {
        if (StrUtil.isEmpty(msg)) {
            return;
        }
        for (Player player : Bukkit.getOnlinePlayers()) {
            MessageUtil.sendMessage(player, msg);
        }
    }

    /**
     * 发送全服消息
     *
     * @param msgList 消息
     * @since 3.10.3
     */
    public static void sendAllMessage(List msgList) {
        if (CollUtil.isEmpty(msgList)) {
            return;
        }
        for (String msg : msgList) {
            sendAllMessage(msg);
        }
    }

    /**
     * 发送全服消息
     *
     * @param msg 消息
     * @since 3.6.8
     */
    public static void sendAllMessage(TextComponent msg) {
        if (msg == null) {
            return;
        }
        for (Player player : Bukkit.getOnlinePlayers()) {
            MessageUtil.sendMessage(player, msg);
        }
    }

    /**
     * 发送Console消息
     *
     * @param msg 消息
     */
    public static void sendConsoleMessage(String msg) {
        if (StrUtil.isEmpty(msg)) {
            return;
        }
        Bukkit.getConsoleSender().sendMessage(ChatColor.GRAY + "[" + InitApi.PLUGIN.getName() + "] " + BaseUtil.replaceChatColor(msg));
    }

    /**
     * 发送Console消息
     *
     * @param msgList 消息列表
     * @since 3.9.7
     */
    public static void sendConsoleMessage(List msgList) {
        if (CollUtil.isEmpty(msgList)) {
            return;
        }
        for (String msg : msgList) {
            sendConsoleMessage(msg);
        }
    }

    /**
     * 发送 Console debug消息
     *
     * @param msg 消息
     */
    public static void sendConsoleDebugMessage(String msg) {
        if (BaseConstants.DEBUG) {
            sendConsoleMessage(msg);
        }
    }

    /**
     * 发送title消息
     *
     * @param player   玩家
     * @param title    标题
     * @param subtitle 副标题
     * @param fadein   淡入时间
     * @param stay     存在时间
     * @param fadeout  淡出时间
     */
    @SuppressWarnings("deprecation")
    public static void sendTitle(Player player, String title, String subtitle, int fadein, int stay, int fadeout) {
        if (BaseConstants.VERSION_ID > VersionCheckEnum.V_1_8_8.getVersionId() && BaseConstants.VERSION_ID < VersionCheckEnum.V_1_11.getVersionId()) {
            player.sendTitle(BaseUtil.replaceChatColor(title), BaseUtil.replaceChatColor(subtitle));
            return;
        }
        if (BaseConstants.VERSION_ID > VersionCheckEnum.V_1_10.getVersionId()) {
            player.sendTitle(BaseUtil.replaceChatColor(title), BaseUtil.replaceChatColor(subtitle), fadein, stay, fadeout);
        }
    }

    /**
     * 发送全服title消息
     *
     * @param title    标题
     * @param subtitle 副标题
     * @param fadein   淡入时间
     * @param stay     存在时间
     * @param fadeout  淡出时间
     */
    @SuppressWarnings("deprecation")
    public static void sendAllTitle(String title, String subtitle, int fadein, int stay, int fadeout) {
        for (Player player : Bukkit.getOnlinePlayers()) {
            if (BaseConstants.VERSION_ID > VersionCheckEnum.V_1_8_8.getVersionId() && BaseConstants.VERSION_ID < VersionCheckEnum.V_1_11.getVersionId()) {
                player.sendTitle(BaseUtil.replaceChatColor(title), BaseUtil.replaceChatColor(subtitle));
                return;
            }
            if (BaseConstants.VERSION_ID > VersionCheckEnum.V_1_10.getVersionId()) {
                player.sendTitle(BaseUtil.replaceChatColor(title), BaseUtil.replaceChatColor(subtitle), fadein, stay, fadeout);
            }
        }
    }

    /**
     * 发送title消息
     *
     * @param player   玩家
     * @param title    标题
     * @param subtitle 副标题
     */
    @SuppressWarnings("deprecation")
    public static void sendTitle(Player player, String title, String subtitle) {
        if (BaseConstants.VERSION_ID < VersionCheckEnum.V_1_9.getVersionId()) {
            sendMessage(player, BaseUtil.replaceChatColor(title + subtitle));
            return;
        }
        player.sendTitle(BaseUtil.replaceChatColor(title), BaseUtil.replaceChatColor(subtitle));
    }

    /**
     * 发送全服title消息
     *
     * @param title    标题
     * @param subtitle 副标题
     */
    @SuppressWarnings("deprecation")
    public static void sendAllTitle(String title, String subtitle) {
        for (Player player : Bukkit.getOnlinePlayers()) {
            if (BaseConstants.VERSION_ID < VersionCheckEnum.V_1_9.getVersionId()) {
                sendMessage(player, BaseUtil.replaceChatColor(title + subtitle));
                return;
            }
            player.sendTitle(BaseUtil.replaceChatColor(title), BaseUtil.replaceChatColor(subtitle));
        }
    }

    /**
     * 发送Actionbar
     *
     * @param player 玩家
     * @param msg    消息
     */
    public static void sendActionbar(Player player, String msg) {
        if (StrUtil.isEmpty(msg)) {
            return;
        }
        if (BaseConstants.VERSION_ID < VersionCheckEnum.V_1_9.getVersionId()) {
            sendMessage(player, msg);
            return;
        }
        player.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(BaseUtil.replaceChatColor(msg)));
    }

    /**
     * 发送全服Actionbar
     *
     * @param msg 消息
     */
    public static void sendAllActionbar(String msg) {
        if (StrUtil.isEmpty(msg)) {
            return;
        }
        if (BaseConstants.VERSION_ID < VersionCheckEnum.V_1_9.getVersionId()) {
            sendAllMessage(msg);
            return;
        }
        for (Player player : Bukkit.getOnlinePlayers()) {
            player.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(BaseUtil.replaceChatColor(msg)));
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy