fix: load & apply config explicitly

This commit is contained in:
Erica Marigold 2024-05-06 15:57:01 +05:30
parent 2d9aefccb0
commit cdc6cf7299
No known key found for this signature in database
GPG key ID: 2768CC0C23D245D1
2 changed files with 24 additions and 4 deletions

View file

@ -2,6 +2,7 @@
import java.util.UUID;
import xyz.devcomp.touch_grass_reminder.config.ConfigHandler;
import xyz.devcomp.touch_grass_reminder.config.ConfigModel;
import xyz.devcomp.touch_grass_reminder.utils.PlayDurationHandler;
@ -16,7 +17,7 @@
public class TouchGrassReminderClient implements ClientModInitializer {
public static final Logger LOGGER = LoggerFactory.getLogger("Touch Grass Reminder");
private ConfigModel config = new ConfigModel();
private ConfigModel config = new ConfigHandler().getInstance();
private Thread thread;
@Override
@ -28,12 +29,16 @@ public void onInitializeClient(ModContainer mod) {
if (config.isEnabled) {
ClientPlayConnectionEvents.JOIN.register((net, packet, client) -> {
UUID sessionId = net.getId();
ServerData serverInfo = net.getServerData() == null ? new ServerData("Unknown", "Unknown", false)
: net.getServerData();
ServerData serverInfo = net.getServerData();
LOGGER.info(
if (serverInfo != null) {
LOGGER.info(
"Player initiated connection; sessionId={}, name={}, version={}, protocolVersion={}, isLocal={}",
sessionId, serverInfo.name, serverInfo.version, serverInfo.protocol, serverInfo.isLan());
} else {
LOGGER.warn("Could not get details about joined server.");
}
PlayDurationHandler worker = new PlayDurationHandler(client, System.currentTimeMillis(),
config.reminderFrequency * 60 * 60 * 1000);

View file

@ -2,6 +2,7 @@
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.resources.ResourceLocation;
import xyz.devcomp.touch_grass_reminder.client.TouchGrassReminderClient;
import org.quiltmc.loader.api.QuiltLoader;
@ -9,6 +10,7 @@
import dev.isxander.yacl3.config.v2.api.serializer.GsonConfigSerializerBuilder;
public class ConfigHandler {
private boolean isLoaded = false;
public static final ConfigClassHandler<ConfigModel> HANDLER = ConfigClassHandler.createBuilder(ConfigModel.class)
.id(new ResourceLocation("touchgrassreminder", "config"))
.serializer(config -> GsonConfigSerializerBuilder.create(config)
@ -17,7 +19,20 @@ public class ConfigHandler {
.build())
.build();
private void loadConfig() {
if (!this.isLoaded) {
this.isLoaded = HANDLER.load();
TouchGrassReminderClient.LOGGER.info("Config not loaded. Attempting to load.");
}
}
public Screen showGui(Screen parent) {
this.loadConfig();
return HANDLER.generateGui().generateScreen(parent);
}
public ConfigModel getInstance() {
this.loadConfig();
return HANDLER.instance();
}
}