|
| 1 | +package com.cake.azimuth.registration; |
| 2 | + |
| 3 | +import com.simibubi.create.foundation.data.CreateRegistrate; |
| 4 | +import com.tterrag.registrate.builders.BlockBuilder; |
| 5 | +import net.neoforged.fml.ModList; |
| 6 | +import net.neoforged.neoforgespi.language.ModFileScanData; |
| 7 | + |
| 8 | +import java.lang.annotation.ElementType; |
| 9 | +import java.lang.annotation.Retention; |
| 10 | +import java.lang.annotation.RetentionPolicy; |
| 11 | +import java.lang.annotation.Target; |
| 12 | +import java.lang.reflect.Method; |
| 13 | +import java.lang.reflect.Modifier; |
| 14 | +import java.util.Arrays; |
| 15 | +import java.util.Comparator; |
| 16 | +import java.util.LinkedHashMap; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.Objects; |
| 20 | +import java.util.function.Consumer; |
| 21 | + |
| 22 | +/** |
| 23 | + * Registry for block edits to be applied to Create blocks during registration. |
| 24 | + * Registrators are discovered and invoked via NeoForge scan data during Create's AllBlocks static initialization. |
| 25 | + */ |
| 26 | +public class CreateBlockEdits { |
| 27 | + |
| 28 | + private static final Map<String, Consumer<BlockBuilder<?, CreateRegistrate>>> EDITS_BY_ID = new LinkedHashMap<>(); |
| 29 | + private static RegistrationWindow registrationWindow = RegistrationWindow.NOT_STARTED; |
| 30 | + |
| 31 | + public static synchronized void bootstrapRegistrators() { |
| 32 | + if (registrationWindow != RegistrationWindow.NOT_STARTED) { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + final ModList modList = ModList.get(); |
| 37 | + if (modList == null) { |
| 38 | + throw new IllegalStateException("Cannot discover @CreateBlockEdits.Registrator methods before NeoForge ModList is available."); |
| 39 | + } |
| 40 | + |
| 41 | + registrationWindow = RegistrationWindow.OPEN; |
| 42 | + try { |
| 43 | + discoverRegistrators(modList).forEach(CreateBlockEdits::invokeRegistrator); |
| 44 | + } finally { |
| 45 | + registrationWindow = RegistrationWindow.CLOSED; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public static synchronized void forBlock(final String id, final Consumer<BlockBuilder<?, CreateRegistrate>> edit) { |
| 50 | + if (registrationWindow != RegistrationWindow.OPEN) { |
| 51 | + throw new IllegalStateException("CreateBlockEdits.forBlock(...) can only be called from a @CreateBlockEdits.Registrator method while Create's AllBlocks are bootstrapping; current registration window is " + registrationWindow + "."); |
| 52 | + } |
| 53 | + |
| 54 | + Objects.requireNonNull(id, "id"); |
| 55 | + Objects.requireNonNull(edit, "edit"); |
| 56 | + EDITS_BY_ID.merge(id, edit, (existing, additional) -> builder -> { |
| 57 | + existing.accept(builder); |
| 58 | + additional.accept(builder); |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + public static Consumer<BlockBuilder<?, CreateRegistrate>> getEditForId(final String id) { |
| 63 | + if (registrationWindow != RegistrationWindow.CLOSED) { |
| 64 | + throw new IllegalStateException("CreateBlockEdits.getEditForId(...) was called before registrators were fully discovered; current registration window is " + registrationWindow + "."); |
| 65 | + } |
| 66 | + |
| 67 | + return EDITS_BY_ID.get(id); |
| 68 | + } |
| 69 | + |
| 70 | + private static List<ModFileScanData.AnnotationData> discoverRegistrators(final ModList modList) { |
| 71 | + return modList.getAllScanData().stream() |
| 72 | + .flatMap(scanData -> scanData.getAnnotatedBy(Registrator.class, ElementType.METHOD)) |
| 73 | + .sorted(Comparator.comparing((ModFileScanData.AnnotationData data) -> data.clazz().getClassName()) |
| 74 | + .thenComparing(ModFileScanData.AnnotationData::memberName)) |
| 75 | + .toList(); |
| 76 | + } |
| 77 | + |
| 78 | + private static void invokeRegistrator(final ModFileScanData.AnnotationData annotationData) { |
| 79 | + final Method registratorMethod = resolveRegistratorMethod(annotationData); |
| 80 | + try { |
| 81 | + registratorMethod.invoke(null); |
| 82 | + } catch (final ReflectiveOperationException e) { |
| 83 | + throw new IllegalStateException("Failed to invoke @CreateBlockEdits.Registrator method " + describe(annotationData) + ".", e); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private static Method resolveRegistratorMethod(final ModFileScanData.AnnotationData annotationData) { |
| 88 | + final Class<?> owner; |
| 89 | + try { |
| 90 | + final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); |
| 91 | + owner = Class.forName(annotationData.clazz().getClassName(), false, contextClassLoader != null ? contextClassLoader : CreateBlockEdits.class.getClassLoader()); |
| 92 | + } catch (final ClassNotFoundException e) { |
| 93 | + throw new IllegalStateException("Failed to load @CreateBlockEdits.Registrator owner " + describe(annotationData) + ".", e); |
| 94 | + } |
| 95 | + |
| 96 | + final List<Method> registrators = Arrays.stream(owner.getDeclaredMethods()) |
| 97 | + .filter(method -> method.getName().equals(annotationData.memberName())) |
| 98 | + .filter(method -> method.isAnnotationPresent(Registrator.class)) |
| 99 | + .toList(); |
| 100 | + if (registrators.size() != 1) { |
| 101 | + throw new IllegalStateException("Expected exactly one annotated @CreateBlockEdits.Registrator method for " + describe(annotationData) + ", but found " + registrators.size() + "."); |
| 102 | + } |
| 103 | + |
| 104 | + final Method registratorMethod = registrators.get(0); |
| 105 | + if (!Modifier.isPublic(registratorMethod.getModifiers()) |
| 106 | + || !Modifier.isStatic(registratorMethod.getModifiers()) |
| 107 | + || registratorMethod.getParameterCount() != 0 |
| 108 | + || registratorMethod.getReturnType() != Void.TYPE |
| 109 | + || !registratorMethod.getName().equals("register")) { |
| 110 | + throw new IllegalStateException("Invalid @CreateBlockEdits.Registrator method " + registratorMethod.toGenericString() + "; expected public static void register() with no arguments."); |
| 111 | + } |
| 112 | + |
| 113 | + return registratorMethod; |
| 114 | + } |
| 115 | + |
| 116 | + private static String describe(final ModFileScanData.AnnotationData annotationData) { |
| 117 | + return annotationData.clazz().getClassName() + "#" + annotationData.memberName(); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Marks a public static void register() method with no arguments to be invoked while Create's AllBlocks are bootstrapping. |
| 122 | + */ |
| 123 | + @Retention(RetentionPolicy.RUNTIME) |
| 124 | + @Target(ElementType.METHOD) |
| 125 | + public @interface Registrator { |
| 126 | + } |
| 127 | + |
| 128 | + private enum RegistrationWindow { |
| 129 | + NOT_STARTED, |
| 130 | + OPEN, |
| 131 | + CLOSED |
| 132 | + } |
| 133 | + |
| 134 | +} |
0 commit comments