tterrag1098

Untitled

Jan 1st, 2019
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 KB | None | 0 0
  1.   @Override
  2.   @Nullable
  3.   public IForgeRegistryEntry read(Registry registry, Set<NBTAction> phase, NBTTagCompound nbt, Type type, String name, @Nullable IForgeRegistryEntry object)
  4.       throws IllegalArgumentException, IllegalAccessException, InstantiationException, NoHandlerFoundException {
  5.     IForgeRegistry reg = findRegistry(type);
  6.     ResourceLocation rl = new ResourceLocation(nbt.getString(name));
  7.     IForgeRegistryEntry ret = reg.getValue(rl);
  8.     return ret;
  9.   }
  10.  
  11.   @SuppressWarnings("unchecked")
  12.   private IForgeRegistry findRegistry(Type type) {
  13.     Class<?> cls = TypeUtil.toClass(type);
  14.     return NullHelper.notnullJ(registryCache.computeIfAbsent(cls, $ -> {
  15.       Iterator<@NonnullType Type> iter = walkHierarchy(type);
  16.       while (iter.hasNext()) {
  17.         Type t = iter.next();
  18.         Class<?> c = TypeUtil.toClass(t);
  19.         if (c == IForgeRegistryEntry.class || c == IForgeRegistryEntry.Impl.class) {
  20.           Type[] types = ((ParameterizedType)t).getActualTypeArguments();
  21.           return GameRegistry.findRegistry((Class) TypeUtil.toClass(NullHelper.notnullJ(types[0], "ParameterizedType#getActualTypeArguments[i]")));
  22.         }
  23.       }
  24.       throw new IllegalArgumentException("Not a registry object subtype");
  25.     }), "Map#computeIfAbsent");
  26.   }
  27.  
  28.   private Iterator<Type> walkHierarchy(Type cls) {
  29.     return new Iterator<Type>() {
  30.  
  31.       Set<Class<?>>                 seenInterfaces = new HashSet<>();
  32.       ArrayDeque<@NonnullType Type> queue          = new ArrayDeque<>();
  33.       {
  34.         queue.add(cls);
  35.       }
  36.  
  37.       @Override
  38.       public boolean hasNext() {
  39.         return !queue.isEmpty();
  40.       }
  41.  
  42.       @Override
  43.       public Type next() {
  44.         if (hasNext()) {
  45.           Type ret = queue.pop();
  46.           Class<?> cls = TypeUtil.toClass(ret);
  47.           if (cls != Object.class && !cls.isInterface()) {
  48.             queue.push(NullHelper.notnullJ(cls.getGenericSuperclass(), "Class#getGenericSuperclass"));
  49.             for (Type itf : cls.getGenericInterfaces()) {
  50.               if (seenInterfaces.add(TypeUtil.toClass(NullHelper.notnullJ(itf, "Class#getGenericInterfaces")))) {
  51.                 queue.push(NullHelper.notnullJ(itf, "Class#getGenericInterfaces"));
  52.               }
  53.             }
  54.           }
  55.           return ret;
  56.         }
  57.         throw new NoSuchElementException();
  58.       }
  59.     };
  60.   }
Advertisement
Add Comment
Please, Sign In to add comment