Package net.minecraftforge.fml
Class DistExecutor
java.lang.Object
net.minecraftforge.fml.DistExecutor
Use to execute code conditionally based on sidedness.
- When you want to call something on one side and return a result
safeCallWhenOn(Dist, Supplier)
- When you want to call one thing on one side, another thing on the other and return a result
safeRunForDist(Supplier, Supplier)
- When you want to run something on one side
safeRunWhenOn(Dist, Supplier)
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
SafeCallable version ofDistExecutor.SafeReferent
.static interface
A safe referent.static interface
SafeRunnable version ofDistExecutor.SafeReferent
static interface
SafeSupplier version ofDistExecutor.SafeReferent
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
callWhenOn
(net.minecraftforge.api.distmarker.Dist dist, Supplier<Callable<T>> toRun) Deprecated.static <T> T
runForDist
(Supplier<Supplier<T>> clientTarget, Supplier<Supplier<T>> serverTarget) Deprecated.static void
Deprecated.usesafeRunWhenOn(Dist, Supplier)
where possible.static <T> T
safeCallWhenOn
(net.minecraftforge.api.distmarker.Dist dist, Supplier<DistExecutor.SafeCallable<T>> toRun) Call the SafeCallable when on the correctDist
.static <T> T
safeRunForDist
(Supplier<DistExecutor.SafeSupplier<T>> clientTarget, Supplier<DistExecutor.SafeSupplier<T>> serverTarget) Executes one of the two suppliers, based on which side is active.static void
safeRunWhenOn
(net.minecraftforge.api.distmarker.Dist dist, Supplier<DistExecutor.SafeRunnable> toRun) Call the supplied SafeRunnable when on the correct Dist.static <T> T
unsafeCallWhenOn
(net.minecraftforge.api.distmarker.Dist dist, Supplier<Callable<T>> toRun) static <T> T
unsafeRunForDist
(Supplier<Supplier<T>> clientTarget, Supplier<Supplier<T>> serverTarget) Unsafe version ofsafeRunForDist(Supplier, Supplier)
.static void
unsafeRunWhenOn
(net.minecraftforge.api.distmarker.Dist dist, Supplier<Runnable> toRun) Runs the supplied Runnable on the speicified side.private static final void
validateSafeReferent
(Supplier<? extends DistExecutor.SafeReferent> safeReferentSupplier)
-
Field Details
-
LOGGER
private static final org.apache.logging.log4j.Logger LOGGER
-
-
Constructor Details
-
DistExecutor
private DistExecutor()
-
-
Method Details
-
callWhenOn
@Deprecated public static <T> T callWhenOn(net.minecraftforge.api.distmarker.Dist dist, Supplier<Callable<T>> toRun) Deprecated.usesafeCallWhenOn(Dist, Supplier)
instead. This remains for advanced use cases.Run the callable in the supplier only on the specifiedDist
. This method is NOT sided-safe and special care needs to be taken in code using this method that implicit class loading is not triggered by the Callable. This method can cause unexpected ClassNotFound exceptions. UsesafeCallWhenOn(Dist, Supplier)
where possible.- Type Parameters:
T
- The return type from the callable- Parameters:
dist
- The dist to run ontoRun
- A supplier of the callable to run (Supplier wrapper to ensure classloading only on the appropriate dist)- Returns:
- The callable's result
-
unsafeCallWhenOn
-
safeCallWhenOn
public static <T> T safeCallWhenOn(net.minecraftforge.api.distmarker.Dist dist, Supplier<DistExecutor.SafeCallable<T>> toRun) Call the SafeCallable when on the correctDist
. The lambda supplied here is required to be a method reference to a method defined in another class, otherwise an invalid SafeReferent error will be thrown- Type Parameters:
T
- The type of the SafeCallable- Parameters:
dist
- the dist which this will run ontoRun
- the SafeCallable to run and return the result from- Returns:
- the result of the SafeCallable or null if on the wrong side
-
runWhenOn
@Deprecated public static void runWhenOn(net.minecraftforge.api.distmarker.Dist dist, Supplier<Runnable> toRun) Deprecated.usesafeRunWhenOn(Dist, Supplier)
where possible. Advanced uses only.Runs the supplied Runnable on the speicified side. Same warnings apply ascallWhenOn(Dist, Supplier)
. This method can cause unexpected ClassNotFound exceptions.- Parameters:
dist
- Dist to run this code ontoRun
- The code to run- See Also:
-
unsafeRunWhenOn
public static void unsafeRunWhenOn(net.minecraftforge.api.distmarker.Dist dist, Supplier<Runnable> toRun) Runs the supplied Runnable on the speicified side. Same warnings apply asunsafeCallWhenOn(Dist, Supplier)
. This method can cause unexpected ClassNotFoundException problems in common scenarios. Understand the pitfalls of the way the class verifier works to load classes before using this. UsesafeRunWhenOn(Dist, Supplier)
if you can.- Parameters:
dist
- Dist to run this code ontoRun
- The code to run- See Also:
-
safeRunWhenOn
public static void safeRunWhenOn(net.minecraftforge.api.distmarker.Dist dist, Supplier<DistExecutor.SafeRunnable> toRun) Call the supplied SafeRunnable when on the correct Dist.- Parameters:
dist
- The dist to run ontoRun
- The code to run
-
runForDist
@Deprecated public static <T> T runForDist(Supplier<Supplier<T>> clientTarget, Supplier<Supplier<T>> serverTarget) Deprecated.Executes one of the two suppliers, based on which side is active.Example (replacement for old SidedProxy):
Proxy p = DistExecutor.runForDist(()->ClientProxy::new, ()->ServerProxy::new);
NOTE: the double supplier is required to avoid classloading the secondary target.- Type Parameters:
T
- The common type to return- Parameters:
clientTarget
- The supplier supplier to run when on theDist.CLIENT
serverTarget
- The supplier supplier to run when on theDist.DEDICATED_SERVER
- Returns:
- The returned instance
-
unsafeRunForDist
public static <T> T unsafeRunForDist(Supplier<Supplier<T>> clientTarget, Supplier<Supplier<T>> serverTarget) Unsafe version ofsafeRunForDist(Supplier, Supplier)
. Use only when you know what you're doing and understand why the verifier can cause unexpected ClassNotFoundException crashes even when code is apparently not sided. Ensure you test both sides fully to be confident in using this.- Type Parameters:
T
- The common type to return- Parameters:
clientTarget
- The supplier supplier to run when on theDist.CLIENT
serverTarget
- The supplier supplier to run when on theDist.DEDICATED_SERVER
- Returns:
- The returned instance
-
safeRunForDist
public static <T> T safeRunForDist(Supplier<DistExecutor.SafeSupplier<T>> clientTarget, Supplier<DistExecutor.SafeSupplier<T>> serverTarget) Executes one of the two suppliers, based on which side is active.Example (replacement for old SidedProxy):
Proxy p = DistExecutor.safeRunForDist(()->ClientProxy::new, ()->ServerProxy::new);
NOTE: the double supplier is required to avoid classloading the secondary target.- Type Parameters:
T
- The common type to return- Parameters:
clientTarget
- The supplier supplier to run when on theDist.CLIENT
serverTarget
- The supplier supplier to run when on theDist.DEDICATED_SERVER
- Returns:
- The returned instance
-
validateSafeReferent
private static final void validateSafeReferent(Supplier<? extends DistExecutor.SafeReferent> safeReferentSupplier)
-
safeCallWhenOn(Dist, Supplier)
instead.