Skip to content

Commit 3248a01

Browse files
authored
Support namespaced hooks and require explicit hook initialization (#152)
1 parent 469e2ab commit 3248a01

14 files changed

+84
-137
lines changed

docs/MI_HOOKS.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ who may want to make them.
88
First you need to register your mod with the hook system. To do so, you must provide a hook registry and a hook
99
listener.
1010

11-
To register your mod's hook registry and listener, simply include the `@MIHookEntrypoint` annotation on your classes. Be
12-
sure to include it on both the registry and the listener.
11+
To register your mod's hook registry and listener, include the `@MIHookEntrypoint` annotation on your classes. Be sure
12+
to include it on both the registry and the listener. Also, be sure to call `TesseractMI.init("your_mod_id")` in your
13+
mod constructor! Otherwise, your machines will not be registered.
1314

1415
### Hook Registries
1516

src/main/java/net/swedz/tesseract/neoforge/Tesseract.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import net.neoforged.neoforge.event.entity.living.LivingEquipmentChangeEvent;
1212
import net.neoforged.neoforge.event.tick.EntityTickEvent;
1313
import net.swedz.tesseract.neoforge.compat.ModLoadedHelper;
14-
import net.swedz.tesseract.neoforge.compat.mi.TesseractMI;
14+
import net.swedz.tesseract.neoforge.compat.mi.TesseractMILootConditions;
1515
import net.swedz.tesseract.neoforge.datagen.client.LanguageDatagenProvider;
1616
import net.swedz.tesseract.neoforge.event.ItemHurtEvent;
1717
import net.swedz.tesseract.neoforge.item.ArmorTickHandler;
@@ -40,7 +40,7 @@ public Tesseract(IEventBus bus)
4040

4141
if(ModLoadedHelper.isLoaded("modern_industrialization"))
4242
{
43-
TesseractMI.init(bus);
43+
TesseractMILootConditions.init(bus);
4444
}
4545

4646
bus.addListener(GatherDataEvent.class, (event) ->
Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
11
package net.swedz.tesseract.neoforge.compat.mi;
22

3-
import net.neoforged.bus.api.IEventBus;
3+
import net.swedz.tesseract.neoforge.compat.mi.hook.MIHooks;
4+
import net.swedz.tesseract.neoforge.compat.mi.hook.context.listener.HatchMIHookContext;
5+
import net.swedz.tesseract.neoforge.compat.mi.hook.context.listener.MachineCasingsMIHookContext;
6+
import net.swedz.tesseract.neoforge.compat.mi.hook.context.listener.MachineProcessConditionsMIHookContext;
7+
import net.swedz.tesseract.neoforge.compat.mi.hook.context.listener.MachineRecipeTypesMIHookContext;
8+
import net.swedz.tesseract.neoforge.compat.mi.hook.context.listener.MultiblockMachinesMIHookContext;
9+
import net.swedz.tesseract.neoforge.compat.mi.hook.context.listener.SingleBlockCraftingMachinesMIHookContext;
10+
import net.swedz.tesseract.neoforge.compat.mi.hook.context.listener.SingleBlockSpecialMachinesMIHookContext;
411

512
public final class TesseractMI
613
{
7-
public static void init(IEventBus bus)
14+
public static void init(String modId)
815
{
9-
TesseractMILootConditions.init(bus);
16+
MIHooks.triggerHookListeners(modId, (hook, listener) ->
17+
{
18+
listener.machineProcessConditions(new MachineProcessConditionsMIHookContext(hook));
19+
20+
listener.machineRecipeTypes(new MachineRecipeTypesMIHookContext(hook));
21+
22+
listener.machineCasings(new MachineCasingsMIHookContext(hook));
23+
24+
listener.hatches(new HatchMIHookContext(hook));
25+
listener.singleBlockSpecialMachines(new SingleBlockSpecialMachinesMIHookContext(hook));
26+
listener.singleBlockCraftingMachines(new SingleBlockCraftingMachinesMIHookContext(hook));
27+
listener.multiblockMachines(new MultiblockMachinesMIHookContext(hook));
28+
});
1029
}
1130
}

src/main/java/net/swedz/tesseract/neoforge/compat/mi/hook/MIHookEfficiency.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import net.swedz.tesseract.neoforge.compat.mi.hook.context.machine.EfficiencyMIHookContext;
44

5-
public interface MIHookEfficiency
5+
public interface MIHookEfficiency extends MIHookInstance
66
{
77
/**
88
* Gets the priority for the efficiency hook. Higher priorities are run first, and lower priorities are run last.

src/main/java/net/swedz/tesseract/neoforge/compat/mi/hook/MIHookEntrypointLoader.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,28 @@ public static void ensureLoaded()
3131
}
3232
}
3333

34-
private static <H> boolean registerEntrypoint(
35-
ModFileScanData data, Class<?> entrypointClass, Class<H> hookClass, BiConsumer<String, H> register
34+
private static <H extends MIHookInstance> boolean registerEntrypoint(
35+
ModFileScanData data,
36+
ModFileScanData.AnnotationData annotation, Class<?> entrypointClass,
37+
Class<H> hookClass,
38+
BiConsumer<String, H> register
3639
) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException
3740
{
3841
if(hookClass.isAssignableFrom(entrypointClass))
3942
{
4043
Class<? extends H> hookClassReference = entrypointClass.asSubclass(hookClass);
4144
H hook = hookClassReference.getConstructor().newInstance();
42-
String id = data.getIModInfoData().getFirst().getMods().getFirst().getModId();
43-
LOGGER.info("Registered entrypoint for mod {}: {}", id, hookClassReference.getName());
44-
register.accept(id, hook);
45+
if(!hook.shouldInitialize())
46+
{
47+
return true;
48+
}
49+
var modId = hook.modId();
50+
if(modId == null)
51+
{
52+
modId = data.getIModInfoData().getFirst().getMods().getFirst().getModId();
53+
}
54+
LOGGER.info("Registered entrypoint for mod {}: {}", modId, hookClassReference.getName());
55+
register.accept(modId, hook);
4556
return true;
4657
}
4758
return false;
@@ -67,15 +78,15 @@ private static void load()
6778

6879
boolean registered = false;
6980

70-
if(registerEntrypoint(data, entrypointClass, MIHookListener.class, MIHooks::registerListener))
81+
if(registerEntrypoint(data, annotation, entrypointClass, MIHookListener.class, MIHooks::registerListener))
7182
{
7283
registered = true;
7384
}
74-
else if(registerEntrypoint(data, entrypointClass, MIHookRegistry.class, MIHooks::registerRegistry))
85+
else if(registerEntrypoint(data, annotation, entrypointClass, MIHookRegistry.class, MIHooks::registerRegistry))
7586
{
7687
registered = true;
7788
}
78-
else if(registerEntrypoint(data, entrypointClass, MIHookEfficiency.class, MIHooks::registerEfficiencyListener))
89+
else if(registerEntrypoint(data, annotation, entrypointClass, MIHookEfficiency.class, MIHooks::registerEfficiencyListener))
7990
{
8091
registered = true;
8192
}
@@ -87,7 +98,8 @@ else if(registerEntrypoint(data, entrypointClass, MIHookEfficiency.class, MIHook
8798
}
8899
catch (Throwable ex)
89100
{
90-
LOGGER.error("Exception constructing entrypoint:", ex);
101+
LOGGER.error("Exception constructing entrypoint");
102+
throw new RuntimeException(ex);
91103
}
92104
});
93105

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package net.swedz.tesseract.neoforge.compat.mi.hook;
2+
3+
public interface MIHookInstance
4+
{
5+
/**
6+
* Gets the mod ID that should be referenced in the hook. This should only be overridden if you really know what
7+
* you are doing. Things can get messy.
8+
*
9+
* @return the mod ID, or null to automatically inherit the mod's actual ID
10+
*/
11+
default String modId()
12+
{
13+
return null;
14+
}
15+
16+
/**
17+
* Checks whether this hook should initialize or not.
18+
*
19+
* @return true if the hook should initialize, false if it should be ignored
20+
*/
21+
default boolean shouldInitialize()
22+
{
23+
return true;
24+
}
25+
}

src/main/java/net/swedz/tesseract/neoforge/compat/mi/hook/MIHookListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import net.swedz.tesseract.neoforge.compat.mi.hook.context.listener.SingleBlockSpecialMachinesMIHookContext;
1212
import net.swedz.tesseract.neoforge.compat.mi.hook.context.listener.ViewerSetupMIHookContext;
1313

14-
public interface MIHookListener
14+
public interface MIHookListener extends MIHookInstance
1515
{
1616
default void beforeInit()
1717
{

src/main/java/net/swedz/tesseract/neoforge/compat/mi/hook/MIHookRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import net.swedz.tesseract.neoforge.registry.holder.BlockHolder;
1010
import net.swedz.tesseract.neoforge.registry.holder.ItemHolder;
1111

12-
public interface MIHookRegistry
12+
public interface MIHookRegistry extends MIHookInstance
1313
{
1414
DeferredRegister.Blocks blockRegistry();
1515

src/main/java/net/swedz/tesseract/neoforge/compat/mi/hook/MIHooks.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.TreeSet;
1212
import java.util.function.BiConsumer;
1313

14+
@ApiStatus.Internal
1415
public final class MIHooks
1516
{
1617
private static final Map<String, MIHook> HOOKS = Maps.newHashMap();
@@ -62,20 +63,17 @@ static void registerEfficiencyListener(String modId, MIHookEfficiency efficiency
6263
EFFICIENCY_LISTENERS.add(efficiencyListener);
6364
}
6465

65-
@ApiStatus.Internal
6666
public static Set<String> getModIds()
6767
{
6868
return HOOKS.keySet();
6969
}
7070

71-
@ApiStatus.Internal
7271
private static MIHook getHook(String modId)
7372
{
7473
MIHookEntrypointLoader.ensureLoaded();
7574
return HOOKS.computeIfAbsent(modId, MIHook::new);
7675
}
7776

78-
@ApiStatus.Internal
7977
public static MIHookRegistry getRegistry(String modId)
8078
{
8179
MIHookEntrypointLoader.ensureLoaded();
@@ -86,7 +84,13 @@ public static MIHookRegistry getRegistry(String modId)
8684
return getHook(modId).registry();
8785
}
8886

89-
@ApiStatus.Internal
87+
public static void triggerHookListeners(String modId, BiConsumer<MIHook, MIHookListener> action)
88+
{
89+
MIHookEntrypointLoader.ensureLoaded();
90+
var hook = getHook(modId);
91+
action.accept(hook, hook.listener());
92+
}
93+
9094
public static void triggerHookListeners(BiConsumer<MIHook, MIHookListener> action)
9195
{
9296
MIHookEntrypointLoader.ensureLoaded();
@@ -96,7 +100,6 @@ public static void triggerHookListeners(BiConsumer<MIHook, MIHookListener> actio
96100
}
97101
}
98102

99-
@ApiStatus.Internal
100103
public static void triggerHookEfficiencyListeners(EfficiencyMIHookContext context,
101104
BiConsumer<MIHookEfficiency, EfficiencyMIHookContext> action)
102105
{

src/main/java/net/swedz/tesseract/neoforge/compat/mi/mixin/hook/listener/MachineCasingsHookMixin.java

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)