ConfigBuilderImpl.java 7.5 KB

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