添加了自定义端口配置类
This commit is contained in:
parent
43483f28e6
commit
70f2648ded
50
src/main/java/com/example/playertime/ModConfig.java
Normal file
50
src/main/java/com/example/playertime/ModConfig.java
Normal file
@ -0,0 +1,50 @@
|
||||
package com.example.playertime;
|
||||
|
||||
import com.google.gson.*;
|
||||
import java.io.*;
|
||||
import java.nio.file.*;
|
||||
|
||||
public class ModConfig {
|
||||
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
|
||||
private final Path configPath;
|
||||
private int webPort = 8080; // 默认端口
|
||||
|
||||
public ModConfig(Path configDir) {
|
||||
this.configPath = configDir.resolve("playertime-config.json");
|
||||
loadConfig();
|
||||
}
|
||||
|
||||
private void loadConfig() {
|
||||
if (!Files.exists(configPath)) {
|
||||
saveConfig();
|
||||
return;
|
||||
}
|
||||
|
||||
try (Reader reader = Files.newBufferedReader(configPath)) {
|
||||
JsonObject json = JsonParser.parseReader(reader).getAsJsonObject();
|
||||
if (json.has("webPort")) {
|
||||
webPort = json.get("webPort").getAsInt();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
PlayerTimeMod.LOGGER.error("Failed to load config", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void saveConfig() {
|
||||
JsonObject json = new JsonObject();
|
||||
json.addProperty("webPort", webPort);
|
||||
|
||||
try {
|
||||
Files.createDirectories(configPath.getParent());
|
||||
try (Writer writer = Files.newBufferedWriter(configPath)) {
|
||||
GSON.toJson(json, writer);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
PlayerTimeMod.LOGGER.error("Failed to save config", e);
|
||||
}
|
||||
}
|
||||
|
||||
public int getWebPort() {
|
||||
return webPort;
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@ package com.example.playertime;
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
|
||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -10,23 +11,26 @@ public class PlayerTimeMod implements ModInitializer {
|
||||
public static final Logger LOGGER = LoggerFactory.getLogger("PlayerTimeTracker");
|
||||
private static PlayerTimeTracker timeTracker;
|
||||
private static WebServer webServer;
|
||||
private static ModConfig config;
|
||||
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
|
||||
config = new ModConfig(FabricLoader.getInstance().getConfigDir());
|
||||
|
||||
|
||||
try {
|
||||
LOGGER.info("[在线时间] 初始化玩家在线时长视奸MOD");
|
||||
|
||||
ServerLifecycleEvents.SERVER_STARTING.register(server -> {
|
||||
LOGGER.info("[在线时间] 服务器启动 - 初始化跟踪器");
|
||||
timeTracker = new PlayerTimeTracker(server);
|
||||
|
||||
try {
|
||||
webServer = new WebServer(timeTracker, 60048);
|
||||
// 使用配置中的端口
|
||||
webServer = new WebServer(timeTracker, config.getWebPort());
|
||||
webServer.start();
|
||||
LOGGER.info("[在线时间] 在线时长Web服务器启动在端口60048");
|
||||
LOGGER.info("Web服务器在端口" + config.getWebPort()+ "启动");
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("[在线时间] 无法启动 Web 服务器", e);
|
||||
throw new RuntimeException("[在线时间] Web 服务器启动失败", e);
|
||||
LOGGER.error("无法启动Web服务器", e);
|
||||
}
|
||||
});
|
||||
|
||||
@ -58,6 +62,10 @@ public class PlayerTimeMod implements ModInitializer {
|
||||
}
|
||||
}
|
||||
|
||||
public static ModConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public static PlayerTimeTracker getTimeTracker() {
|
||||
return timeTracker;
|
||||
}
|
||||
|
@ -25,6 +25,9 @@ public class WebServer {
|
||||
);
|
||||
|
||||
public WebServer(PlayerTimeTracker timeTracker, int port) throws IOException {
|
||||
if (port < 1 || port > 65535) {
|
||||
throw new IllegalArgumentException("Invalid port number: " + port);
|
||||
}
|
||||
this.timeTracker = timeTracker;
|
||||
this.server = HttpServer.create(new InetSocketAddress(port), 0);
|
||||
setupContexts();
|
||||
|
3
src/main/resources/config/playertime-default-config.json
Normal file
3
src/main/resources/config/playertime-default-config.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"webPort": 60048
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user