Skip to content

Commit

Permalink
Fix zenregister causing issues with extended classes. Close #1311
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredlll08 committed Jun 29, 2021
1 parent c0ff692 commit c0e45d2
Showing 1 changed file with 13 additions and 1 deletion.
Expand Up @@ -23,12 +23,14 @@
import org.openzen.zencode.java.module.JavaNativeModule;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -43,8 +45,13 @@ public class CraftTweakerRegistry {
* Find all classes that have a {@link ZenRegister} annotation and registers them to the class list for loading.
*/
public static void findClasses() {

final CraftTweakerModList craftTweakerModList = new CraftTweakerModList();
final List<Class<?>> collect = getAllClassesWith(ZenRegister.class, craftTweakerModList::add)
final List<Class<?>> collect = getAllClassesWith(ZenRegister.class, craftTweakerModList::add, annotationData -> ((ArrayList<String>) annotationData
.getAnnotationData()
.getOrDefault("modDeps", new ArrayList<String>(0)))
.stream()
.allMatch(ModList.get()::isLoaded))
.filter(Objects::nonNull)
.collect(Collectors.toList());
craftTweakerModList.printToLog();
Expand Down Expand Up @@ -81,13 +88,18 @@ private static Stream<? extends Class<?>> getAllClassesWith(Class<? extends Anno
}

private static Stream<? extends Class<?>> getAllClassesWith(Class<? extends Annotation> annotationCls, Consumer<ModFileScanData> consumer) {
return getAllClassesWith(annotationCls, consumer, annotationData -> true);
}

private static Stream<? extends Class<?>> getAllClassesWith(Class<? extends Annotation> annotationCls, Consumer<ModFileScanData> consumer, Predicate<ModFileScanData.AnnotationData> annotationFilter) {
final Type annotationType = Type.getType(annotationCls);
return ModList.get()
.getAllScanData()
.stream()
.flatMap(scanData -> scanData.getAnnotations()
.stream()
.filter(a -> annotationType.equals(a.getAnnotationType()))
.filter(annotationFilter)
.peek(ignored -> consumer.accept(scanData))
.map(ModFileScanData.AnnotationData::getClassType))
.map(CraftTweakerRegistry::getClassFromType)
Expand Down

0 comments on commit c0e45d2

Please sign in to comment.