perf: 简化代码
This commit is contained in:
@@ -8,7 +8,7 @@ dependencies {
|
|||||||
}
|
}
|
||||||
compileOnly("org.spigotmc:spigot-api:1.18.2-R0.1-SNAPSHOT")
|
compileOnly("org.spigotmc:spigot-api:1.18.2-R0.1-SNAPSHOT")
|
||||||
|
|
||||||
compileOnly("cn.hamster3.mc.plugin:core-bukkit:1.3.0")
|
compileOnly("cn.hamster3.mc.plugin:core-bukkit:+")
|
||||||
compileOnly("me.clip:placeholderapi:2.11.5") {
|
compileOnly("me.clip:placeholderapi:2.11.5") {
|
||||||
isTransitive = false
|
isTransitive = false
|
||||||
}
|
}
|
||||||
|
@@ -14,6 +14,10 @@ import org.bukkit.Bukkit;
|
|||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.StandardCopyOption;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@SuppressWarnings("CallToPrintStackTrace")
|
@SuppressWarnings("CallToPrintStackTrace")
|
||||||
@@ -39,7 +43,19 @@ public class HamsterBallPlugin extends JavaPlugin {
|
|||||||
reloadConfig();
|
reloadConfig();
|
||||||
logger.info("已读取配置文件");
|
logger.info("已读取配置文件");
|
||||||
try {
|
try {
|
||||||
BallBukkitAPI.init();
|
File dataFolder = getDataFolder();
|
||||||
|
if (dataFolder.mkdir()) {
|
||||||
|
logger.info("已生成插件存档文件夹");
|
||||||
|
}
|
||||||
|
File configFile = new File(dataFolder, "config.yml");
|
||||||
|
if (!configFile.exists()) {
|
||||||
|
Files.copy(
|
||||||
|
Objects.requireNonNull(getResource("config.yml")),
|
||||||
|
configFile.toPath(),
|
||||||
|
StandardCopyOption.REPLACE_EXISTING
|
||||||
|
);
|
||||||
|
}
|
||||||
|
BallBukkitAPI.init(configFile);
|
||||||
logger.info("已初始化 BallAPI");
|
logger.info("已初始化 BallAPI");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@@ -1,69 +1,31 @@
|
|||||||
package cn.hamster3.mc.plugin.ball.bukkit.api;
|
package cn.hamster3.mc.plugin.ball.bukkit.api;
|
||||||
|
|
||||||
import cn.hamster3.mc.plugin.ball.bukkit.HamsterBallPlugin;
|
import cn.hamster3.mc.plugin.ball.bukkit.HamsterBallPlugin;
|
||||||
import cn.hamster3.mc.plugin.ball.bukkit.util.BallBukkitUtils;
|
|
||||||
import cn.hamster3.mc.plugin.ball.common.api.BallAPI;
|
import cn.hamster3.mc.plugin.ball.common.api.BallAPI;
|
||||||
import cn.hamster3.mc.plugin.ball.common.config.BallConfig;
|
import cn.hamster3.mc.plugin.core.common.config.ConfigSection;
|
||||||
import cn.hamster3.mc.plugin.ball.common.entity.BallServerInfo;
|
import cn.hamster3.mc.plugin.core.common.config.YamlConfig;
|
||||||
import cn.hamster3.mc.plugin.ball.common.entity.BallServerType;
|
|
||||||
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
public class BallBukkitAPI extends BallAPI {
|
public class BallBukkitAPI extends BallAPI {
|
||||||
public BallBukkitAPI(@NotNull BallConfig ballConfig) {
|
public BallBukkitAPI(@NotNull ConfigSection config) {
|
||||||
super(ballConfig);
|
super(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static BallBukkitAPI getInstance() {
|
public static BallBukkitAPI getInstance() {
|
||||||
return (BallBukkitAPI) instance;
|
return (BallBukkitAPI) instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void init() {
|
public static void init(@NotNull File configFile) throws IOException {
|
||||||
if (instance != null) {
|
if (instance != null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
HamsterBallPlugin plugin = HamsterBallPlugin.getInstance();
|
YamlConfig config = YamlConfig.load(configFile);
|
||||||
plugin.saveDefaultConfig();
|
instance = new BallBukkitAPI(config);
|
||||||
plugin.reloadConfig();
|
|
||||||
|
|
||||||
FileConfiguration config = plugin.getConfig();
|
|
||||||
Map<String, String> env = System.getenv();
|
|
||||||
|
|
||||||
BallServerInfo serverInfo = new BallServerInfo(
|
|
||||||
env.getOrDefault("BALL_LOCAL_SERVER_INFO_ID", config.getString("server-info.id")),
|
|
||||||
env.getOrDefault("BALL_LOCAL_SERVER_INFO_NAME", config.getString("server-info.name")),
|
|
||||||
BallServerType.GAME,
|
|
||||||
env.getOrDefault("BALL_LOCAL_SERVER_IP", config.getString("server-info.host", Bukkit.getIp())),
|
|
||||||
Integer.parseInt(
|
|
||||||
env.getOrDefault("BALL_LOCAL_SERVER_PORT", String.valueOf(config.getInt("server-info.port", Bukkit.getPort())))
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
DataSource datasource;
|
|
||||||
if (config.contains("datasource")) {
|
|
||||||
plugin.getLogger().info("启用仓鼠球自定义数据库连接池");
|
|
||||||
datasource = BallBukkitUtils.getDataSource(config.getConfigurationSection("datasource"));
|
|
||||||
} else {
|
|
||||||
plugin.getLogger().info("复用 HamsterCore 的数据库连接池");
|
|
||||||
datasource = CoreAPI.getInstance().getDataSource();
|
|
||||||
}
|
|
||||||
|
|
||||||
BallConfig ballConfig = new BallConfig(
|
|
||||||
config.getBoolean("debug", false),
|
|
||||||
config.getString("channel-prefix", "") + ":",
|
|
||||||
config.getBoolean("game-server-update-player-info", false),
|
|
||||||
config.getStringList("load-player-info-filter"),
|
|
||||||
serverInfo,
|
|
||||||
datasource
|
|
||||||
);
|
|
||||||
instance = new BallBukkitAPI(ballConfig);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -5,13 +5,8 @@ import cn.hamster3.mc.plugin.ball.common.entity.BallPlayerInfo;
|
|||||||
import cn.hamster3.mc.plugin.ball.common.event.BallActions;
|
import cn.hamster3.mc.plugin.ball.common.event.BallActions;
|
||||||
import cn.hamster3.mc.plugin.ball.common.event.player.BallPlayerInfoUpdateEvent;
|
import cn.hamster3.mc.plugin.ball.common.event.player.BallPlayerInfoUpdateEvent;
|
||||||
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
||||||
import cn.hamster3.mc.plugin.core.lib.com.zaxxer.hikari.HikariConfig;
|
|
||||||
import cn.hamster3.mc.plugin.core.lib.com.zaxxer.hikari.HikariDataSource;
|
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
@@ -21,34 +16,6 @@ public final class BallBukkitUtils {
|
|||||||
private BallBukkitUtils() {
|
private BallBukkitUtils() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public static DataSource getDataSource(@Nullable ConfigurationSection datasourceConfig) {
|
|
||||||
if (datasourceConfig == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
HikariConfig hikariConfig = new HikariConfig();
|
|
||||||
String driver = datasourceConfig.getString("driver");
|
|
||||||
hikariConfig.setDriverClassName(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("HamsterBall-Pool");
|
|
||||||
return new HikariDataSource(hikariConfig);
|
|
||||||
} catch (Exception | Error e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void uploadPlayerInfo(@NotNull BallPlayerInfo playerInfo) {
|
public static void uploadPlayerInfo(@NotNull BallPlayerInfo playerInfo) {
|
||||||
CoreAPI.getInstance().getExecutorService().execute(() -> {
|
CoreAPI.getInstance().getExecutorService().execute(() -> {
|
||||||
try (Connection connection = BallAPI.getInstance().getDatasource().getConnection()) {
|
try (Connection connection = BallAPI.getInstance().getDatasource().getConnection()) {
|
||||||
|
@@ -8,7 +8,7 @@ dependencies {
|
|||||||
}
|
}
|
||||||
compileOnly("net.md-5:bungeecord-api:1.20-R0.1")
|
compileOnly("net.md-5:bungeecord-api:1.20-R0.1")
|
||||||
|
|
||||||
compileOnly("cn.hamster3.mc.plugin:core-bungee:1.3.0")
|
compileOnly("cn.hamster3.mc.plugin:core-bungee:+")
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks {
|
tasks {
|
||||||
|
@@ -13,6 +13,9 @@ import lombok.Getter;
|
|||||||
import net.md_5.bungee.api.ProxyServer;
|
import net.md_5.bungee.api.ProxyServer;
|
||||||
import net.md_5.bungee.api.plugin.Plugin;
|
import net.md_5.bungee.api.plugin.Plugin;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.StandardCopyOption;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@SuppressWarnings("CallToPrintStackTrace")
|
@SuppressWarnings("CallToPrintStackTrace")
|
||||||
@@ -27,7 +30,19 @@ public class HamsterBallPlugin extends Plugin {
|
|||||||
logger.info("仓鼠球正在初始化");
|
logger.info("仓鼠球正在初始化");
|
||||||
instance = this;
|
instance = this;
|
||||||
try {
|
try {
|
||||||
BallBungeeCordAPI.init();
|
File dataFolder = getDataFolder();
|
||||||
|
if (dataFolder.mkdir()) {
|
||||||
|
logger.info("已生成插件存档文件夹");
|
||||||
|
}
|
||||||
|
File configFile = new File(dataFolder, "config.yml");
|
||||||
|
if (!configFile.exists()) {
|
||||||
|
Files.copy(
|
||||||
|
getResourceAsStream("config.yml"),
|
||||||
|
configFile.toPath(),
|
||||||
|
StandardCopyOption.REPLACE_EXISTING
|
||||||
|
);
|
||||||
|
}
|
||||||
|
BallBungeeCordAPI.init(configFile);
|
||||||
logger.info("已初始化 BallAPI");
|
logger.info("已初始化 BallAPI");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@@ -1,66 +1,31 @@
|
|||||||
package cn.hamster3.mc.plugin.ball.bungee.api;
|
package cn.hamster3.mc.plugin.ball.bungee.api;
|
||||||
|
|
||||||
import cn.hamster3.mc.plugin.ball.bungee.HamsterBallPlugin;
|
import cn.hamster3.mc.plugin.ball.bungee.HamsterBallPlugin;
|
||||||
import cn.hamster3.mc.plugin.ball.bungee.util.BallBungeeCordUtils;
|
|
||||||
import cn.hamster3.mc.plugin.ball.common.api.BallAPI;
|
import cn.hamster3.mc.plugin.ball.common.api.BallAPI;
|
||||||
import cn.hamster3.mc.plugin.ball.common.config.BallConfig;
|
import cn.hamster3.mc.plugin.core.common.config.ConfigSection;
|
||||||
import cn.hamster3.mc.plugin.ball.common.entity.BallServerInfo;
|
import cn.hamster3.mc.plugin.core.common.config.YamlConfig;
|
||||||
import cn.hamster3.mc.plugin.ball.common.entity.BallServerType;
|
|
||||||
import cn.hamster3.mc.plugin.core.bungee.util.CoreBungeeCordUtils;
|
|
||||||
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
|
||||||
import net.md_5.bungee.config.Configuration;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
public class BallBungeeCordAPI extends BallAPI {
|
public class BallBungeeCordAPI extends BallAPI {
|
||||||
public BallBungeeCordAPI(@NotNull BallConfig ballConfig) {
|
public BallBungeeCordAPI(@NotNull ConfigSection config) {
|
||||||
super(ballConfig);
|
super(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static BallBungeeCordAPI getInstance() {
|
public static BallBungeeCordAPI getInstance() {
|
||||||
return (BallBungeeCordAPI) instance;
|
return (BallBungeeCordAPI) instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void init() {
|
public static void init(@NotNull File configFile) throws IOException {
|
||||||
if (instance != null) {
|
if (instance != null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
HamsterBallPlugin plugin = HamsterBallPlugin.getInstance();
|
YamlConfig config = YamlConfig.load(configFile);
|
||||||
Configuration config = CoreBungeeCordUtils.getPluginConfig(plugin);
|
instance = new BallBungeeCordAPI(config);
|
||||||
Map<String, String> env = System.getenv();
|
|
||||||
|
|
||||||
BallServerInfo serverInfo = new BallServerInfo(
|
|
||||||
env.getOrDefault("BALL_LOCAL_SERVER_INFO_ID", config.getString("server-info.id")),
|
|
||||||
env.getOrDefault("BALL_LOCAL_SERVER_INFO_NAME", config.getString("server-info.name")),
|
|
||||||
BallServerType.PROXY,
|
|
||||||
env.getOrDefault("BALL_LOCAL_SERVER_IP", config.getString("server-info.host", "0.0.0.0")),
|
|
||||||
Integer.parseInt(
|
|
||||||
env.getOrDefault("BALL_LOCAL_SERVER_PORT", config.getString("server-info.port", "25577"))
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
DataSource datasource;
|
|
||||||
if (config.contains("datasource")) {
|
|
||||||
plugin.getLogger().info("启用仓鼠球自定义数据库连接池");
|
|
||||||
datasource = BallBungeeCordUtils.getDataSource(config.getSection("datasource"));
|
|
||||||
} else {
|
|
||||||
plugin.getLogger().info("复用 HamsterCore 的数据库连接池");
|
|
||||||
datasource = CoreAPI.getInstance().getDataSource();
|
|
||||||
}
|
|
||||||
|
|
||||||
BallConfig ballConfig = new BallConfig(
|
|
||||||
config.getBoolean("debug", false),
|
|
||||||
config.getString("channel-prefix", "") + ":",
|
|
||||||
config.getBoolean("game-server-update-player-info", false),
|
|
||||||
config.getStringList("load-player-info-filter"),
|
|
||||||
serverInfo,
|
|
||||||
datasource
|
|
||||||
);
|
|
||||||
instance = new BallBungeeCordAPI(ballConfig);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -5,15 +5,10 @@ import cn.hamster3.mc.plugin.ball.common.entity.BallPlayerInfo;
|
|||||||
import cn.hamster3.mc.plugin.ball.common.event.BallActions;
|
import cn.hamster3.mc.plugin.ball.common.event.BallActions;
|
||||||
import cn.hamster3.mc.plugin.ball.common.event.player.BallPlayerInfoUpdateEvent;
|
import cn.hamster3.mc.plugin.ball.common.event.player.BallPlayerInfoUpdateEvent;
|
||||||
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
||||||
import cn.hamster3.mc.plugin.core.lib.com.zaxxer.hikari.HikariConfig;
|
|
||||||
import cn.hamster3.mc.plugin.core.lib.com.zaxxer.hikari.HikariDataSource;
|
|
||||||
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
||||||
import net.md_5.bungee.api.connection.Server;
|
import net.md_5.bungee.api.connection.Server;
|
||||||
import net.md_5.bungee.config.Configuration;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
@@ -60,32 +55,4 @@ public final class BallBungeeCordUtils {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public static DataSource getDataSource(@Nullable Configuration datasourceConfig) {
|
|
||||||
if (datasourceConfig == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
HikariConfig hikariConfig = new HikariConfig();
|
|
||||||
String driver = datasourceConfig.getString("driver");
|
|
||||||
hikariConfig.setDriverClassName(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("HamsterBall-Pool");
|
|
||||||
return new HikariDataSource(hikariConfig);
|
|
||||||
} catch (Exception | Error e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
@file:Suppress("VulnerableLibrariesLocal", "GradlePackageVersionRange", "GradlePackageUpdate")
|
@file:Suppress("VulnerableLibrariesLocal", "GradlePackageVersionRange", "GradlePackageUpdate")
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compileOnly("cn.hamster3.mc.plugin:core-common:1.3.0")
|
compileOnly("cn.hamster3.mc.plugin:core-common:+")
|
||||||
|
|
||||||
compileOnly("com.google.code.gson:gson:2.8.0")
|
compileOnly("com.google.code.gson:gson:2.8.0")
|
||||||
compileOnly("com.google.guava:guava:31.0-jre")
|
compileOnly("com.google.guava:guava:31.0-jre")
|
||||||
|
@@ -14,7 +14,9 @@ import cn.hamster3.mc.plugin.ball.common.listener.BallCommonListener;
|
|||||||
import cn.hamster3.mc.plugin.ball.common.listener.BallDebugListener;
|
import cn.hamster3.mc.plugin.ball.common.listener.BallDebugListener;
|
||||||
import cn.hamster3.mc.plugin.ball.common.listener.BallRedisListener;
|
import cn.hamster3.mc.plugin.ball.common.listener.BallRedisListener;
|
||||||
import cn.hamster3.mc.plugin.core.common.api.CoreAPI;
|
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.data.DisplayMessage;
|
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
|
||||||
|
import cn.hamster3.mc.plugin.core.common.util.CoreUtils;
|
||||||
import cn.hamster3.mc.plugin.core.lib.net.kyori.adventure.text.Component;
|
import cn.hamster3.mc.plugin.core.lib.net.kyori.adventure.text.Component;
|
||||||
import cn.hamster3.mc.plugin.core.lib.redis.clients.jedis.Jedis;
|
import cn.hamster3.mc.plugin.core.lib.redis.clients.jedis.Jedis;
|
||||||
import com.google.common.eventbus.AsyncEventBus;
|
import com.google.common.eventbus.AsyncEventBus;
|
||||||
@@ -40,26 +42,62 @@ public abstract class BallAPI {
|
|||||||
* API 使用的玩家信息更新通信频道
|
* API 使用的玩家信息更新通信频道
|
||||||
*/
|
*/
|
||||||
public static final String PLAYER_INFO_CHANNEL = "HamsterBall:PlayerInfo";
|
public static final String PLAYER_INFO_CHANNEL = "HamsterBall:PlayerInfo";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* API 实例
|
* API 实例
|
||||||
*/
|
*/
|
||||||
@Getter
|
@Getter
|
||||||
protected static BallAPI instance;
|
protected static BallAPI instance;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final BallConfig ballConfig;
|
private final BallConfig ballConfig;
|
||||||
|
@NotNull
|
||||||
|
private final DataSource datasource;
|
||||||
|
@NotNull
|
||||||
|
private final BallServerInfo serverInfo;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final EventBus eventBus;
|
private final EventBus eventBus;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final Map<String, BallServerInfo> allServerInfo;
|
private final Map<String, BallServerInfo> allServerInfo;
|
||||||
@NotNull
|
@NotNull
|
||||||
private final Map<UUID, BallPlayerInfo> allPlayerInfo;
|
private final Map<UUID, BallPlayerInfo> allPlayerInfo;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final Jedis redisSub;
|
private final Jedis redisSub;
|
||||||
@NotNull
|
@NotNull
|
||||||
private final Jedis redisPub;
|
private final Jedis redisPub;
|
||||||
|
|
||||||
public BallAPI(@NotNull BallConfig ballConfig) {
|
public BallAPI(@NotNull ConfigSection config) {
|
||||||
this.ballConfig = ballConfig;
|
Map<String, String> env = System.getenv();
|
||||||
|
ConfigSection serverInfoConfig = config.getSection("server-info");
|
||||||
|
if (serverInfoConfig == null) {
|
||||||
|
throw new IllegalArgumentException("配置文件中未找到 server-info 节点");
|
||||||
|
}
|
||||||
|
serverInfo = new BallServerInfo(
|
||||||
|
env.getOrDefault("BALL_LOCAL_SERVER_INFO_ID", serverInfoConfig.getString("id")),
|
||||||
|
env.getOrDefault("BALL_LOCAL_SERVER_INFO_NAME", serverInfoConfig.getString("name")),
|
||||||
|
BallServerType.GAME,
|
||||||
|
env.getOrDefault("BALL_LOCAL_SERVER_IP", serverInfoConfig.getString("host")),
|
||||||
|
Integer.parseInt(
|
||||||
|
env.getOrDefault("BALL_LOCAL_SERVER_PORT", String.valueOf(serverInfoConfig.getInt("port")))
|
||||||
|
)
|
||||||
|
);
|
||||||
|
ConfigSection section = config.getSection("datasource");
|
||||||
|
if (section != null) {
|
||||||
|
getLogger().info("启用仓鼠球自定义数据库连接池");
|
||||||
|
datasource = CoreUtils.getDataSource(section);
|
||||||
|
} else {
|
||||||
|
getLogger().info("复用 HamsterCore 的数据库连接池");
|
||||||
|
datasource = CoreAPI.getInstance().getDataSource();
|
||||||
|
}
|
||||||
|
ballConfig = new BallConfig(
|
||||||
|
config.getBoolean("debug", false),
|
||||||
|
config.getString("channel-prefix", "") + ":",
|
||||||
|
config.getBoolean("game-server-update-player-info", false),
|
||||||
|
config.getStringList("load-player-info-filter")
|
||||||
|
);
|
||||||
redisSub = CoreAPI.getInstance().getJedisPool().getResource();
|
redisSub = CoreAPI.getInstance().getJedisPool().getResource();
|
||||||
redisPub = CoreAPI.getInstance().getJedisPool().getResource();
|
redisPub = CoreAPI.getInstance().getJedisPool().getResource();
|
||||||
allServerInfo = new ConcurrentHashMap<>();
|
allServerInfo = new ConcurrentHashMap<>();
|
||||||
@@ -526,12 +564,12 @@ public abstract class BallAPI {
|
|||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
public BallServerInfo getLocalServerInfo() {
|
public BallServerInfo getLocalServerInfo() {
|
||||||
return ballConfig.getServerInfo();
|
return serverInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public String getLocalServerId() {
|
public String getLocalServerId() {
|
||||||
return ballConfig.getServerInfo().getId();
|
return serverInfo.getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -651,6 +689,6 @@ public abstract class BallAPI {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public DataSource getDatasource() {
|
public DataSource getDatasource() {
|
||||||
return ballConfig.getDatasource() == null ? CoreAPI.getInstance().getDataSource() : ballConfig.getDatasource();
|
return datasource;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,12 +1,9 @@
|
|||||||
package cn.hamster3.mc.plugin.ball.common.config;
|
package cn.hamster3.mc.plugin.ball.common.config;
|
||||||
|
|
||||||
import cn.hamster3.mc.plugin.ball.common.entity.BallServerInfo;
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@@ -17,8 +14,4 @@ public class BallConfig {
|
|||||||
private String channelPrefix;
|
private String channelPrefix;
|
||||||
private boolean gameServerUpdatePlayerInfo;
|
private boolean gameServerUpdatePlayerInfo;
|
||||||
private List<String> loadPlayerInfoFilter;
|
private List<String> loadPlayerInfoFilter;
|
||||||
@NotNull
|
|
||||||
private BallServerInfo serverInfo;
|
|
||||||
@Nullable
|
|
||||||
private DataSource datasource;
|
|
||||||
}
|
}
|
||||||
|
@@ -5,7 +5,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group = "cn.hamster3.mc.plugin"
|
group = "cn.hamster3.mc.plugin"
|
||||||
version = "1.6.0"
|
version = "1.6.1-SNAPSHOT"
|
||||||
|
|
||||||
subprojects {
|
subprojects {
|
||||||
apply {
|
apply {
|
||||||
|
Reference in New Issue
Block a user