ConfigHandler.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package me.lortseam.completeconfig;
  2. import me.lortseam.completeconfig.api.ConfigGroup;
  3. import me.lortseam.completeconfig.api.ConfigOwner;
  4. import me.lortseam.completeconfig.data.Config;
  5. import me.lortseam.completeconfig.gui.GuiBuilder;
  6. import net.fabricmc.api.EnvType;
  7. import net.fabricmc.api.Environment;
  8. import net.fabricmc.loader.api.FabricLoader;
  9. import net.minecraft.client.gui.screen.Screen;
  10. import org.apache.commons.lang3.ArrayUtils;
  11. import org.apache.logging.log4j.LogManager;
  12. import org.apache.logging.log4j.Logger;
  13. import org.spongepowered.configurate.CommentedConfigurationNode;
  14. import org.spongepowered.configurate.ConfigurateException;
  15. import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
  16. import java.nio.file.Path;
  17. import java.nio.file.Paths;
  18. import java.util.*;
  19. public final class ConfigHandler {
  20. private static final Logger LOGGER = LogManager.getLogger();
  21. private static final Map<Class<? extends ConfigOwner>, ConfigHandler> HANDLERS = new HashMap<>();
  22. static {
  23. Runtime.getRuntime().addShutdownHook(new Thread(() -> {
  24. for (ConfigHandler handler : HANDLERS.values()) {
  25. handler.save();
  26. }
  27. }));
  28. }
  29. static ConfigHandler registerConfig(String modID, String[] branch, Class<? extends ConfigOwner> owner, List<ConfigGroup> topLevelGroups, GuiBuilder guiBuilder) {
  30. if (HANDLERS.containsKey(owner)) {
  31. throw new IllegalArgumentException("The specified owner " + owner + " already created a config!");
  32. }
  33. if (topLevelGroups.isEmpty()) {
  34. LOGGER.warn("[CompleteConfig] Owner " + owner + " of mod " + modID + " tried to create an empty config!");
  35. return null;
  36. }
  37. String[] subPath = ArrayUtils.add(branch, 0, modID);
  38. subPath[subPath.length - 1] = subPath[subPath.length - 1] + ".conf";
  39. Path filePath = Paths.get(FabricLoader.getInstance().getConfigDir().toString(), subPath);
  40. //TODO: filePath field was removed
  41. /*if (HANDLERS.values().stream().anyMatch(handler -> handler.filePath.equals(filePath))) {
  42. throw new IllegalArgumentException("A config of the mod " + modID + " with the specified branch " + Arrays.toString(branch) + " already exists!");
  43. }*/
  44. ConfigHandler handler = new ConfigHandler(modID, filePath, topLevelGroups, guiBuilder);
  45. HANDLERS.put(owner, handler);
  46. return handler;
  47. }
  48. /**
  49. * Gets the {@link ConfigHandler} for the specified owner if that owner created a config before.
  50. *
  51. * @param owner The owner class of the config
  52. * @return The handler if one was found or else an empty result
  53. */
  54. public static Optional<ConfigHandler> of(Class<? extends ConfigOwner> owner) {
  55. return Optional.ofNullable(HANDLERS.get(owner));
  56. }
  57. private final HoconConfigurationLoader loader;
  58. private final Config config;
  59. private GuiBuilder guiBuilder;
  60. private ConfigHandler(String modID, Path filePath, List<ConfigGroup> topLevelGroups, GuiBuilder guiBuilder) {
  61. loader = HoconConfigurationLoader.builder()
  62. .path(filePath)
  63. .build();
  64. config = new Config(modID, topLevelGroups);
  65. CommentedConfigurationNode root = load();
  66. if (!root.virtual()) {
  67. config.apply(root);
  68. }
  69. this.guiBuilder = guiBuilder;
  70. }
  71. private CommentedConfigurationNode load() {
  72. try {
  73. return loader.load();
  74. } catch (ConfigurateException e) {
  75. //TODO
  76. e.printStackTrace();
  77. }
  78. return CommentedConfigurationNode.root();
  79. }
  80. /**
  81. * Generates the configuration GUI.
  82. *
  83. * @param parentScreen The parent screen
  84. * @return The generated configuration screen
  85. */
  86. @Environment(EnvType.CLIENT)
  87. public Screen buildScreen(Screen parentScreen) {
  88. if (guiBuilder == null) {
  89. if (GuiBuilder.DEFAULT != null) {
  90. guiBuilder = GuiBuilder.DEFAULT;
  91. } else {
  92. throw new UnsupportedOperationException("No GUI builder provided");
  93. }
  94. }
  95. return guiBuilder.buildScreen(parentScreen, config, this::save);
  96. }
  97. /**
  98. * Saves the config to a save file.
  99. */
  100. public void save() {
  101. CommentedConfigurationNode root = loader.createNode();
  102. config.fetch(root);
  103. try {
  104. loader.save(root);
  105. } catch (ConfigurateException e) {
  106. //TODO
  107. e.printStackTrace();
  108. }
  109. }
  110. }