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

cn.handyplus.lib.InitApi Maven / Gradle / Ivy

The newest version!
package cn.handyplus.lib;

import cn.handyplus.lib.adapter.HandySchedulerUtil;
import cn.handyplus.lib.annotation.TableName;
import cn.handyplus.lib.command.HandyCommandWrapper;
import cn.handyplus.lib.constants.BaseConstants;
import cn.handyplus.lib.constants.VersionCheckEnum;
import cn.handyplus.lib.core.ClassUtil;
import cn.handyplus.lib.core.CollUtil;
import cn.handyplus.lib.db.Db;
import cn.handyplus.lib.db.SqlManagerUtil;
import cn.handyplus.lib.inventory.HandyInventoryWrapper;
import cn.handyplus.lib.metrics.Metrics;
import cn.handyplus.lib.metrics.MetricsPluginNameCallable;
import cn.handyplus.lib.metrics.MetricsVersionCallable;
import cn.handyplus.lib.util.BcUtil;
import cn.handyplus.lib.util.HandyHttpUtil;
import lombok.SneakyThrows;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.List;

/**
 * 初始化
 *
 * @author handy
 * @since 1.2.0
 */
public class InitApi {
    private final static String VERSION = "3.10.9";
    private static final InitApi INSTANCE = new InitApi();
    public static JavaPlugin PLUGIN;
    private static ClassUtil CLASS_UTIL;

    private InitApi() {
    }

    /**
     * 获取唯一实例
     *
     * @param plugin 插件
     * @return this
     */
    public static InitApi getInstance(JavaPlugin plugin) {
        PLUGIN = plugin;
        BaseConstants.VERSION_CHECK_ENUM = VersionCheckEnum.getEnum();
        BaseConstants.VERSION_ID = BaseConstants.VERSION_CHECK_ENUM.getVersionId();
        CLASS_UTIL = new ClassUtil();
        handyMetrics();
        HandySchedulerUtil.init(plugin);
        return INSTANCE;
    }

    /**
     * 命令注入器
     *
     * @param packageName 扫描的包名
     * @return this
     */
    @SneakyThrows
    public InitApi initCommand(String packageName) {
        HandyCommandWrapper.initCommand(packageName);
        return this;
    }

    /**
     * 监听器注入
     *
     * @param packageName 扫描的包名
     * @return this
     */
    public InitApi initListener(String packageName) {
        return this.initListener(packageName, null);
    }

    /**
     * 监听器注入
     *
     * @param packageName 扫描的包名
     * @param ignoreList  忽视的类
     * @return this
     */
    @SneakyThrows
    public InitApi initListener(String packageName, List ignoreList) {
        HandyInventoryWrapper.initListener(packageName, ignoreList);
        return this;
    }

    /**
     * 背包事件处理器注入
     *
     * @param packageName 扫描的包名
     * @return this
     */
    @SneakyThrows
    public InitApi initClickEvent(String packageName) {
        HandyInventoryWrapper.initClickEvent(packageName);
        return this;
    }

    /**
     * bStats进行插件使用数据统计
     */
    private static void handyMetrics() {
        try {
            Metrics metrics = new Metrics(PLUGIN, 12612);
            metrics.addCustomChart(new Metrics.SimplePie("version", new MetricsVersionCallable(VERSION)));
            metrics.addCustomChart(new Metrics.SimplePie("pluginName", new MetricsPluginNameCallable(PLUGIN.getName())));
        } catch (Throwable ignored) {
        }
    }

    /**
     * 初始化版本更新提醒
     *
     * @param isVersion 是否提醒
     * @return this
     */
    public InitApi checkVersion(boolean isVersion) {
        if (isVersion) {
            HandyHttpUtil.checkVersion(null);
        }
        return this;
    }

    /**
     * 进行插件使用数据统计
     *
     * @param pluginId 插件id
     * @return this
     */
    public InitApi addMetrics(int pluginId) {
        return addMetrics(pluginId, null);
    }

    /**
     * 进行插件使用数据统计
     *
     * @param pluginId     插件id
     * @param customCharts 自定义图表
     * @return this
     * @since 1.7.9
     */
    public InitApi addMetrics(int pluginId, List customCharts) {
        try {
            // bStats进行插件使用数据统计
            Metrics metrics = new Metrics(PLUGIN, pluginId);
            // 添加自定义图表
            if (CollUtil.isNotEmpty(customCharts)) {
                for (Metrics.CustomChart customChart : customCharts) {
                    metrics.addCustomChart(customChart);
                }
            }
        } catch (Throwable ignored) {
        }
        return this;
    }

    /**
     * 存储初始化
     *
     * @param packageName 扫描的包名
     * @return this
     * @since 1.4.8
     */
    public InitApi enableSql(String packageName) {
        // 实体类
        List> tableList = CLASS_UTIL.getClassByAnnotation(packageName, TableName.class);
        if (CollUtil.isEmpty(tableList)) {
            return this;
        }
        // 初始化链接池
        SqlManagerUtil.enableSql();
        // 建表
        for (Class aClass : tableList) {
            Db.use(aClass).createTable();
        }
        return this;
    }

    /**
     * 注册bc通道
     *
     * @return this
     * @since 3.5.3
     */
    public InitApi enableBc() {
        BcUtil.registerOut();
        return this;
    }

    /**
     * 关闭事件
     */
    public static void disable() {
        for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
            onlinePlayer.closeInventory();
        }
        SqlManagerUtil.getInstance().close();
        HandySchedulerUtil.cancelTask();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy