Package net.minecraftforge.fml
Class DistExecutor
- java.lang.Object
-
- net.minecraftforge.fml.DistExecutor
-
public final class DistExecutor extends java.lang.Object
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)
- When you want to call something on one side and return a result
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
DistExecutor.SafeCallable<T>
SafeCallable version ofDistExecutor.SafeReferent
.static interface
DistExecutor.SafeReferent
A safe referent.static interface
DistExecutor.SafeRunnable
SafeRunnable version ofDistExecutor.SafeReferent
static interface
DistExecutor.SafeSupplier<T>
SafeSupplier version ofDistExecutor.SafeReferent
-
Field Summary
Fields Modifier and Type Field Description private static org.apache.logging.log4j.Logger
LOGGER
-
Constructor Summary
Constructors Modifier Constructor Description private
DistExecutor()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static <T> T
callWhenOn(net.minecraftforge.api.distmarker.Dist dist, java.util.function.Supplier<java.util.concurrent.Callable<T>> toRun)
Deprecated.usesafeCallWhenOn(Dist, Supplier)
instead.static <T> T
runForDist(java.util.function.Supplier<java.util.function.Supplier<T>> clientTarget, java.util.function.Supplier<java.util.function.Supplier<T>> serverTarget)
Deprecated.static void
runWhenOn(net.minecraftforge.api.distmarker.Dist dist, java.util.function.Supplier<java.lang.Runnable> toRun)
Deprecated.usesafeRunWhenOn(Dist, Supplier)
where possible.static <T> T
safeCallWhenOn(net.minecraftforge.api.distmarker.Dist dist, java.util.function.Supplier<DistExecutor.SafeCallable<T>> toRun)
Call the SafeCallable when on the correctDist
.static <T> T
safeRunForDist(java.util.function.Supplier<DistExecutor.SafeSupplier<T>> clientTarget, java.util.function.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, java.util.function.Supplier<DistExecutor.SafeRunnable> toRun)
Call the supplied SafeRunnable when on the correct Dist.static <T> T
unsafeCallWhenOn(net.minecraftforge.api.distmarker.Dist dist, java.util.function.Supplier<java.util.concurrent.Callable<T>> toRun)
static <T> T
unsafeRunForDist(java.util.function.Supplier<java.util.function.Supplier<T>> clientTarget, java.util.function.Supplier<java.util.function.Supplier<T>> serverTarget)
Unsafe version ofsafeRunForDist(Supplier, Supplier)
.static void
unsafeRunWhenOn(net.minecraftforge.api.distmarker.Dist dist, java.util.function.Supplier<java.lang.Runnable> toRun)
Runs the supplied Runnable on the speicified side.private static void
validateSafeReferent(java.util.function.Supplier<? extends DistExecutor.SafeReferent> safeReferentSupplier)
-
-
-
Method Detail
-
callWhenOn
@Deprecated public static <T> T callWhenOn(net.minecraftforge.api.distmarker.Dist dist, java.util.function.Supplier<java.util.concurrent.Callable<T>> toRun)
Deprecated.usesafeCallWhenOn(Dist, Supplier)
instead. This remains for advanced use cases.Run the callable in the supplier only on the specifiedSide
. 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
public static <T> T unsafeCallWhenOn(net.minecraftforge.api.distmarker.Dist dist, java.util.function.Supplier<java.util.concurrent.Callable<T>> toRun)
-
safeCallWhenOn
public static <T> T safeCallWhenOn(net.minecraftforge.api.distmarker.Dist dist, java.util.function.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, java.util.function.Supplier<java.lang.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:
callWhenOn(Dist, Supplier)
-
unsafeRunWhenOn
public static void unsafeRunWhenOn(net.minecraftforge.api.distmarker.Dist dist, java.util.function.Supplier<java.lang.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:
unsafeCallWhenOn(Dist, Supplier)
-
safeRunWhenOn
public static void safeRunWhenOn(net.minecraftforge.api.distmarker.Dist dist, java.util.function.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(java.util.function.Supplier<java.util.function.Supplier<T>> clientTarget, java.util.function.Supplier<java.util.function.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(java.util.function.Supplier<java.util.function.Supplier<T>> clientTarget, java.util.function.Supplier<java.util.function.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(java.util.function.Supplier<DistExecutor.SafeSupplier<T>> clientTarget, java.util.function.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(java.util.function.Supplier<? extends DistExecutor.SafeReferent> safeReferentSupplier)
-
-