Posted April 23, 20214 yr Hello Forge Community, I am relatively new to modding so please be patient, also I apologize if I make beginner mistakes. Currently I'm re-writing a 1.8.9 HUD mod that would display various text and items to the screen. When I wrote the original mod I didn't follow config file conventions, because I was more focused on making my first mod. But now I would like to understand the convention and the correct way of using ".cfg" files. I have been googling to see if others have any tutorials, or have asked on the forums about config files but I cant find any up to date resources (I'm not sure what the latest config convention is). My Goal: What I am trying to accomplish is, be able to read in values from a config file such as (int x, int y, boolean enabled, String name, int color) for different HUD sub-mods. Then update them via chat commands and save them to the config file again. I am willing to write this mod in 1.16.x if the config implementation has been upgraded since 1.12.2 and would appreciate any information/tutorials for doing so. Steps I've Taken: I began with the Forge Documentation but was unable to see any information of config files for 1.16.x , I might have been just looking in the wrong place. I did look back at the 1.12.x Documentation and found config annotations. (I also followed the link to the annotation testing GitHub repo) When I made an attempt to implement these I was able to create a "ModConfig.java" that created a ".cfg" file that was correctly structured for how it was defined, but I was unable to update the values and save them to the config file when running the mod. I was using `ConfigManager.sync(MODID, Type.INSTANCE)` to attempt to write the updated values to the file but It would overwrite the values in the code before saving, I think this is due to me modifying it by just setting the fields and not using GUI but I'm not sure. I appreciate any and all help on this topic and if you require more information I can provide it. Thank you in advance for your time! Files: Here is the mod entry point file ->"CryoHudMod.java", and my config annotated file -> "ModConfig.java", and the generated "cryo-hud-mod.cfg". Spoiler package com.cryoexn.hudmod.util; import com.cryoexn.hudmod.CryoHudMod; import net.minecraftforge.common.config.ConfigManager; import net.minecraftforge.fml.client.event.ConfigChangedEvent; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.common.config.Config; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; @Config(modid= CryoHudMod.MODID, type=Config.Type.INSTANCE, category="sub-mods") public class ModConfig { private static final int DEFAULT_FG = 0xffffffff; private static final int DEFAULT_BG = 0x4f000000; public static SubModConfig fpsConf = new SubModConfig(false, new Position(10, 10),"fps", DEFAULT_FG, DEFAULT_BG);; public static SubModConfig memConf = new SubModConfig(true, new Position(0, 0),"mem", DEFAULT_FG, DEFAULT_BG);; public static class SubModConfig { public boolean isEnabled; public ModConfig.PositionConfig pos; public String name; @Config.Name("foreground-color") public int fgColor; @Config.Name("background-color") public int bgColor; public SubModConfig(boolean isEnabled, Position pos, String name, int fgColor, int bgColor) { this.isEnabled = isEnabled; this.pos = new ModConfig.PositionConfig(pos); this.name = name; this.fgColor = fgColor; this.bgColor = bgColor; } // end Constructor. } // end FpsConfig. public static class PositionConfig { public int x; public int y; public PositionConfig(Position pos) { this.x = pos.getXpos(); this.y = pos.getYpos(); } // end Default Constructor. } // end PositionConfig. @Mod.EventBusSubscriber(modid=CryoHudMod.MODID) private static class EventHandler { /** * Inject the new values and save to the config file when the config has been changed from the GUI. * * @param event The event */ @SubscribeEvent public static void onConfigChanged(final ConfigChangedEvent.OnConfigChangedEvent event) { if (event.getModID().equals(CryoHudMod.MODID)) { ConfigManager.sync(CryoHudMod.MODID, Config.Type.INSTANCE); } } } // end EventHandler. } //end ModConfig. Spoiler package com.cryoexn.hudmod; import com.cryoexn.hudmod.commands.CommandHud; import com.cryoexn.hudmod.submods.fps.ModFpsCounter; import com.cryoexn.hudmod.util.ModConfig; import net.minecraftforge.client.ClientCommandHandler; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod.EventHandler; import net.minecraftforge.fml.common.event.FMLInitializationEvent; import com.cryoexn.hudmod.commands.CommandAbstract; import net.minecraftforge.fml.common.event.FMLServerStoppingEvent; import org.apache.logging.log4j.Logger; import java.util.Arrays; /** * Mod for HUD * * experimenting and learning coding practices and interfacing with existing libraries. */ @Mod(modid = CryoHudMod.MODID, name = CryoHudMod.NAME, version = CryoHudMod.VERSION) public class CryoHudMod { public static final String MODID = "cryo-hud-mod"; public static final String NAME = "Cryo-Hud-Mod"; public static final String VERSION = "1.0.0"; private static Logger logger; public static ModFpsCounter fpsCounter; /** * Initialize all event, and command handlers. * * @param event Initialization event. */ @EventHandler public void init(FMLInitializationEvent event) { logger.debug("Old: {}", ModConfig.fpsConf.isEnabled); ModConfig.fpsConf.isEnabled = !ModConfig.fpsConf.isEnabled; logger.debug("New: {}", ModConfig.fpsConf.isEnabled); fpsCounter = new ModFpsCounter(); // EventHandlers are auto registered. // Register CommandHandler(s). this.registerCommands(new CommandHud()); } // end init. @EventHandler public void serverStopping(FMLServerStoppingEvent event) { ModConfig.fpsConf = fpsCounter.getValues(); } // end serverStopping. /** * Register all commands with the Forge ClientCommandHandler. * * @param commands Command handlers to register. */ private void registerCommands(Object... commands) { Arrays.asList(commands).forEach((command) -> { ClientCommandHandler.instance.registerCommand((CommandAbstract)command); }); } // end registerCommands. } // end CryoHudMod. Spoiler # Configuration file sub-mods { fpsconf { I:background-color=1325400064 I:foreground-color=-1 B:isEnabled=false S:name=fps pos { I:x=10 I:y=10 } } memconf { I:background-color=1325400064 I:foreground-color=-1 B:isEnabled=true S:name=mem pos { I:x=0 I:y=0 } } } Edited April 23, 20214 yr by Cryoexn Syntax Highlighting was off for a code block.
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.