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

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

The newest version!
package cn.handyplus.lib.util;

import cn.handyplus.lib.constants.BaseConstants;
import cn.handyplus.lib.constants.VersionCheckEnum;
import cn.handyplus.lib.core.StrUtil;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.chat.hover.content.Text;

/**
 * TextComponent 的封装
 *
 * @author handy
 * @since 2.7.3
 */
public class TextUtil {

    private TextComponent textComponent;

    private TextUtil() {
    }

    /**
     * 获取实例
     *
     * @return 实例
     */
    public static TextUtil getInstance() {
        return new TextUtil();
    }

    /**
     * 初始化
     *
     * @param msg 消息内容
     * @return this
     */
    public TextUtil init(String msg) {
        return init(msg, true);
    }

    /**
     * 初始化
     *
     * @param msg     消息内容
     * @param isColor 是否加载颜色代码
     * @return this
     * @since 3.6.8
     */
    public TextUtil init(String msg, boolean isColor) {
        textComponent = new TextComponent(isColor ? BaseUtil.replaceChatColor(msg) : msg);
        return this;
    }

    /**
     * 添加鼠标点击事件
     *
     * @param action 類型
     * @param msg    消息
     * @return this
     * @since 3.0.9
     */
    public TextUtil addClick(ClickEvent.Action action, String msg) {
        textComponent.setClickEvent(new ClickEvent(action, msg));
        return this;
    }

    /**
     * 添加鼠标点击打开url
     *
     * @param url url
     * @return this
     */
    public TextUtil addClickUrl(String url) {
        return addClick(ClickEvent.Action.OPEN_URL, url);
    }

    /**
     * 添加鼠标点击执行命令
     *
     * @param command 命令
     * @return this
     */
    public TextUtil addClickCommand(String command) {
        return addClick(ClickEvent.Action.RUN_COMMAND, command);
    }

    /**
     * 添加鼠标点击 将给定的字符串插入到玩家的文本框中。
     *
     * @param suggestCommand 字符串
     * @return this
     * @since 3.0.9
     */
    public TextUtil addClickSuggestCommand(String suggestCommand) {
        return addClick(ClickEvent.Action.SUGGEST_COMMAND, suggestCommand);
    }

    /**
     * 添加文本内容复制到剪贴板 1.15+可用
     *
     * @param text 文字
     * @return this
     * @since 3.4.9
     */
    public TextUtil addClickCopyToClipboard(String text) {
        if (BaseConstants.VERSION_ID < VersionCheckEnum.V_1_15.getVersionId()) {
            return this;
        }
        return addClick(ClickEvent.Action.COPY_TO_CLIPBOARD, BaseUtil.replaceChatColor(text));
    }

    /**
     * 添加鼠标移动上去显示效果 1.16+可用
     *
     * @param text 文字
     * @return this
     */
    public TextUtil addHoverText(String text) {
        if (BaseConstants.VERSION_ID < VersionCheckEnum.V_1_16.getVersionId()) {
            return this;
        }
        if (StrUtil.isEmpty(text)) {
            return this;
        }
        textComponent.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(BaseUtil.replaceChatColor(text))));
        return this;
    }

    /**
     * 添加组件
     *
     * @param extra 要附加的组件
     * @return this
     * @since 3.6.8
     */
    public TextUtil addExtra(TextComponent extra) {
        textComponent.addExtra(extra);
        return this;
    }

    /**
     * 获取 TextComponent
     *
     * @return TextComponent
     */
    public TextComponent build() {
        return this.textComponent;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy