perf: 简化代码

This commit is contained in:
2024-03-17 01:21:08 +08:00
parent ade9a1094a
commit 134454f94f
4 changed files with 20 additions and 18 deletions

View File

@@ -2,6 +2,7 @@ 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.common.api.BallAPI; import cn.hamster3.mc.plugin.ball.common.api.BallAPI;
import cn.hamster3.mc.plugin.ball.common.entity.BallServerType;
import cn.hamster3.mc.plugin.core.common.config.ConfigSection; 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.config.YamlConfig;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -13,7 +14,7 @@ import java.util.logging.Logger;
public class BallBukkitAPI extends BallAPI { public class BallBukkitAPI extends BallAPI {
public BallBukkitAPI(@NotNull ConfigSection config) { public BallBukkitAPI(@NotNull ConfigSection config) {
super(config); super(config, BallServerType.GAME);
} }
public static BallBukkitAPI getInstance() { public static BallBukkitAPI getInstance() {

View File

@@ -2,6 +2,7 @@ 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.common.api.BallAPI; import cn.hamster3.mc.plugin.ball.common.api.BallAPI;
import cn.hamster3.mc.plugin.ball.common.entity.BallServerType;
import cn.hamster3.mc.plugin.core.common.config.ConfigSection; 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.config.YamlConfig;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -13,7 +14,7 @@ import java.util.logging.Logger;
public class BallBungeeCordAPI extends BallAPI { public class BallBungeeCordAPI extends BallAPI {
public BallBungeeCordAPI(@NotNull ConfigSection config) { public BallBungeeCordAPI(@NotNull ConfigSection config) {
super(config); super(config, BallServerType.PROXY);
} }
public static BallBungeeCordAPI getInstance() { public static BallBungeeCordAPI getInstance() {

View File

@@ -69,21 +69,12 @@ public abstract class BallAPI {
@NotNull @NotNull
private final Jedis redisPub; private final Jedis redisPub;
public BallAPI(@NotNull ConfigSection config) { public BallAPI(@NotNull ConfigSection config, BallServerType type) {
Map<String, String> env = System.getenv();
ConfigSection serverInfoConfig = config.getSection("server-info"); ConfigSection serverInfoConfig = config.getSection("server-info");
if (serverInfoConfig == null) { if (serverInfoConfig == null) {
throw new IllegalArgumentException("配置文件中未找到 server-info 节点"); throw new IllegalArgumentException("配置文件中未找到 server-info 节点");
} }
serverInfo = new BallServerInfo( serverInfo = new BallServerInfo(serverInfoConfig, type);
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"); ConfigSection section = config.getSection("datasource");
if (section != null) { if (section != null) {
getLogger().info("启用仓鼠球自定义数据库连接池"); getLogger().info("启用仓鼠球自定义数据库连接池");
@@ -686,9 +677,4 @@ public abstract class BallAPI {
@NotNull @NotNull
public abstract Logger getLogger(); public abstract Logger getLogger();
@NotNull
public DataSource getDatasource() {
return datasource;
}
} }

View File

@@ -1,9 +1,12 @@
package cn.hamster3.mc.plugin.ball.common.entity; package cn.hamster3.mc.plugin.ball.common.entity;
import cn.hamster3.mc.plugin.core.common.config.ConfigSection;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.jetbrains.annotations.NotNull;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
/** /**
@@ -40,6 +43,17 @@ public class BallServerInfo {
*/ */
private int port; private int port;
public BallServerInfo(@NotNull ConfigSection config, BallServerType type) {
Map<String, String> env = System.getenv();
id = env.getOrDefault("BALL_LOCAL_SERVER_INFO_ID", config.getString("id"));
name = env.getOrDefault("BALL_LOCAL_SERVER_INFO_NAME", config.getString("name"));
this.type = type;
host = env.getOrDefault("BALL_LOCAL_SERVER_IP", config.getString("host"));
port = Integer.parseInt(
env.getOrDefault("BALL_LOCAL_SERVER_PORT", String.valueOf(config.getInt("port")))
);
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;