PageRenderTime 21ms CodeModel.GetById 2ms app.highlight 16ms 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
  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/. */
  5package buildcraft.lib.registry;
  6
  7import java.util.ArrayDeque;
  8import java.util.ArrayList;
  9import java.util.Deque;
 10import java.util.EnumMap;
 11import java.util.HashMap;
 12import java.util.LinkedList;
 13import java.util.List;
 14import java.util.Map;
 15import java.util.function.Consumer;
 16
 17import net.minecraft.item.Item;
 18import net.minecraft.util.ResourceLocation;
 19
 20import net.minecraftforge.fml.common.registry.ForgeRegistries;
 21
 22/** Stores several types of "tag" (strings) for BuildCraft. A central place for all of them to init in. Refer to the
 23 * "static" block for all of the tag ID's
 24 * 
 25 * You are free to add your own tags (say for addons) but it is recommended that you include your addon name somewhere
 26 * near the start - we don't want name clashes between addons or an addon and BC itself. If you want more types of tags
 27 * keys then just make an issue for it, and it will probably be added. */
 28public class TagManager {
 29    private static final Map<String, TagEntry> idsToEntry = new HashMap<>();
 30
 31    public static Item getItem(String id) {
 32        String regTag = getTag(id, EnumTagType.REGISTRY_NAME);
 33        ResourceLocation loc = new ResourceLocation(regTag);
 34        if (ForgeRegistries.ITEMS.containsKey(loc)) {
 35            return ForgeRegistries.ITEMS.getValue(loc);
 36        } else {
 37            return null;
 38        }
 39    }
 40
 41    // getBlock?
 42
 43    public static String getTag(String id, EnumTagType type) {
 44        if (idsToEntry.containsKey(id)) {
 45            TagEntry entry = idsToEntry.get(id);
 46            return entry.getSingleTag(type);
 47        } else {
 48            throw new IllegalArgumentException("Unknown id " + id);
 49        }
 50    }
 51
 52    public static boolean hasTag(String id, EnumTagType type) {
 53        if (idsToEntry.containsKey(id)) {
 54            TagEntry entry = idsToEntry.get(id);
 55            return entry.tags.containsKey(type);
 56        } else {
 57            throw new IllegalArgumentException("Unknown id " + id);
 58        }
 59    }
 60
 61    public static String[] getMultiTag(String id, EnumTagTypeMulti type) {
 62        if (idsToEntry.containsKey(id)) {
 63            TagEntry entry = idsToEntry.get(id);
 64            return entry.getMultiTag(type);
 65        } else {
 66            throw new IllegalArgumentException("Unknown id " + id);
 67        }
 68    }
 69
 70    // hasMultiTag?
 71
 72    public enum EnumTagType {
 73        UNLOCALIZED_NAME,
 74        OREDICT_NAME,
 75        REGISTRY_NAME,
 76        CREATIVE_TAB,
 77        MODEL_LOCATION,
 78    }
 79
 80    public enum EnumTagTypeMulti {
 81        OLD_REGISTRY_NAME,
 82    }
 83
 84    public static class TagEntry {
 85        /** The actual ID */
 86        public final String id;
 87        private final Map<EnumTagType, String> tags = new EnumMap<>(EnumTagType.class);
 88        private final Map<EnumTagTypeMulti, List<String>> multiTags = new EnumMap<>(EnumTagTypeMulti.class);
 89
 90        public TagEntry(String id) {
 91            this.id = id;
 92
 93        }
 94
 95        public String getSingleTag(EnumTagType type) {
 96            if (!tags.containsKey(type)) throw new IllegalArgumentException("Unknown tag type " + type + " for the entry " + id);
 97            return tags.get(type);
 98        }
 99
100        public boolean hasSingleTag(EnumTagType type) {
101            return tags.containsKey(type);
102        }
103
104        public String[] getMultiTag(EnumTagTypeMulti type) {
105            List<String> ts = multiTags.get(type);
106            if (ts == null) return new String[0];
107            return ts.toArray(new String[ts.size()]);
108        }
109
110        public TagEntry setSingleTag(EnumTagType type, String tag) {
111            tags.put(type, tag);
112            return this;
113        }
114
115        public TagEntry reg(String name) {
116            return setSingleTag(EnumTagType.REGISTRY_NAME, name);
117        }
118
119        public TagEntry locale(String name) {
120            return setSingleTag(EnumTagType.UNLOCALIZED_NAME, name);
121        }
122
123        public TagEntry oreDict(String name) {
124            return setSingleTag(EnumTagType.OREDICT_NAME, name);
125        }
126
127        public TagEntry tab(String creativeTab) {
128            return setSingleTag(EnumTagType.CREATIVE_TAB, creativeTab);
129        }
130
131        public TagEntry model(String modelLocation) {
132            return setSingleTag(EnumTagType.MODEL_LOCATION, modelLocation);
133        }
134
135        public TagEntry addMultiTag(EnumTagTypeMulti type, String... tags) {
136            if (!this.tags.containsKey(type)) {
137                this.multiTags.put(type, new LinkedList<>());
138            }
139            for (String tag : tags) {
140                multiTags.get(type).add(tag);
141            }
142            return this;
143        }
144
145        public TagEntry oldReg(String... tags) {
146            return addMultiTag(EnumTagTypeMulti.OLD_REGISTRY_NAME, tags);
147        }
148    }
149
150    public static TagEntry getTag(String id) {
151        return idsToEntry.get(id);
152    }
153
154    public static TagEntry registerTag(String id) {
155        TagEntry entry = new TagEntry(id);
156        idsToEntry.put(id, entry);
157        for (List<TagEntry> list : batchTasks) {
158            list.add(entry);
159        }
160        return entry;
161    }
162
163    // #########################
164    //
165    // Batching repetitive tags
166    //
167    // #########################
168
169    private static final Deque<List<TagEntry>> batchTasks = new ArrayDeque<>();
170
171    public static void startBatch() {
172        batchTasks.push(new ArrayList<>());
173    }
174
175    public static void endBatch(Consumer<TagEntry> consumer) {
176        batchTasks.pop().forEach(consumer);
177    }
178
179    public static Consumer<TagEntry> prependTag(EnumTagType type, String prefix) {
180        return tag -> {
181            if (tag.hasSingleTag(type)) {
182                tag.setSingleTag(type, prefix + tag.getSingleTag(type));
183            }
184        };
185    }
186
187    public static Consumer<TagEntry> prependTags(String prefix, EnumTagType... tags) {
188        Consumer<TagEntry> consumer = tag -> {};
189        for (EnumTagType type : tags) {
190            consumer = consumer.andThen(prependTag(type, prefix));
191        }
192        return consumer;
193    }
194
195    public static Consumer<TagEntry> set(EnumTagType type, String value) {
196        return tag -> tag.setSingleTag(type, value);
197    }
198
199    public static Consumer<TagEntry> setTab(String creativeTab) {
200        return tag -> {
201            if (tag.hasSingleTag(EnumTagType.REGISTRY_NAME) && !tag.hasSingleTag(EnumTagType.CREATIVE_TAB)) {
202                tag.tab(creativeTab);
203            }
204        };
205    }
206}