feat: 添加 velocity 支持,取消重定向包名
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
package cn.hamster3.mc.plugin.core.velocity;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
||||
import com.google.inject.Inject;
|
||||
import com.velocitypowered.api.event.Subscribe;
|
||||
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
|
||||
import com.velocitypowered.api.event.proxy.ProxyShutdownEvent;
|
||||
import com.velocitypowered.api.plugin.Plugin;
|
||||
import com.velocitypowered.api.plugin.annotation.DataDirectory;
|
||||
import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import lombok.Getter;
|
||||
import cn.hamster3.mc.plugin.core.velocity.api.CoreVelocityAPI;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.Objects;
|
||||
|
||||
@Plugin(
|
||||
id = "airgame-core",
|
||||
name = "AirGameCore",
|
||||
version = BuildConstants.VERSION,
|
||||
description = BuildConstants.DESCRIPTION,
|
||||
authors = {"MiniDay"}
|
||||
)
|
||||
public class AirGameCorePlugin {
|
||||
@Getter
|
||||
private static AirGameCorePlugin instance;
|
||||
@Getter
|
||||
private final java.util.logging.Logger logger;
|
||||
@Getter
|
||||
private final Logger slf4jLogger;
|
||||
@Getter
|
||||
private final ProxyServer proxyServer;
|
||||
@Getter
|
||||
private final File dataFolder;
|
||||
|
||||
@Inject
|
||||
public AirGameCorePlugin(Logger slf4jLogger, ProxyServer proxyServer, @DataDirectory Path dataPath) {
|
||||
logger = java.util.logging.Logger.getLogger("airgame-core");
|
||||
this.slf4jLogger = slf4jLogger;
|
||||
this.proxyServer = proxyServer;
|
||||
dataFolder = dataPath.toFile();
|
||||
instance = this;
|
||||
long start = System.currentTimeMillis();
|
||||
try {
|
||||
if (dataFolder.mkdir()) {
|
||||
slf4jLogger.info("已生成插件存档文件夹");
|
||||
}
|
||||
File configFile = new File(dataFolder, "config.yml");
|
||||
if (!configFile.exists()) {
|
||||
Files.copy(
|
||||
Objects.requireNonNull(AirGameCorePlugin.class.getResourceAsStream("/config.yml")),
|
||||
configFile.toPath(),
|
||||
StandardCopyOption.REPLACE_EXISTING
|
||||
);
|
||||
}
|
||||
CoreVelocityAPI.init(configFile);
|
||||
slf4jLogger.info("已初始化 CoreAPI");
|
||||
} catch (Exception e) {
|
||||
slf4jLogger.error("初始化 CoreAPI 出错", e);
|
||||
}
|
||||
long time = System.currentTimeMillis() - start;
|
||||
slf4jLogger.info("AirGameCore 初始化完成,总计耗时 " + time + " ms");
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onProxyInitialization(ProxyInitializeEvent event) {
|
||||
long start = System.currentTimeMillis();
|
||||
slf4jLogger.info("仓鼠核心正在启动");
|
||||
// CoreMessage.init(getLogger(), new File(dataFolder, "messages.yml"));
|
||||
// logger.info("已初始化语言文本");
|
||||
long time = System.currentTimeMillis() - start;
|
||||
slf4jLogger.info("仓鼠核心启动完成,总计耗时 " + time + " ms");
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onProxyInitialization(ProxyShutdownEvent event) {
|
||||
long start = System.currentTimeMillis();
|
||||
CoreAPI.getInstance().getJedisPool().close();
|
||||
slf4jLogger.info("已关闭 Redis 连接池");
|
||||
if (CoreAPI.getInstance().getDataSource() instanceof HikariDataSource dataSource) {
|
||||
dataSource.close();
|
||||
slf4jLogger.info("已关闭数据库连接池");
|
||||
}
|
||||
CoreAPI.getInstance().getExecutorService().shutdownNow();
|
||||
slf4jLogger.info("已关闭 ExecutorService 线程池");
|
||||
CoreAPI.getInstance().getScheduledService().shutdownNow();
|
||||
slf4jLogger.info("已关闭 ScheduledExecutorService 线程池");
|
||||
long time = System.currentTimeMillis() - start;
|
||||
slf4jLogger.info("AirGameCore 关闭完成,总计耗时 " + time + " ms");
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,73 @@
|
||||
package cn.hamster3.mc.plugin.core.velocity.api;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
||||
import cn.hamster3.mc.plugin.core.common.config.ConfigSection;
|
||||
import cn.hamster3.mc.plugin.core.common.config.YamlConfig;
|
||||
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
|
||||
import cn.hamster3.mc.plugin.core.common.impl.ComponentTypeAdapter;
|
||||
import cn.hamster3.mc.plugin.core.common.impl.MessageTypeAdapter;
|
||||
import cn.hamster3.mc.plugin.core.velocity.AirGameCorePlugin;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import cn.hamster3.mc.plugin.core.velocity.impl.AudienceProviderImpl;
|
||||
import net.kyori.adventure.platform.AudienceProvider;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public final class CoreVelocityAPI extends CoreAPI {
|
||||
@NotNull
|
||||
private final Gson gson;
|
||||
@NotNull
|
||||
private final Gson gsonHuman;
|
||||
|
||||
public CoreVelocityAPI(@NotNull ConfigSection config) {
|
||||
super(config);
|
||||
gson = new GsonBuilder()
|
||||
.registerTypeAdapter(Component.class, ComponentTypeAdapter.INSTANCE)
|
||||
.registerTypeAdapter(DisplayMessage.class, MessageTypeAdapter.INSTANCE)
|
||||
.create();
|
||||
gsonHuman = new GsonBuilder()
|
||||
.registerTypeAdapter(Component.class, ComponentTypeAdapter.INSTANCE)
|
||||
.registerTypeAdapter(DisplayMessage.class, MessageTypeAdapter.INSTANCE)
|
||||
.serializeNulls()
|
||||
.setPrettyPrinting()
|
||||
.create();
|
||||
}
|
||||
|
||||
public static CoreVelocityAPI getInstance() {
|
||||
return (CoreVelocityAPI) instance;
|
||||
}
|
||||
|
||||
public static void init(@NotNull File configFile) throws IOException {
|
||||
if (instance != null) {
|
||||
return;
|
||||
}
|
||||
YamlConfig config = YamlConfig.load(configFile);
|
||||
instance = new CoreVelocityAPI(config);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull AudienceProvider getAudienceProvider() {
|
||||
return AudienceProviderImpl.INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Logger getLogger() {
|
||||
return AirGameCorePlugin.getInstance().getLogger();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Gson getGson() {
|
||||
return gson;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Gson getGsonHuman() {
|
||||
return gsonHuman;
|
||||
}
|
||||
}
|
@@ -0,0 +1,67 @@
|
||||
package cn.hamster3.mc.plugin.core.velocity.impl;
|
||||
|
||||
import cn.hamster3.mc.plugin.core.velocity.AirGameCorePlugin;
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.platform.AudienceProvider;
|
||||
import net.kyori.adventure.text.flattener.ComponentFlattener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class AudienceProviderImpl implements AudienceProvider {
|
||||
public static final AudienceProviderImpl INSTANCE = new AudienceProviderImpl();
|
||||
|
||||
private AudienceProviderImpl() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Audience all() {
|
||||
return Audience.audience(console(), players());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Audience console() {
|
||||
return AirGameCorePlugin.getInstance().getProxyServer().getConsoleCommandSource();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Audience players() {
|
||||
return Audience.audience(AirGameCorePlugin.getInstance().getProxyServer().getAllPlayers());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Audience player(@NotNull UUID playerId) {
|
||||
Player player = AirGameCorePlugin.getInstance().getProxyServer().getPlayer(playerId).orElse(null);
|
||||
if (player == null) {
|
||||
return Audience.empty();
|
||||
}
|
||||
return player;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Audience permission(@NotNull String permission) {
|
||||
return Audience.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Audience world(@NotNull Key world) {
|
||||
return Audience.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Audience server(@NotNull String serverName) {
|
||||
return Audience.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ComponentFlattener flattener() {
|
||||
return ComponentFlattener.basic();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
|
||||
}
|
||||
}
|
47
core-velocity/src/main/resources/config.yml
Normal file
47
core-velocity/src/main/resources/config.yml
Normal file
@@ -0,0 +1,47 @@
|
||||
# redis 连接配置
|
||||
# 完整格式如下:
|
||||
# redis://用户名:密码@主机名:端口/数据库索引?参数名=参数值&参数名=参数值
|
||||
# 若没有设置 redis 用户名,但设置了密码,则可以使用以下格式:
|
||||
# redis://密码@localhost:6379/0?clientName=AirGameCore
|
||||
# 若没有设置 redis 用户名,也没有设置密码,则可以使用以下格式:
|
||||
# redis://localhost:6379/0?clientName=AirGameCore
|
||||
# 若不设置数据库,则默认使用 0
|
||||
redis-url: "redis://localhost:6379/0?clientName=AirGameCore&timeout=5s"
|
||||
|
||||
datasource:
|
||||
# 数据库链接驱动地址
|
||||
# 除非你知道自己在做什么,否则不建议更改该项
|
||||
# 旧版服务端(低于1.13)请使用:com.mysql.jdbc.Driver
|
||||
driver: "com.mysql.cj.jdbc.Driver"
|
||||
# 数据库链接填写格式:
|
||||
# jdbc:mysql://{数据库地址}:{数据库端口}/{使用的库名}?参数
|
||||
# 除非你知道自己在做什么,否则不建议随意更改参数
|
||||
url: "jdbc:mysql://localhost:3306/Test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true"
|
||||
# 如果你不需要做多端跨服,那么请使用 sqlite 作本地数据库
|
||||
# driver: "org.sqlite.JDBC"
|
||||
# url: "jdbc:sqlite:./plugins/AirGameCore/database.db"
|
||||
# 用户名
|
||||
username: "root"
|
||||
# 密码
|
||||
password: "Root123.."
|
||||
# 最小闲置链接数
|
||||
# 推荐值:1~3
|
||||
minimum-idle: 0
|
||||
# 最大链接数
|
||||
# 推荐值:不低于3
|
||||
maximum-pool-size: 3
|
||||
# 保持连接池可用的间隔
|
||||
# 除非你的服务器数据库连接经常断开,否则不建议启用该选项
|
||||
# 单位:毫秒
|
||||
# 默认值为0(禁用)
|
||||
keep-alive-time: 0
|
||||
# 连接闲置回收时间
|
||||
# 单位:毫秒
|
||||
# 推荐值:600000(10分钟)
|
||||
idle-timeout: 600000
|
||||
# 链接最长存活时间
|
||||
# 单位:毫秒
|
||||
max-lifetime: 1800000
|
||||
# 验证连接存活的超时时间
|
||||
# 单位:毫秒
|
||||
validation-timeout: 5000
|
6
core-velocity/src/main/resources/jenkins.yml
Normal file
6
core-velocity/src/main/resources/jenkins.yml
Normal file
@@ -0,0 +1,6 @@
|
||||
BUILD_ID: ${BUILD_ID}
|
||||
BUILD_NUMBER: ${BUILD_NUMBER}
|
||||
BUILD_DISPLAY_NAME: ${BUILD_DISPLAY_NAME}
|
||||
JOB_URL: ${JOB_URL}
|
||||
BUILD_URL: ${BUILD_URL}
|
||||
GIT_COMMIT: ${GIT_COMMIT}
|
@@ -0,0 +1,8 @@
|
||||
package cn.hamster3.mc.plugin.core.velocity;
|
||||
|
||||
// The constants are replaced before compilation
|
||||
@SuppressWarnings("unused")
|
||||
public class BuildConstants {
|
||||
public static final String VERSION = "${version}";
|
||||
public static final String DESCRIPTION = "${description}";
|
||||
}
|
Reference in New Issue
Block a user