ConfigBuilderImpl.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package me.shedaniel.clothconfig2.impl;
  2. import com.google.common.collect.Lists;
  3. import com.google.common.collect.Maps;
  4. import me.shedaniel.clothconfig2.api.ConfigBuilder;
  5. import me.shedaniel.clothconfig2.api.ConfigCategory;
  6. import me.shedaniel.clothconfig2.gui.ClothConfigScreen;
  7. import net.fabricmc.api.EnvType;
  8. import net.fabricmc.api.Environment;
  9. import net.minecraft.client.gui.DrawableHelper;
  10. import net.minecraft.client.gui.screen.Screen;
  11. import net.minecraft.client.resource.language.I18n;
  12. import net.minecraft.util.Identifier;
  13. import net.minecraft.util.Pair;
  14. import java.util.List;
  15. import java.util.Map;
  16. import java.util.Objects;
  17. import java.util.function.Consumer;
  18. @Deprecated
  19. @Environment(EnvType.CLIENT)
  20. public class ConfigBuilderImpl implements ConfigBuilder {
  21. private Runnable savingRunnable;
  22. private Screen parent;
  23. private String title = "text.cloth-config.config";
  24. private boolean editable = true;
  25. private boolean tabsSmoothScroll = true;
  26. private boolean listSmoothScroll = true;
  27. private boolean doesProcessErrors = true;
  28. private boolean doesConfirmSave = true;
  29. private boolean transparentBackground = false;
  30. private Identifier defaultBackground = DrawableHelper.BACKGROUND_LOCATION;
  31. private Consumer<Screen> afterInitConsumer = screen -> {};
  32. private final Map<String, Identifier> categoryBackground = Maps.newHashMap();
  33. private final Map<String, List<Pair<String, Object>>> dataMap = Maps.newLinkedHashMap();
  34. private String fallbackCategory = null;
  35. private boolean alwaysShowTabs = false;
  36. @Deprecated
  37. public ConfigBuilderImpl() {
  38. }
  39. @Override
  40. public boolean isAlwaysShowTabs() {
  41. return alwaysShowTabs;
  42. }
  43. @Override
  44. public ConfigBuilder setAlwaysShowTabs(boolean alwaysShowTabs) {
  45. this.alwaysShowTabs = alwaysShowTabs;
  46. return this;
  47. }
  48. @Override
  49. public ConfigBuilder setTransparentBackground(boolean transparentBackground) {
  50. this.transparentBackground = transparentBackground;
  51. return this;
  52. }
  53. @Override
  54. public ConfigBuilder setAfterInitConsumer(Consumer<Screen> afterInitConsumer) {
  55. this.afterInitConsumer = afterInitConsumer;
  56. return this;
  57. }
  58. @Override
  59. public ConfigBuilder setFallbackCategory(ConfigCategory fallbackCategory) {
  60. this.fallbackCategory = Objects.requireNonNull(fallbackCategory).getCategoryKey();
  61. return this;
  62. }
  63. @Override
  64. public Screen getParentScreen() {
  65. return parent;
  66. }
  67. @Override
  68. public ConfigBuilder setParentScreen(Screen parent) {
  69. this.parent = parent;
  70. return this;
  71. }
  72. @Override
  73. public String getTitle() {
  74. return title;
  75. }
  76. @Override
  77. public ConfigBuilder setTitle(String title) {
  78. this.title = title;
  79. return this;
  80. }
  81. @Override
  82. public boolean isEditable() {
  83. return editable;
  84. }
  85. @Override
  86. public ConfigBuilder setEditable(boolean editable) {
  87. this.editable = editable;
  88. return this;
  89. }
  90. @Override
  91. public ConfigCategory getOrCreateCategory(String categoryKey) {
  92. if (dataMap.containsKey(categoryKey))
  93. return new ConfigCategoryImpl(categoryKey, identifier -> {
  94. if (transparentBackground)
  95. throw new IllegalStateException("Cannot set category background if screen is using transparent background.");
  96. categoryBackground.put(categoryKey, identifier);
  97. }, () -> dataMap.get(categoryKey), () -> removeCategory(categoryKey));
  98. dataMap.put(categoryKey, Lists.newArrayList());
  99. if (fallbackCategory == null)
  100. fallbackCategory = categoryKey;
  101. return new ConfigCategoryImpl(categoryKey, identifier -> {
  102. if (transparentBackground)
  103. throw new IllegalStateException("Cannot set category background if screen is using transparent background.");
  104. categoryBackground.put(categoryKey, identifier);
  105. }, () -> dataMap.get(categoryKey), () -> removeCategory(categoryKey));
  106. }
  107. @Override
  108. public ConfigBuilder removeCategory(String category) {
  109. if (dataMap.containsKey(category) && fallbackCategory.equals(category))
  110. fallbackCategory = null;
  111. if (!dataMap.containsKey(category))
  112. throw new NullPointerException("Category doesn't exist!");
  113. dataMap.remove(category);
  114. return this;
  115. }
  116. @Override
  117. public ConfigBuilder removeCategoryIfExists(String category) {
  118. if (dataMap.containsKey(category) && fallbackCategory.equals(category))
  119. fallbackCategory = null;
  120. dataMap.remove(category);
  121. return this;
  122. }
  123. @Override
  124. public boolean hasCategory(String category) {
  125. return dataMap.containsKey(category);
  126. }
  127. @Override
  128. public ConfigBuilder setShouldTabsSmoothScroll(boolean shouldTabsSmoothScroll) {
  129. this.tabsSmoothScroll = shouldTabsSmoothScroll;
  130. return this;
  131. }
  132. @Override
  133. public boolean isTabsSmoothScrolling() {
  134. return tabsSmoothScroll;
  135. }
  136. @Override
  137. public ConfigBuilder setShouldListSmoothScroll(boolean shouldListSmoothScroll) {
  138. this.listSmoothScroll = shouldListSmoothScroll;
  139. return this;
  140. }
  141. @Override
  142. public boolean isListSmoothScrolling() {
  143. return listSmoothScroll;
  144. }
  145. @Override
  146. public ConfigBuilder setDoesConfirmSave(boolean confirmSave) {
  147. this.doesConfirmSave = confirmSave;
  148. return this;
  149. }
  150. @Override
  151. public boolean doesConfirmSave() {
  152. return doesConfirmSave;
  153. }
  154. @Override
  155. public ConfigBuilder setDoesProcessErrors(boolean processErrors) {
  156. this.doesProcessErrors = processErrors;
  157. return this;
  158. }
  159. @Override
  160. public boolean doesProcessErrors() {
  161. return doesProcessErrors;
  162. }
  163. @Override
  164. public Identifier getDefaultBackgroundTexture() {
  165. return defaultBackground;
  166. }
  167. @Override
  168. public ConfigBuilder setDefaultBackgroundTexture(Identifier texture) {
  169. this.defaultBackground = texture;
  170. return this;
  171. }
  172. @Override
  173. public ConfigBuilder setSavingRunnable(Runnable runnable) {
  174. this.savingRunnable = runnable;
  175. return this;
  176. }
  177. @Override
  178. public Consumer<Screen> getAfterInitConsumer() {
  179. return afterInitConsumer;
  180. }
  181. @Override
  182. public Screen build() {
  183. if (dataMap.isEmpty() || fallbackCategory == null)
  184. throw new NullPointerException("There cannot be no categories or fallback category!");
  185. ClothConfigScreen screen = new ClothConfigScreen(parent, I18n.translate(title), dataMap, doesConfirmSave, doesProcessErrors, listSmoothScroll, defaultBackground, categoryBackground) {
  186. @Override
  187. public void save() {
  188. if (savingRunnable != null)
  189. savingRunnable.run();
  190. }
  191. @Override
  192. protected void init() {
  193. super.init();
  194. afterInitConsumer.accept(this);
  195. }
  196. };
  197. screen.setEditable(editable);
  198. screen.setFallbackCategory(fallbackCategory);
  199. screen.setSmoothScrollingTabs(tabsSmoothScroll);
  200. screen.setTransparentBackground(transparentBackground);
  201. screen.setAlwaysShowTabs(alwaysShowTabs);
  202. return screen;
  203. }
  204. @Override
  205. public Runnable getSavingRunnable() {
  206. return savingRunnable;
  207. }
  208. }