From caf3f1610cb8d5ce8867f87ddf17dcb4c9e0852f Mon Sep 17 00:00:00 2001 From: Nixuge Date: Fri, 30 Jun 2023 15:00:20 +0200 Subject: [PATCH] Added config page & settings --- gradle.properties | 2 +- .../nixuge/serverlistbufferfixer/McMod.java | 34 +++++++++-- .../config/ConfigCache.java | 59 +++++++++++++++++++ .../serverlistbufferfixer/gui/GuiFactory.java | 29 +++++++++ .../gui/ModGuiConfig.java | 20 +++++++ .../mixins/ServerListEntryNormalMixin.java | 11 +++- 6 files changed, 145 insertions(+), 10 deletions(-) create mode 100644 src/main/java/me/nixuge/serverlistbufferfixer/config/ConfigCache.java create mode 100644 src/main/java/me/nixuge/serverlistbufferfixer/gui/GuiFactory.java create mode 100644 src/main/java/me/nixuge/serverlistbufferfixer/gui/ModGuiConfig.java diff --git a/gradle.properties b/gradle.properties index b1819be..d5d5b92 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,7 +1,7 @@ mod_id=serverlistbufferfixer mod_name=Serverlist Buffer Fixer mod_group=me.nixuge.serverlistbufferfixer -mod_version=1.0.0 +mod_version=1.0.1 mod_author=["Nixuge"] mod_description=A mod that fixes the slow/infinite server data loading in the multiplayer menu. Also prevents you from spamming 'refresh' too much. minecraft_version=1.8.9 diff --git a/src/main/java/me/nixuge/serverlistbufferfixer/McMod.java b/src/main/java/me/nixuge/serverlistbufferfixer/McMod.java index 55a7e8d..7a0235b 100644 --- a/src/main/java/me/nixuge/serverlistbufferfixer/McMod.java +++ b/src/main/java/me/nixuge/serverlistbufferfixer/McMod.java @@ -1,13 +1,21 @@ package me.nixuge.serverlistbufferfixer; +import java.io.File; + import lombok.Getter; import lombok.Setter; +import me.nixuge.serverlistbufferfixer.config.ConfigCache; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.common.config.Configuration; import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.common.event.FMLInitializationEvent; +import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; @Mod( modid = McMod.MOD_ID, name = McMod.NAME, version = McMod.VERSION, + guiFactory = "me.nixuge.serverlistbufferfixer.gui.GuiFactory", clientSideOnly = true ) @@ -15,16 +23,30 @@ public class McMod { public static final String MOD_ID = "serverlistbufferfixer"; public static final String NAME = "Serverlist Buffer Fixer"; - public static final String VERSION = "1.0.0"; + public static final String VERSION = "1.0.1"; @Getter @Mod.Instance(value = McMod.MOD_ID) private static McMod instance; - //@Mod.EventHandler - //public void preInit(final FMLPreInitializationEvent event) { - // Keeping that to setup the config later - // (timeout & custom messages?) - //} + @Getter + private Configuration configuration; + @Getter + private String configDirectory; + @Getter + private ConfigCache configCache; + + @Mod.EventHandler + public void preInit(final FMLPreInitializationEvent event) { + this.configDirectory = event.getModConfigurationDirectory().toString(); + final File path = new File(this.configDirectory + File.separator + McMod.MOD_ID + ".cfg"); + this.configuration = new Configuration(path); + this.configCache = new ConfigCache(this.configuration); + } + + @Mod.EventHandler + public void init(final FMLInitializationEvent event) { + MinecraftForge.EVENT_BUS.register(configCache); + } } diff --git a/src/main/java/me/nixuge/serverlistbufferfixer/config/ConfigCache.java b/src/main/java/me/nixuge/serverlistbufferfixer/config/ConfigCache.java new file mode 100644 index 0000000..3a33d87 --- /dev/null +++ b/src/main/java/me/nixuge/serverlistbufferfixer/config/ConfigCache.java @@ -0,0 +1,59 @@ +package me.nixuge.serverlistbufferfixer.config; + +import lombok.Getter; +import me.nixuge.serverlistbufferfixer.McMod; +import net.minecraftforge.common.config.Configuration; +import net.minecraftforge.fml.client.event.ConfigChangedEvent; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; + +@Getter +public class ConfigCache { + private final Configuration configuration; + + private int maxThreadCountPinger; + private int maxThreadCountTimeout; + private int serverTimeout; + + public ConfigCache(final Configuration configuration) { + this.configuration = configuration; + this.loadConfiguration(); + this.configuration.save(); + } + + private void loadConfiguration() { + // Enable + this.maxThreadCountPinger = this.configuration.getInt( + "Max threadcount (Pinger)", + "General", + 50, + 5, + 500, + "Max numbers of threads for the server pinger. Mc defaults to 5, this mods defaults to 50. Those threads are lightweight so shouldn't cause performance issues, and are only used when loading/reloading servers. Requires a Minecraft restart." + ); + this.maxThreadCountTimeout = this.configuration.getInt( + "Max threadcount (timeouts)", + "General", + 100, + 10, + 1000, + "Max numbers of threads for the server timeout. Usually double of the max threadcount for the server pinger. This mods defaults to 100. Those threads are lightweight so shouldn't cause performance issues, and are only used when loading/reloading servers. Requires a Minecraft restart." + ); + this.serverTimeout = this.configuration.getInt( + "Server fetching timeout", + "General", + 4, + 1, + 60, + "Timeout for fetching a server's data." + ); + + } + + @SubscribeEvent + public void onConfigurationChangeEvent(final ConfigChangedEvent.OnConfigChangedEvent event) { + this.configuration.save(); + if (event.modID.equalsIgnoreCase(McMod.MOD_ID)) { + this.loadConfiguration(); + } + } +} \ No newline at end of file diff --git a/src/main/java/me/nixuge/serverlistbufferfixer/gui/GuiFactory.java b/src/main/java/me/nixuge/serverlistbufferfixer/gui/GuiFactory.java new file mode 100644 index 0000000..aa17380 --- /dev/null +++ b/src/main/java/me/nixuge/serverlistbufferfixer/gui/GuiFactory.java @@ -0,0 +1,29 @@ +package me.nixuge.serverlistbufferfixer.gui; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiScreen; +import net.minecraftforge.fml.client.IModGuiFactory; + +import java.util.HashSet; +import java.util.Set; + +public class GuiFactory implements IModGuiFactory { + @Override + public void initialize(final Minecraft minecraftInstance) { + } + + @Override + public Class mainConfigGuiClass() { + return ModGuiConfig.class; + } + + @Override + public Set runtimeGuiCategories() { + return new HashSet<>(); + } + + @Override + public RuntimeOptionGuiHandler getHandlerFor(final RuntimeOptionCategoryElement element) { + return null; + } +} \ No newline at end of file diff --git a/src/main/java/me/nixuge/serverlistbufferfixer/gui/ModGuiConfig.java b/src/main/java/me/nixuge/serverlistbufferfixer/gui/ModGuiConfig.java new file mode 100644 index 0000000..fceeb27 --- /dev/null +++ b/src/main/java/me/nixuge/serverlistbufferfixer/gui/ModGuiConfig.java @@ -0,0 +1,20 @@ +package me.nixuge.serverlistbufferfixer.gui; + +import me.nixuge.serverlistbufferfixer.McMod; +import net.minecraft.client.gui.GuiScreen; +import net.minecraftforge.common.config.ConfigElement; +import net.minecraftforge.common.config.Configuration; +import net.minecraftforge.fml.client.config.GuiConfig; + +public class ModGuiConfig extends GuiConfig { + public ModGuiConfig(final GuiScreen guiScreen) { + super( + guiScreen, + new ConfigElement(McMod.getInstance().getConfiguration().getCategory(Configuration.CATEGORY_GENERAL)).getChildElements(), + McMod.MOD_ID, + false, + false, + McMod.NAME + " Configuration" + ); + } +} \ No newline at end of file diff --git a/src/main/java/me/nixuge/serverlistbufferfixer/mixins/ServerListEntryNormalMixin.java b/src/main/java/me/nixuge/serverlistbufferfixer/mixins/ServerListEntryNormalMixin.java index cf5aa60..698e2f0 100644 --- a/src/main/java/me/nixuge/serverlistbufferfixer/mixins/ServerListEntryNormalMixin.java +++ b/src/main/java/me/nixuge/serverlistbufferfixer/mixins/ServerListEntryNormalMixin.java @@ -2,6 +2,8 @@ import com.google.common.util.concurrent.ThreadFactoryBuilder; import lombok.SneakyThrows; +import me.nixuge.serverlistbufferfixer.McMod; +import me.nixuge.serverlistbufferfixer.config.ConfigCache; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiMultiplayer; import net.minecraft.client.multiplayer.ServerData; @@ -31,8 +33,11 @@ public class ServerListEntryNormalMixin { @Shadow private static ThreadPoolExecutor field_148302_b; - private static final int MAX_THREAD_COUNT_PINGER = 50; - private static final int MAX_THREAD_COUNT_TIMEOUT = 100; + // still callign mcMod.getinstance.getconfigcache just in case + private static final int MAX_THREAD_COUNT_PINGER = McMod.getInstance().getConfigCache().getMaxThreadCountPinger(); + private static final int MAX_THREAD_COUNT_TIMEOUT = McMod.getInstance().getConfigCache().getMaxThreadCountTimeout(); + + private static final ConfigCache config = McMod.getInstance().getConfigCache(); // Note: if servers are added, this will be inaccurate // But it should be good enough still @@ -80,7 +85,7 @@ public Future drawEntry(ThreadPoolExecutor instance, Runnable r) { return field_148302_b.submit(new Runnable() { public void run() { try { - future.get(4, TimeUnit.SECONDS); + future.get(config.getServerTimeout(), TimeUnit.SECONDS); } catch (TimeoutException e1) { setServerFail(EnumChatFormatting.RED + "Timed out"); } catch (ExecutionException e2) {