Class DelayedCommand<T>

java.lang.Object
nl.pim16aap2.animatedarchitecture.core.commands.DelayedCommand<T>
Direct Known Subclasses:
AddOwnerDelayed, RemoveOwnerDelayed, SetBlocksToMoveDelayed, SetOpenDirectionDelayed, SetOpenStatusDelayed

public abstract class DelayedCommand<T> extends Object
Represents a delayed command.

A delayed command allows "starting" the command while only knowing the target structure. This is useful when the structure is already known (e.g. after having been selected in a GUI), but the other specific data is not.

An example usage of this system could be changing the name of a structure via a GUI. Using the GUI, the user has to select a structure to apply the change to, but we'll need to wait for user input to update the name. For a user, having to use specify the structure they would need to change in a command or something would be rather awkward, so this way we can remember that information and not require the user to input duplicate data.

  • Field Details

    • delayedCommandInputManager

      protected final DelayedCommandInputManager delayedCommandInputManager
      Manager responsible for keeping track of active delayed command input requests.
    • commandFactory

      protected final jakarta.inject.Provider<CommandFactory> commandFactory
      Provider for the command factory to create new commands.
    • inputRequestFactory

      protected final DelayedCommandInputRequest.IFactory<T> inputRequestFactory
      Factory for creating delayed command input requests.
  • Constructor Details

    • DelayedCommand

      protected DelayedCommand(DelayedCommand.Context context, DelayedCommandInputRequest.IFactory<T> inputRequestFactory, Class<T> delayedInputClz)
      Constructs a new DelayedCommand.
      Parameters:
      context - The context containing required dependencies.
      inputRequestFactory - Factory for creating delayed command input requests.
      delayedInputClz - The class representing the type of delayed input this command expects.
  • Method Details

    • runDelayed

      public CompletableFuture<?> runDelayed(ICommandSender commandSender, Creator creator, Function<T, CompletableFuture<?>> executor, @Nullable Supplier<String> initMessageSupplier)
      Starts the (new) DelayedInputRequest for this delayed command.

      The DelayedCommandInputRequest will be used to retrieve the values that are required to execute the command. The player will be asked to use the command (again, if needed) to supply the missing data.

      These missing data can be supplied using provideDelayedInput(ICommandSender, T). Once the data are supplied, the command will be executed.

      Parameters:
      commandSender - The entity that sent the command and is held responsible (i.e. permissions, communication) for its execution.
      creator - A Creator that will wait for the input.
      executor - Function to execute with the delayed input.
      initMessageSupplier - Supplier for the initial message to show to the user.
      Returns:
      A CompletableFuture representing the result of the command.
    • runDelayed

      public CompletableFuture<?> runDelayed(ICommandSender commandSender, StructureRetriever structureRetriever)
      Starts the (new) DelayedInputRequest for this delayed command.

      The DelayedCommandInputRequest will be used to retrieve the values that are required to execute the command. The player will be asked to use the command (again, if needed) to supply the missing data.

      These missing data can be supplied using provideDelayedInput(ICommandSender, T). Once the data are supplied, the command will be executed.

      Parameters:
      commandSender - The entity that sent the command and is held responsible (i.e. permissions, communication) for its execution.
      structureRetriever - A StructureRetrieverFactory that references the target structure.
      Returns:
      See BaseCommand.run().
    • provideDelayedInput

      public CompletableFuture<?> provideDelayedInput(ICommandSender commandSender, T data)
      Provides the delayed input if there is currently an active DelayedCommandInputRequest for the ICommandSender. After processing the input, the new command will be executed immediately.

      If no active DelayedCommandInputRequest can be found for the command sender, the command sender will be informed about it (e.g. "We are not waiting for a command!").

      Parameters:
      commandSender - The ICommandSender for which to look for an active DelayedCommandInputRequest that can be fulfilled.
      data - The data specified by the user.
      Returns:
      See ToolUser.handleInput(Object).
    • getCommandDefinition

      protected abstract CommandDefinition getCommandDefinition()
      Gets the command definition for this delayed command.
      Returns:
      The command definition.
    • delayedInputExecutor

      protected abstract CompletableFuture<?> delayedInputExecutor(ICommandSender commandSender, StructureRetriever structureRetriever, T delayedInput)
      The method that is run once delayed input is received.

      It processes the new input and executes the command using the previously-provided data (see runDelayed(ICommandSender, StructureRetriever)).

      Parameters:
      commandSender - The entity that sent the command and is held responsible (i.e. permissions, communication) for its execution.
      structureRetriever - A StructureRetrieverFactory that references the target structure.
      delayedInput - The delayed input that was retrieved.
      Returns:
      See BaseCommand.run().
    • inputRequestMessage

      protected abstract String inputRequestMessage(ICommandSender commandSender, StructureRetriever structureRetriever)
      Retrieves the message that will be sent to the command sender after initialization of a delayed input request.
      Parameters:
      commandSender - The user responsible for the delayed command.
      structureRetriever - The structure retriever as currently specified.
      Returns:
      The init message for the delayed input request.
    • toString

      public @NonNull String toString()
      Overrides:
      toString in class Object