|
1 | 1 | package gregtech.api.capability; |
2 | 2 |
|
| 3 | +import gregtech.common.covers.ender.CoverAbstractEnderLink; |
| 4 | +import gregtech.common.metatileentities.multi.multiblockpart.MetaTileEntityFluidHatch; |
| 5 | + |
3 | 6 | import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap; |
4 | 7 | import it.unimi.dsi.fastutil.ints.Int2ObjectMap; |
| 8 | +import org.apache.commons.lang3.ArrayUtils; |
5 | 9 |
|
6 | 10 | import java.lang.reflect.Field; |
| 11 | +import java.lang.reflect.Modifier; |
7 | 12 |
|
8 | 13 | public class GregtechDataCodes { |
9 | 14 |
|
@@ -190,15 +195,34 @@ public static int assignId() { |
190 | 195 | public static final Int2ObjectMap<String> NAMES = new Int2ObjectArrayMap<>(); |
191 | 196 |
|
192 | 197 | static { |
| 198 | + registerFields(GregtechDataCodes.class); |
| 199 | + // todo these really should be moved to this class |
| 200 | + registerFields(CoverAbstractEnderLink.class, CoverAbstractEnderLink.UPDATE_PRIVATE); |
| 201 | + registerFields(MetaTileEntityFluidHatch.class, MetaTileEntityFluidHatch.LOCK_FILL); |
| 202 | + } |
| 203 | + |
| 204 | + public static String getNameFor(int id) { |
| 205 | + return NAMES.getOrDefault(id, "Unknown_DataCode:" + id); |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Registers all fields from the passed in class to the name registry. |
| 210 | + * Optionally, you can pass in a list of valid ids to check against so that errant ids are not added |
| 211 | + * |
| 212 | + * @param clazz Class to iterate fields |
| 213 | + * @param validIds optional array of valid ids to check against class fields |
| 214 | + */ |
| 215 | + public static void registerFields(Class<?> clazz, int... validIds) { |
193 | 216 | try { |
194 | | - for (Field field : GregtechDataCodes.class.getFields()) { |
| 217 | + for (Field field : clazz.getDeclaredFields()) { |
195 | 218 | if (field.getType() != Integer.TYPE) continue; |
196 | | - NAMES.put(field.getInt(null), field.getName()); |
| 219 | + if (!Modifier.isStatic(field.getModifiers())) continue; |
| 220 | + if (!Modifier.isFinal(field.getModifiers())) continue; |
| 221 | + int id = field.getInt(null); |
| 222 | + if (!ArrayUtils.isEmpty(validIds) && !ArrayUtils.contains(validIds, id)) |
| 223 | + continue; |
| 224 | + NAMES.put(id, field.getName() + ":" + id); |
197 | 225 | } |
198 | 226 | } catch (IllegalAccessException ignored) {} |
199 | 227 | } |
200 | | - |
201 | | - public static String getNameFor(int id) { |
202 | | - return NAMES.getOrDefault(id, "Unknown DataCode: " + id); |
203 | | - } |
204 | 228 | } |
0 commit comments