Config.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package me.lortseam.completeconfig.data;
  2. import me.lortseam.completeconfig.ConfigHandler;
  3. import me.lortseam.completeconfig.ModController;
  4. import me.lortseam.completeconfig.gui.GuiBuilder;
  5. import me.lortseam.completeconfig.io.ConfigSource;
  6. import me.lortseam.completeconfig.api.ConfigGroup;
  7. import me.lortseam.completeconfig.data.gui.TranslationIdentifier;
  8. import me.lortseam.completeconfig.util.TypeUtils;
  9. import net.fabricmc.api.EnvType;
  10. import net.fabricmc.api.Environment;
  11. import org.apache.logging.log4j.LogManager;
  12. import org.apache.logging.log4j.Logger;
  13. import org.spongepowered.configurate.serialize.TypeSerializerCollection;
  14. import java.util.ArrayList;
  15. import java.util.Arrays;
  16. import java.util.List;
  17. import java.util.Objects;
  18. public class Config extends CollectionMap {
  19. /**
  20. * Creates a new config builder for the specified mod.
  21. *
  22. * @param modID the ID of the mod creating the config
  23. */
  24. public static Builder builder(String modID) {
  25. return new Builder(modID);
  26. }
  27. private final ConfigSource source;
  28. private Config(ConfigSource source, List<ConfigGroup> topLevelGroups) {
  29. super(new TranslationIdentifier(source.getModID()));
  30. this.source = source;
  31. for (ConfigGroup group : topLevelGroups) {
  32. resolve(group);
  33. }
  34. }
  35. public String getModID() {
  36. return source.getModID();
  37. }
  38. public TranslationIdentifier getTranslation() {
  39. return translation;
  40. }
  41. public void load() {
  42. source.load(this);
  43. }
  44. public void save() {
  45. source.save(this);
  46. }
  47. public final static class Builder {
  48. private static final Logger LOGGER = LogManager.getLogger();
  49. private final String modID;
  50. private String[] branch = new String[0];
  51. private final List<ConfigGroup> topLevelGroups = new ArrayList<>();
  52. private TypeSerializerCollection typeSerializers;
  53. private GuiBuilder guiBuilder;
  54. private Builder(String modID) {
  55. this.modID = Objects.requireNonNull(modID);
  56. }
  57. /**
  58. * Sets the branch.
  59. *
  60. * <p>The branch determines the location of the config's save file.
  61. *
  62. * @param branch the branch
  63. * @return this builder
  64. */
  65. public Builder setBranch(String[] branch) {
  66. this.branch = Objects.requireNonNull(branch);
  67. return this;
  68. }
  69. /**
  70. * Adds one or more top-level groups to the config.
  71. *
  72. * @param groups one or more top-level groups
  73. * @return this builder
  74. */
  75. public Builder add(ConfigGroup... groups) {
  76. Arrays.stream(groups).forEach(Objects::requireNonNull);
  77. topLevelGroups.addAll(Arrays.asList(groups));
  78. return this;
  79. }
  80. /**
  81. * Registers custom type serializers, applied only to this config.
  82. *
  83. * <p>To register type serializers for every config of your mod, use
  84. * {@link ModController#registerTypeSerializers(TypeSerializerCollection)}.
  85. *
  86. * @param typeSerializers the type serializers
  87. * @return this builder
  88. */
  89. public Builder registerTypeSerializers(TypeSerializerCollection typeSerializers) {
  90. this.typeSerializers = TypeUtils.mergeSerializers(this.typeSerializers, Objects.requireNonNull(typeSerializers));
  91. return this;
  92. }
  93. /**
  94. * Sets a custom client GUI builder for the config.
  95. *
  96. * @param guiBuilder a GUI builder
  97. * @return this builder
  98. */
  99. @Environment(EnvType.CLIENT)
  100. public Builder setGuiBuilder(GuiBuilder guiBuilder) {
  101. this.guiBuilder = Objects.requireNonNull(guiBuilder);
  102. return this;
  103. }
  104. /**
  105. * Completes the config creation.
  106. *
  107. * @return the handler associated with the created config
  108. */
  109. public ConfigHandler build() {
  110. if (topLevelGroups.isEmpty()) {
  111. LOGGER.warn("[CompleteConfig] Mod " + modID + " tried to create an empty config!");
  112. return null;
  113. }
  114. return new ConfigHandler(new Config(new ConfigSource(ModController.of(modID), branch, typeSerializers), topLevelGroups), guiBuilder);
  115. }
  116. }
  117. }