mirror of
https://github.com/TheWhiteDog9487/ServerAddressSpaceFix.git
synced 2026-07-12 21:41:09 +00:00
使用YACL Config API自动将功能开关状态保存到文件内
使用文件系统监听器使配置文件被外部更改时可以自动重载到游戏内 解决直接连接窗口内输入框里的服务器IP没有被移除空格处理的问题 在移除空格之后打印日志 添加英文README 重新给main入口类添加`@Deprecated`标记,这东西确实用不到,后面看要不要直接删了 为AI Agent添加AGENTS.md文件以指示通用提示词
This commit is contained in:
committed by
TheWhiteDog9487
parent
5cb7a45ff4
commit
92d1e282d8
@@ -1,5 +1,77 @@
|
||||
package xyz.thewhitedog9487;
|
||||
|
||||
import com.google.gson.GsonBuilder;
|
||||
import dev.isxander.yacl3.config.v2.api.ConfigClassHandler;
|
||||
import dev.isxander.yacl3.config.v2.api.SerialEntry;
|
||||
import dev.isxander.yacl3.config.v2.api.serializer.GsonConfigSerializerBuilder;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import net.minecraft.resources.Identifier;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileSystems;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardWatchEventKinds;
|
||||
import java.nio.file.WatchEvent;
|
||||
import java.nio.file.WatchKey;
|
||||
import java.nio.file.WatchService;
|
||||
|
||||
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
|
||||
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
|
||||
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
|
||||
import static xyz.thewhitedog9487.ServerAddressSpaceFixClient.ModID;
|
||||
import static xyz.thewhitedog9487.ServerAddressSpaceFixClient.ModLogger;
|
||||
|
||||
public class Settings {
|
||||
public static boolean ModEnabled = true;
|
||||
public static Path ConfigFilePath = FabricLoader.getInstance()
|
||||
.getConfigDir()
|
||||
.resolve(ModID + ".json5")
|
||||
.toAbsolutePath();
|
||||
public static ConfigClassHandler<Settings> SettingsHandler = ConfigClassHandler.createBuilder(Settings.class)
|
||||
.id(Identifier.fromNamespaceAndPath(ModID, "settings_confighandler"))
|
||||
.serializer(SettingsConfigClassHandler -> GsonConfigSerializerBuilder.create(SettingsConfigClassHandler)
|
||||
.setPath(ConfigFilePath)
|
||||
.appendGsonBuilder(GsonBuilder::setPrettyPrinting)
|
||||
.setJson5(true)
|
||||
.build() )
|
||||
.build();
|
||||
public static volatile Settings SettingsInstance;
|
||||
|
||||
// region 配置文件变更监听器
|
||||
public static void RegisterConfigFileReloadWatcher(){
|
||||
// 由GPT 5.4 Mini生成
|
||||
Thread.startVirtualThread(new Thread(() -> {
|
||||
Path ConfigDirectory = ConfigFilePath.getParent();
|
||||
try (WatchService WatchService = FileSystems.getDefault().newWatchService()) {
|
||||
ConfigDirectory.register(WatchService, ENTRY_CREATE, ENTRY_MODIFY, ENTRY_DELETE);
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
WatchKey WatchKey = WatchService.take();
|
||||
boolean ShouldReload = false;
|
||||
|
||||
for (WatchEvent<?> WatchEvent : WatchKey.pollEvents()) {
|
||||
WatchEvent.Kind<?> EventKind = WatchEvent.kind();
|
||||
if (EventKind == StandardWatchEventKinds.OVERFLOW) {
|
||||
continue; }
|
||||
Path ChangedPath = (Path) WatchEvent.context();
|
||||
if (ChangedPath != null && ChangedPath.equals(ConfigFilePath.getFileName())) {
|
||||
ShouldReload = true; } }
|
||||
if (ShouldReload) {
|
||||
SettingsHandler.load();
|
||||
SettingsInstance = SettingsHandler.instance();
|
||||
ModLogger.info("检测到配置文件变更,已重新加载设置。");}
|
||||
if (!WatchKey.reset()) {
|
||||
break;}}
|
||||
} catch (InterruptedException InterruptedException) {
|
||||
Thread.currentThread().interrupt();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e); } } ) ); }
|
||||
// endregion
|
||||
|
||||
@SerialEntry(comment =
|
||||
"""
|
||||
是否启用本模组的功能
|
||||
类型:布尔值
|
||||
取值范围:true false
|
||||
默认值:true
|
||||
""")
|
||||
public boolean ModEnabled = true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user