Class DistExecutor

java.lang.Object
net.minecraftforge.fml.DistExecutor

public final class DistExecutor extends Object
Use to execute code conditionally based on sidedness.
  • 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.
      use safeCallWhenOn(Dist, Supplier) instead. This remains for advanced use cases.
      Run the callable in the supplier only on the specified Side. 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. Use safeCallWhenOn(Dist, Supplier) where possible.
      Type Parameters:
      T - The return type from the callable
      Parameters:
      dist - The dist to run on
      toRun - 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, Supplier<Callable<T>> toRun)
    • safeCallWhenOn

      public static <T> T safeCallWhenOn(net.minecraftforge.api.distmarker.Dist dist, Supplier<DistExecutor.SafeCallable<T>> toRun)
      Call the SafeCallable when on the correct Dist. 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 on
      toRun - 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.
      use safeRunWhenOn(Dist, Supplier) where possible. Advanced uses only.
      Runs the supplied Runnable on the speicified side. Same warnings apply as callWhenOn(Dist, Supplier). This method can cause unexpected ClassNotFound exceptions.
      Parameters:
      dist - Dist to run this code on
      toRun - 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 as unsafeCallWhenOn(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. Use safeRunWhenOn(Dist, Supplier) if you can.
      Parameters:
      dist - Dist to run this code on
      toRun - 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 on
      toRun - The code to run
    • runForDist

      @Deprecated public static <T> T runForDist(Supplier<Supplier<T>> clientTarget, Supplier<Supplier<T>> serverTarget)
      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 the Dist.CLIENT
      serverTarget - The supplier supplier to run when on the Dist.DEDICATED_SERVER
      Returns:
      The returned instance
    • unsafeRunForDist

      public static <T> T unsafeRunForDist(Supplier<Supplier<T>> clientTarget, Supplier<Supplier<T>> serverTarget)
      Unsafe version of safeRunForDist(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 the Dist.CLIENT
      serverTarget - The supplier supplier to run when on the Dist.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 the Dist.CLIENT
      serverTarget - The supplier supplier to run when on the Dist.DEDICATED_SERVER
      Returns:
      The returned instance
    • validateSafeReferent

      private static final void validateSafeReferent(Supplier<? extends DistExecutor.SafeReferent> safeReferentSupplier)