PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/common/buildcraft/lib/registry/TagManager.java

https://github.com/2xsaiko/BuildCraft
Java | 206 lines | 152 code | 36 blank | 18 comment | 18 complexity | b31d366a2b8bfc2478dd0d29e7538b2c MD5 | raw file
Possible License(s): LGPL-2.0
  1. /* Copyright (c) 2016 SpaceToad and the BuildCraft team
  2. *
  3. * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
  4. * distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. */
  5. package buildcraft.lib.registry;
  6. import java.util.ArrayDeque;
  7. import java.util.ArrayList;
  8. import java.util.Deque;
  9. import java.util.EnumMap;
  10. import java.util.HashMap;
  11. import java.util.LinkedList;
  12. import java.util.List;
  13. import java.util.Map;
  14. import java.util.function.Consumer;
  15. import net.minecraft.item.Item;
  16. import net.minecraft.util.ResourceLocation;
  17. import net.minecraftforge.fml.common.registry.ForgeRegistries;
  18. /** Stores several types of "tag" (strings) for BuildCraft. A central place for all of them to init in. Refer to the
  19. * "static" block for all of the tag ID's
  20. *
  21. * You are free to add your own tags (say for addons) but it is recommended that you include your addon name somewhere
  22. * near the start - we don't want name clashes between addons or an addon and BC itself. If you want more types of tags
  23. * keys then just make an issue for it, and it will probably be added. */
  24. public class TagManager {
  25. private static final Map<String, TagEntry> idsToEntry = new HashMap<>();
  26. public static Item getItem(String id) {
  27. String regTag = getTag(id, EnumTagType.REGISTRY_NAME);
  28. ResourceLocation loc = new ResourceLocation(regTag);
  29. if (ForgeRegistries.ITEMS.containsKey(loc)) {
  30. return ForgeRegistries.ITEMS.getValue(loc);
  31. } else {
  32. return null;
  33. }
  34. }
  35. // getBlock?
  36. public static String getTag(String id, EnumTagType type) {
  37. if (idsToEntry.containsKey(id)) {
  38. TagEntry entry = idsToEntry.get(id);
  39. return entry.getSingleTag(type);
  40. } else {
  41. throw new IllegalArgumentException("Unknown id " + id);
  42. }
  43. }
  44. public static boolean hasTag(String id, EnumTagType type) {
  45. if (idsToEntry.containsKey(id)) {
  46. TagEntry entry = idsToEntry.get(id);
  47. return entry.tags.containsKey(type);
  48. } else {
  49. throw new IllegalArgumentException("Unknown id " + id);
  50. }
  51. }
  52. public static String[] getMultiTag(String id, EnumTagTypeMulti type) {
  53. if (idsToEntry.containsKey(id)) {
  54. TagEntry entry = idsToEntry.get(id);
  55. return entry.getMultiTag(type);
  56. } else {
  57. throw new IllegalArgumentException("Unknown id " + id);
  58. }
  59. }
  60. // hasMultiTag?
  61. public enum EnumTagType {
  62. UNLOCALIZED_NAME,
  63. OREDICT_NAME,
  64. REGISTRY_NAME,
  65. CREATIVE_TAB,
  66. MODEL_LOCATION,
  67. }
  68. public enum EnumTagTypeMulti {
  69. OLD_REGISTRY_NAME,
  70. }
  71. public static class TagEntry {
  72. /** The actual ID */
  73. public final String id;
  74. private final Map<EnumTagType, String> tags = new EnumMap<>(EnumTagType.class);
  75. private final Map<EnumTagTypeMulti, List<String>> multiTags = new EnumMap<>(EnumTagTypeMulti.class);
  76. public TagEntry(String id) {
  77. this.id = id;
  78. }
  79. public String getSingleTag(EnumTagType type) {
  80. if (!tags.containsKey(type)) throw new IllegalArgumentException("Unknown tag type " + type + " for the entry " + id);
  81. return tags.get(type);
  82. }
  83. public boolean hasSingleTag(EnumTagType type) {
  84. return tags.containsKey(type);
  85. }
  86. public String[] getMultiTag(EnumTagTypeMulti type) {
  87. List<String> ts = multiTags.get(type);
  88. if (ts == null) return new String[0];
  89. return ts.toArray(new String[ts.size()]);
  90. }
  91. public TagEntry setSingleTag(EnumTagType type, String tag) {
  92. tags.put(type, tag);
  93. return this;
  94. }
  95. public TagEntry reg(String name) {
  96. return setSingleTag(EnumTagType.REGISTRY_NAME, name);
  97. }
  98. public TagEntry locale(String name) {
  99. return setSingleTag(EnumTagType.UNLOCALIZED_NAME, name);
  100. }
  101. public TagEntry oreDict(String name) {
  102. return setSingleTag(EnumTagType.OREDICT_NAME, name);
  103. }
  104. public TagEntry tab(String creativeTab) {
  105. return setSingleTag(EnumTagType.CREATIVE_TAB, creativeTab);
  106. }
  107. public TagEntry model(String modelLocation) {
  108. return setSingleTag(EnumTagType.MODEL_LOCATION, modelLocation);
  109. }
  110. public TagEntry addMultiTag(EnumTagTypeMulti type, String... tags) {
  111. if (!this.tags.containsKey(type)) {
  112. this.multiTags.put(type, new LinkedList<>());
  113. }
  114. for (String tag : tags) {
  115. multiTags.get(type).add(tag);
  116. }
  117. return this;
  118. }
  119. public TagEntry oldReg(String... tags) {
  120. return addMultiTag(EnumTagTypeMulti.OLD_REGISTRY_NAME, tags);
  121. }
  122. }
  123. public static TagEntry getTag(String id) {
  124. return idsToEntry.get(id);
  125. }
  126. public static TagEntry registerTag(String id) {
  127. TagEntry entry = new TagEntry(id);
  128. idsToEntry.put(id, entry);
  129. for (List<TagEntry> list : batchTasks) {
  130. list.add(entry);
  131. }
  132. return entry;
  133. }
  134. // #########################
  135. //
  136. // Batching repetitive tags
  137. //
  138. // #########################
  139. private static final Deque<List<TagEntry>> batchTasks = new ArrayDeque<>();
  140. public static void startBatch() {
  141. batchTasks.push(new ArrayList<>());
  142. }
  143. public static void endBatch(Consumer<TagEntry> consumer) {
  144. batchTasks.pop().forEach(consumer);
  145. }
  146. public static Consumer<TagEntry> prependTag(EnumTagType type, String prefix) {
  147. return tag -> {
  148. if (tag.hasSingleTag(type)) {
  149. tag.setSingleTag(type, prefix + tag.getSingleTag(type));
  150. }
  151. };
  152. }
  153. public static Consumer<TagEntry> prependTags(String prefix, EnumTagType... tags) {
  154. Consumer<TagEntry> consumer = tag -> {};
  155. for (EnumTagType type : tags) {
  156. consumer = consumer.andThen(prependTag(type, prefix));
  157. }
  158. return consumer;
  159. }
  160. public static Consumer<TagEntry> set(EnumTagType type, String value) {
  161. return tag -> tag.setSingleTag(type, value);
  162. }
  163. public static Consumer<TagEntry> setTab(String creativeTab) {
  164. return tag -> {
  165. if (tag.hasSingleTag(EnumTagType.REGISTRY_NAME) && !tag.hasSingleTag(EnumTagType.CREATIVE_TAB)) {
  166. tag.tab(creativeTab);
  167. }
  168. };
  169. }
  170. }