Config.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package me.lortseam.completeconfig.data;
  2. import lombok.AccessLevel;
  3. import lombok.Getter;
  4. import lombok.NonNull;
  5. import lombok.extern.log4j.Log4j2;
  6. import me.lortseam.completeconfig.api.ConfigContainer;
  7. import me.lortseam.completeconfig.io.ConfigSource;
  8. import me.lortseam.completeconfig.text.TranslationKey;
  9. import net.fabricmc.api.EnvType;
  10. import net.fabricmc.api.Environment;
  11. import net.fabricmc.loader.api.FabricLoader;
  12. import net.fabricmc.loader.api.metadata.ModMetadata;
  13. import java.util.LinkedHashSet;
  14. import java.util.Objects;
  15. /**
  16. * The base config class. Inherit this class to create a config for your mod.
  17. */
  18. @Log4j2(topic = "CompleteConfig")
  19. public class Config extends BaseCollection {
  20. /**
  21. * Creates a new config builder for the specified mod.
  22. *
  23. * @param modId the ID of the mod creating the config
  24. *
  25. * @deprecated Please subclass {@link Config}
  26. */
  27. @Deprecated
  28. public static Builder builder(String modId) {
  29. return new Builder(modId);
  30. }
  31. @Getter(AccessLevel.PACKAGE)
  32. private final ConfigSource source;
  33. @Environment(EnvType.CLIENT)
  34. private TranslationKey translation;
  35. /**
  36. * Creates a config with the specified branch.
  37. *
  38. * <p>The branch determines the location of the config's save file and has to be mod-unique.
  39. *
  40. * @param modId the ID of the mod creating the config
  41. * @param branch the branch
  42. * @param saveOnExit whether to save the config when the client or server stops
  43. */
  44. public Config(String modId, String[] branch, boolean saveOnExit) {
  45. source = new ConfigSource(modId, branch);
  46. ConfigRegistry.register(this);
  47. if (saveOnExit) {
  48. Runtime.getRuntime().addShutdownHook(new Thread(this::save));
  49. }
  50. }
  51. /**
  52. * Creates a config with the default branch.
  53. *
  54. * @param modId the ID of the mod creating the config
  55. * @param saveOnExit whether to save the config when the client or server stops
  56. */
  57. public Config(String modId, boolean saveOnExit) {
  58. this(modId, new String[0], saveOnExit);
  59. }
  60. public ModMetadata getMod() {
  61. return FabricLoader.getInstance().getModContainer(source.getModId()).get().getMetadata();
  62. }
  63. @Override
  64. public TranslationKey getTranslation() {
  65. return getTranslation(false);
  66. }
  67. @Environment(EnvType.CLIENT)
  68. public TranslationKey getTranslation(boolean includeBranch) {
  69. if (translation == null) {
  70. translation = TranslationKey.from(this);
  71. }
  72. if (includeBranch) {
  73. return translation.append(source.getBranch());
  74. }
  75. return translation;
  76. }
  77. public Config add(ConfigContainer... containers) {
  78. resolve(containers);
  79. return this;
  80. }
  81. public Config load() {
  82. if(!isEmpty()) {
  83. source.load(this);
  84. }
  85. return this;
  86. }
  87. /**
  88. * Saves the config.
  89. */
  90. public void save() {
  91. if(isEmpty()) return;
  92. source.save(this);
  93. }
  94. @Log4j2(topic = "CompleteConfig")
  95. @Deprecated
  96. public final static class Builder {
  97. private final String modId;
  98. private String[] branch = new String[0];
  99. private final LinkedHashSet<ConfigContainer> children = new LinkedHashSet<>();
  100. private boolean main;
  101. private boolean saveOnExit;
  102. private Builder(String modId) {
  103. this.modId = modId;
  104. }
  105. /**
  106. * Sets the branch. Every config of a mod needs a unique branch, therefore setting a branch is only required
  107. * when using more than one config.
  108. *
  109. * <p>The branch determines the location of the config's save file.
  110. *
  111. * @param branch the branch
  112. * @return this builder
  113. */
  114. @Deprecated
  115. public Builder setBranch(String[] branch) {
  116. this.branch = branch;
  117. return this;
  118. }
  119. /**
  120. * Adds one or more containers to the config.
  121. *
  122. * @param containers one or more containers
  123. * @return this builder
  124. *
  125. * @deprecated Add the containers transitively to the config object
  126. */
  127. @Deprecated
  128. public Builder add(@NonNull ConfigContainer... containers) {
  129. for (ConfigContainer container : containers) {
  130. if (!children.add(Objects.requireNonNull(container))) {
  131. throw new IllegalArgumentException("Duplicate container " + container.getClass().getSimpleName());
  132. }
  133. }
  134. return this;
  135. }
  136. /**
  137. * Sets a flag to save the config when the game closes.
  138. *
  139. * @return this builder
  140. */
  141. @Deprecated
  142. public Builder saveOnExit() {
  143. saveOnExit = true;
  144. return this;
  145. }
  146. /**
  147. * Registers the config as main mod config.
  148. *
  149. * @return this builder
  150. *
  151. * @deprecated Use {@link ConfigRegistry#setMainConfig(Config)}
  152. */
  153. @Deprecated
  154. public Builder main() {
  155. main = true;
  156. return this;
  157. }
  158. /**
  159. * Creates and loads the config.
  160. *
  161. * @return the created config, or null if empty
  162. */
  163. @Deprecated
  164. public Config build() {
  165. Config config = new Config(modId, branch, saveOnExit).add(children.toArray(new ConfigContainer[0])).load();
  166. if (main) {
  167. ConfigRegistry.setMainConfig(config);
  168. }
  169. return config;
  170. }
  171. }
  172. }