style(core-bungee): 修改模块名称
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
package cn.hamster3.mc.plugin.core.bungee;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.bungee.api.CoreBungeeAPI;
|
||||
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
||||
import net.kyori.adventure.platform.bungeecord.BungeeAudiences;
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class HamsterCorePlugin extends Plugin {
|
||||
private static HamsterCorePlugin instance;
|
||||
private BungeeAudiences audienceProvider;
|
||||
|
||||
public static HamsterCorePlugin getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoad() {
|
||||
long start = System.currentTimeMillis();
|
||||
instance = this;
|
||||
Logger logger = getLogger();
|
||||
logger.info("仓鼠核心正在初始化");
|
||||
CoreBungeeAPI.init();
|
||||
logger.info("已初始化 CoreAPI");
|
||||
long time = System.currentTimeMillis() - start;
|
||||
logger.info("仓鼠核心初始化完成,总计耗时 " + time + " ms");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
long start = System.currentTimeMillis();
|
||||
Logger logger = getLogger();
|
||||
logger.info("仓鼠核心正在启动");
|
||||
audienceProvider = BungeeAudiences.create(this);
|
||||
logger.info("已创建 AudienceProvider");
|
||||
long time = System.currentTimeMillis() - start;
|
||||
logger.info("仓鼠核心启动完成,总计耗时 " + time + " ms");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
long start = System.currentTimeMillis();
|
||||
Logger logger = getLogger();
|
||||
logger.info("仓鼠核心正在关闭");
|
||||
CoreBungeeAPI.getInstance().getDataSource().close();
|
||||
logger.info("已关闭数据库连接池");
|
||||
CoreAPI.getInstance().getExecutorService().shutdownNow();
|
||||
logger.info("已关闭线程池");
|
||||
CoreAPI.getInstance().getScheduledService().shutdownNow();
|
||||
logger.info("已关闭调度器");
|
||||
long time = System.currentTimeMillis() - start;
|
||||
logger.info("仓鼠核心已关闭,总计耗时 " + time + " ms");
|
||||
}
|
||||
|
||||
public BungeeAudiences getAudienceProvider() {
|
||||
return audienceProvider;
|
||||
}
|
||||
}
|
@@ -0,0 +1,92 @@
|
||||
package cn.hamster3.mc.plugin.core.bungee.api;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.bungee.HamsterCorePlugin;
|
||||
import cn.hamster3.mc.plugin.core.bungee.util.CoreBungeeCordUtils;
|
||||
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
||||
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
|
||||
import cn.hamster3.mc.plugin.core.common.util.serializer.ComponentTypeAdapter;
|
||||
import cn.hamster3.mc.plugin.core.common.util.serializer.MessageTypeAdapter;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import net.kyori.adventure.platform.AudienceProvider;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.md_5.bungee.config.Configuration;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public final class CoreBungeeAPI extends CoreAPI {
|
||||
private final Gson gson;
|
||||
private final Gson humanGson;
|
||||
private final HikariDataSource datasource;
|
||||
|
||||
private CoreBungeeAPI() {
|
||||
HamsterCorePlugin plugin = HamsterCorePlugin.getInstance();
|
||||
|
||||
Configuration config = CoreBungeeCordUtils.getPluginConfig(plugin);
|
||||
Configuration datasourceConfig = config.getSection("datasource");
|
||||
if (datasourceConfig == null) {
|
||||
throw new IllegalArgumentException("配置文件中未找到 datasource 节点");
|
||||
}
|
||||
|
||||
HikariConfig hikariConfig = new HikariConfig();
|
||||
hikariConfig.setDriverClassName(datasourceConfig.getString("driver"));
|
||||
hikariConfig.setJdbcUrl(datasourceConfig.getString("url"));
|
||||
hikariConfig.setUsername(datasourceConfig.getString("username"));
|
||||
hikariConfig.setPassword(datasourceConfig.getString("password"));
|
||||
hikariConfig.setMaximumPoolSize(datasourceConfig.getInt("maximum-pool-size", 3));
|
||||
hikariConfig.setMinimumIdle(datasourceConfig.getInt("minimum-idle", 1));
|
||||
long keepAliveTime = datasourceConfig.getLong("keep-alive-time", 0);
|
||||
if (keepAliveTime > 5000) {
|
||||
hikariConfig.setKeepaliveTime(keepAliveTime);
|
||||
}
|
||||
hikariConfig.setIdleTimeout(datasourceConfig.getLong("idle-timeout", 10 * 60 * 1000));
|
||||
hikariConfig.setMaxLifetime(datasourceConfig.getLong("max-lifetime", 30 * 60 * 1000));
|
||||
hikariConfig.setValidationTimeout(datasourceConfig.getLong("validation-timeout", 5000));
|
||||
hikariConfig.setPoolName("HamsterCore-Pool");
|
||||
datasource = new HikariDataSource(hikariConfig);
|
||||
|
||||
gson = new GsonBuilder()
|
||||
.registerTypeAdapter(Component.class, ComponentTypeAdapter.INSTANCE)
|
||||
.registerTypeAdapter(DisplayMessage.class, MessageTypeAdapter.INSTANCE)
|
||||
.create();
|
||||
humanGson = new GsonBuilder()
|
||||
.registerTypeAdapter(Component.class, ComponentTypeAdapter.INSTANCE)
|
||||
.registerTypeAdapter(DisplayMessage.class, MessageTypeAdapter.INSTANCE)
|
||||
.serializeNulls()
|
||||
.setPrettyPrinting()
|
||||
.create();
|
||||
}
|
||||
|
||||
public static CoreBungeeAPI getInstance() {
|
||||
return (CoreBungeeAPI) instance;
|
||||
}
|
||||
|
||||
public static void init() {
|
||||
if (instance != null) {
|
||||
return;
|
||||
}
|
||||
instance = new CoreBungeeAPI();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull AudienceProvider getAudienceProvider() {
|
||||
return HamsterCorePlugin.getInstance().getAudienceProvider();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull HikariDataSource getDataSource() {
|
||||
return datasource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Gson getGson() {
|
||||
return gson;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Gson getHumanGson() {
|
||||
return humanGson;
|
||||
}
|
||||
}
|
@@ -0,0 +1,86 @@
|
||||
package cn.hamster3.mc.plugin.core.bungee.util;
|
||||
|
||||
import net.md_5.bungee.api.plugin.Plugin;
|
||||
import net.md_5.bungee.config.Configuration;
|
||||
import net.md_5.bungee.config.ConfigurationProvider;
|
||||
import net.md_5.bungee.config.YamlConfiguration;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public final class CoreBungeeCordUtils {
|
||||
private CoreBungeeCordUtils() {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Configuration getConfig(@NotNull File file) {
|
||||
if (!file.exists()) {
|
||||
throw new IllegalArgumentException("文件不存在");
|
||||
}
|
||||
try {
|
||||
return ConfigurationProvider.getProvider(YamlConfiguration.class).load(file);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Configuration getPluginConfig(@NotNull Plugin plugin) {
|
||||
File configFile = new File(plugin.getDataFolder(), "config.yml");
|
||||
if (!configFile.exists()) {
|
||||
return saveDefaultConfig(plugin);
|
||||
}
|
||||
try {
|
||||
return ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Configuration getPluginConfig(@NotNull Plugin plugin, @NotNull String filename) {
|
||||
File configFile = new File(plugin.getDataFolder(), filename);
|
||||
if (!configFile.exists()) {
|
||||
return saveDefaultConfig(plugin, filename);
|
||||
}
|
||||
try {
|
||||
return ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Configuration saveDefaultConfig(@NotNull Plugin plugin) {
|
||||
if (plugin.getDataFolder().mkdir()) {
|
||||
plugin.getLogger().info("已生成插件存档文件夹");
|
||||
}
|
||||
File configFile = new File(plugin.getDataFolder(), "config.yml");
|
||||
try {
|
||||
InputStream in = plugin.getResourceAsStream("config.yml");
|
||||
Files.copy(in, configFile.toPath());
|
||||
return ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Configuration saveDefaultConfig(@NotNull Plugin plugin, @NotNull String filename) {
|
||||
if (plugin.getDataFolder().mkdir()) {
|
||||
plugin.getLogger().info("已生成插件存档文件夹");
|
||||
}
|
||||
File configFile = new File(plugin.getDataFolder(), filename);
|
||||
try {
|
||||
InputStream in = plugin.getResourceAsStream(filename);
|
||||
Files.copy(in, configFile.toPath());
|
||||
return ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
5
core-bungee/src/main/resources/bungee.yml
Normal file
5
core-bungee/src/main/resources/bungee.yml
Normal file
@@ -0,0 +1,5 @@
|
||||
name: HamsterCore
|
||||
main: cn.hamster3.mc.plugin.core.bungee.HamsterCorePlugin
|
||||
version: ${version}
|
||||
|
||||
author: MiniDay
|
33
core-bungee/src/main/resources/config.yml
Normal file
33
core-bungee/src/main/resources/config.yml
Normal file
@@ -0,0 +1,33 @@
|
||||
datasource:
|
||||
# 数据库链接驱动地址
|
||||
# 除非你知道自己在做什么,否则不建议更改该项
|
||||
driver: "com.mysql.cj.jdbc.Driver"
|
||||
# 数据库链接填写格式:
|
||||
# jdbc:mysql://{数据库地址}:{数据库端口}/{使用的库名}?参数
|
||||
# 除非你知道自己在做什么,否则不建议随意更改参数
|
||||
url: "jdbc:mysql://sql.hamster3.cn:3306/Test1?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true"
|
||||
# 用户名
|
||||
username: "Test"
|
||||
# 密码
|
||||
password: "Test123.."
|
||||
# 最小闲置链接数
|
||||
# 推荐值:1~3
|
||||
minimum-idle: 0
|
||||
# 最大链接数
|
||||
# 推荐值:不低于5
|
||||
maximum-pool-size: 5
|
||||
# 保持连接池可用的间隔
|
||||
# 除非你的服务器数据库连接经常断开,否则不建议启用该选项
|
||||
# 单位:毫秒
|
||||
# 默认值为0(禁用)
|
||||
keep-alive-time: 0
|
||||
# 连接闲置回收时间
|
||||
# 单位:毫秒
|
||||
# 推荐值:600000(10分钟)
|
||||
idle-timeout: 600000
|
||||
# 链接最长存活时间
|
||||
# 单位:毫秒
|
||||
max-lifetime: 1800000
|
||||
# 验证连接存活的超时时间
|
||||
# 单位:毫秒
|
||||
validation-timeout: 5000
|
Reference in New Issue
Block a user