The container that wraps around mods in the system. The philosophy is that individual mod implementation technologies should not impact the actual loading and management of mod code. This class provides a mechanism by which we can wrap actual mod code so that the loader and other facilities can
| 51 | */ |
| 52 | |
| 53 | public abstract class ModContainer |
| 54 | { |
| 55 | protected final String modId; |
| 56 | protected final String namespace; |
| 57 | protected final IModInfo modInfo; |
| 58 | protected ModLoadingStage modLoadingStage; |
| 59 | protected Supplier<?> contextExtension; |
| 60 | protected final Map<ModLoadingStage, Runnable> activityMap = new HashMap<>(); |
| 61 | protected final Map<ExtensionPoint, Supplier<?>> extensionPoints = new IdentityHashMap<>(); |
| 62 | protected final EnumMap<ModConfig.Type, ModConfig> configs = new EnumMap<>(ModConfig.Type.class); |
| 63 | @SuppressWarnings("OptionalUsedAsFieldOrParameterType") |
| 64 | protected Optional<Consumer<ModConfig.ModConfigEvent>> configHandler = Optional.empty(); |
| 65 | |
| 66 | public ModContainer(IModInfo info) |
| 67 | { |
| 68 | this.modId = info.getModId(); |
| 69 | // TODO: Currently not reading namespace from configuration.. |
| 70 | this.namespace = this.modId; |
| 71 | this.modInfo = info; |
| 72 | this.modLoadingStage = ModLoadingStage.CONSTRUCT; |
| 73 | // default displaytest extension checks for version string match |
| 74 | registerExtensionPoint(ExtensionPoint.DISPLAYTEST, () -> Pair.of(()->this.modInfo.getVersion().toString(), |
| 75 | (incoming, isNetwork)->Objects.equals(incoming, this.modInfo.getVersion().toString()))); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @return the modid for this mod |
| 80 | */ |
| 81 | public final String getModId() |
| 82 | { |
| 83 | return modId; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @return the resource prefix for the mod |
| 88 | */ |
| 89 | public final String getNamespace() |
| 90 | { |
| 91 | return namespace; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @return The current loading stage for this mod |
| 96 | */ |
| 97 | public ModLoadingStage getCurrentState() |
| 98 | { |
| 99 | return modLoadingStage; |
| 100 | } |
| 101 | |
| 102 | public static <T extends Event & IModBusEvent> CompletableFuture<Void> buildTransitionHandler( |
| 103 | final ModContainer target, |
| 104 | final ModLoadingStage.EventGenerator<T> eventGenerator, |
| 105 | final BiFunction<ModLoadingStage, Throwable, ModLoadingStage> stateChangeHandler, |
| 106 | final Executor executor) { |
| 107 | return CompletableFuture |
| 108 | .runAsync(() -> { |
| 109 | ModLoadingContext.get().setActiveContainer(target, target.contextExtension.get()); |
| 110 | target.activityMap.getOrDefault(target.modLoadingStage, ()->{}).run(); |