Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- @Nullable
- public IForgeRegistryEntry read(Registry registry, Set<NBTAction> phase, NBTTagCompound nbt, Type type, String name, @Nullable IForgeRegistryEntry object)
- throws IllegalArgumentException, IllegalAccessException, InstantiationException, NoHandlerFoundException {
- IForgeRegistry reg = findRegistry(type);
- ResourceLocation rl = new ResourceLocation(nbt.getString(name));
- IForgeRegistryEntry ret = reg.getValue(rl);
- return ret;
- }
- @SuppressWarnings("unchecked")
- private IForgeRegistry findRegistry(Type type) {
- Class<?> cls = TypeUtil.toClass(type);
- return NullHelper.notnullJ(registryCache.computeIfAbsent(cls, $ -> {
- Iterator<@NonnullType Type> iter = walkHierarchy(type);
- while (iter.hasNext()) {
- Type t = iter.next();
- Class<?> c = TypeUtil.toClass(t);
- if (c == IForgeRegistryEntry.class || c == IForgeRegistryEntry.Impl.class) {
- Type[] types = ((ParameterizedType)t).getActualTypeArguments();
- return GameRegistry.findRegistry((Class) TypeUtil.toClass(NullHelper.notnullJ(types[0], "ParameterizedType#getActualTypeArguments[i]")));
- }
- }
- throw new IllegalArgumentException("Not a registry object subtype");
- }), "Map#computeIfAbsent");
- }
- private Iterator<Type> walkHierarchy(Type cls) {
- return new Iterator<Type>() {
- Set<Class<?>> seenInterfaces = new HashSet<>();
- ArrayDeque<@NonnullType Type> queue = new ArrayDeque<>();
- {
- queue.add(cls);
- }
- @Override
- public boolean hasNext() {
- return !queue.isEmpty();
- }
- @Override
- public Type next() {
- if (hasNext()) {
- Type ret = queue.pop();
- Class<?> cls = TypeUtil.toClass(ret);
- if (cls != Object.class && !cls.isInterface()) {
- queue.push(NullHelper.notnullJ(cls.getGenericSuperclass(), "Class#getGenericSuperclass"));
- for (Type itf : cls.getGenericInterfaces()) {
- if (seenInterfaces.add(TypeUtil.toClass(NullHelper.notnullJ(itf, "Class#getGenericInterfaces")))) {
- queue.push(NullHelper.notnullJ(itf, "Class#getGenericInterfaces"));
- }
- }
- }
- return ret;
- }
- throw new NoSuchElementException();
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment