Class ToolUser
- Direct Known Subclasses:
Creator, PowerBlockInspector, PowerBlockRelocator
Procedure that is populated with Steps.
The procedure can be used to guide the player through the process of creating a structure, or any other process that uses any kind of input.
The main way to interact with this class is the handleInput(Object) method. This method takes the provided
input and applies it to the current step in the procedure. The expected input type and behavior depend on the
currently-active step.
The handleInput(Object) method is thread-safe and will handle input in a synchronized manner. This means
that only one input can be processed at a time. If multiple inputs are provided, they will be processed in the order
they were received. The lock is not thread-specific, so subclasses should be careful around threading issues. If a
subclass needs to perform multiple operations that require the lock, it should use the runWithLock(Supplier)
method.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final IAnimatedArchitectureToolUtilprotected final PersonalizedLocalizerprotected final IProtectionHookManagerprotected final Step.Factory.IFactoryprotected final ToolUserManager -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedToolUser(ToolUser.Context context, IPlayer player) Creates a newToolUserfor the given player. -
Method Summary
Modifier and TypeMethodDescriptionvoidabort()Aborts the process thisToolUseris currently in.protected final voidEnsures that the input lock is held.protected final voidTakes care of the final part of the process.Generates the list ofSteps that together will make up theprocedure.Accessor forProcedure.getAllSteps().Gets the message for the current step.protected final voidAdds the AnimatedArchitecture tool from the player's inventory.protected final voidProceeds to the next step in the procedure.final CompletableFuture<Boolean> handleInput(@Nullable Object obj) Handles some kind of input for the given step.protected final CompletableFuture<Boolean> handleInputWithLock(@Nullable Object obj) Handles user input for the given step.protected voidinit()Basic initialization executed that should be called after the constructor.protected final voidinsertStep(String name) Inserts an existing, named step at the current position.final booleanisActive()Keeps track of whether thisToolUseris active or not.playerHasAccessToCuboid(Cuboid cuboid, IWorld world) Checks if a player is allowed to break all blocks in a given cuboid.Checks if a player is allowed to break the block in a given location.final booleanAccessor forplayerHasTool.protected CompletableFuture<Boolean> Prepares the next step.protected final voidRemoves the AnimatedArchitecture tool from the player's inventory.protected final <T> CompletableFuture<T> runWithLock(Supplier<CompletableFuture<T>> supplier) Runs the provided supplier while holding the input lock.protected voidSends the localized message of the current step to the player that owns this object.protected voidsendMessage(@Nullable Step step) Sends the localized message of the given step to the player that owns this object.protected final booleansetPlayerHasTool(boolean playerHasTool) Sets the playerHasTool field.protected CompletableFuture<Boolean> skipToStep(Step goalStep) Returns a minimal string representation of this object.toString()
-
Field Details
-
localizer
-
toolUserManager
-
protectionHookManager
-
animatedArchitectureToolUtil
-
stepFactory
-
-
Constructor Details
-
Method Details
-
init
protected void init()Basic initialization executed that should be called after the constructor.This method will initialize the
Procedureusing the steps generated bygenerateSteps()and register thisToolUserwith theToolUserManager.If this method is not called, the
ToolUserwill be considered uninitialized and any method that requires the procedure (e.g. handling input) will throw an exception.If this method is called more than once, an exception will be thrown.
- Throws:
IllegalStateException- If this method is called more than once.RuntimeException- If the procedure could not be instantiated.
-
generateSteps
Generates the list ofSteps that together will make up theprocedure.- Returns:
- The list of
Steps that together will make up theprocedure. - Throws:
InstantiationException- When a step's factory is incomplete or otherwise invalid.
-
cleanUpProcess
protected final void cleanUpProcess()Takes care of the final part of the process. This unregisters thisToolUserand removes the tool from the player's inventory (if they still have it). -
abort
public void abort()Aborts the process thisToolUseris currently in.By default, this method only calls
cleanUpProcess(). Subclasses can override this method to add additional behavior. -
giveTool
Adds the AnimatedArchitecture tool from the player's inventory.- Parameters:
nameKey- The localization key of the name of the tool.loreKey- The localization key of the lore of the tool.text- The message to send to the player after giving them the tool.
-
removeTool
protected final void removeTool()Removes the AnimatedArchitecture tool from the player's inventory. -
getCurrentStepMessage
Gets the message for the current step.- Returns:
- The message of the current step if possible. Otherwise, an empty String is returned.
-
prepareCurrentStep
Prepares the next step. For example by sending the player some instructions about what they should do.This should be used after proceeding to the next step.
- Returns:
- A
CompletableFuturethat will be completed when the current step is prepared.If the step does not wait for user input, the returned future will be the result of
handleInputWithLock(Object). Otherwise, it will be completed with true. - Throws:
IllegalStateException- If the lock is not held. SeeacquireInputLock().
-
skipToStep
SeeProcedure.skipToStep(Step).After successfully skipping to the target step, the newly-selected step will be prepared. See
prepareCurrentStep().- Parameters:
goalStep- The step to skip to.- Returns:
- A
CompletableFuturethat will be completed an exception if the step could not be found, or the result ofprepareCurrentStep()otherwise. - Throws:
IllegalStateException- If the lock is not held. SeeacquireInputLock().
-
handleInputWithLock
@CheckReturnValue protected final CompletableFuture<Boolean> handleInputWithLock(@Nullable Object obj) Handles user input for the given step.This method assumes that the input lock is already held. If the caller does not hold the lock, they should use either
handleInput(Object)orrunWithLock(Supplier).This method is intended for use by subclasses or step executors that need to provide input as part of their operation and therefore already hold the lock.
- Parameters:
obj- The input to handle. What actual type is expected depends on the step.- Returns:
- A completable future that will either complete with true if the step was completed successfully, or false if it was not.
- Throws:
IllegalStateException- If the lock is not held.
-
handleInput
Handles some kind of input for the given step.- Parameters:
obj- The input to handle. What actual type is expected depends on the step.- Returns:
- True if the input was processed successfully.
-
runWithLock
Runs the provided supplier while holding the input lock.This method will acquire the input lock, run the supplier, and then release the input lock.
If the supplier throws an exception, the lock will still be released.
- Type Parameters:
T- The type of the result of the supplier.- Parameters:
supplier- The supplier to run.- Returns:
- The result of the supplier.
-
sendMessage
Sends the localized message of the given step to the player that owns this object.- Parameters:
step- The step to send the message for.
-
sendMessage
protected void sendMessage()Sends the localized message of the current step to the player that owns this object. -
getCurrentStep
-
playerHasAccessToLocation
Checks if a player is allowed to break the block in a given location.If the player is not allowed to break blocks in the location, a message will be sent to them (provided the name of the compat isn't empty).
- Parameters:
loc- The location to check.- Returns:
- True if the player is allowed to break the block at the given location.
-
playerHasAccessToCuboid
Checks if a player is allowed to break all blocks in a given cuboid.If the player is not allowed to break one or more blocks in the cuboid, a message will be sent to them. (provided the name of the compat isn't empty).
- Parameters:
cuboid- The cuboid to check.world- The world to check in.- Returns:
- The cuboid if the player is allowed to break all blocks in it, or an empty Optional otherwise.
-
isActive
public final boolean isActive()Keeps track of whether thisToolUseris active or not.- Returns:
- true if this tool user is active.
-
setPlayerHasTool
protected final boolean setPlayerHasTool(boolean playerHasTool) Sets the playerHasTool field.- Parameters:
playerHasTool- The new value.- Returns:
- The old value.
-
playerHasTool
public final boolean playerHasTool()Accessor forplayerHasTool.- Returns:
- The value of
playerHasTool.
-
getAllSteps
Accessor forProcedure.getAllSteps().- Returns:
- All the steps in the procedure including any that may have been completed/skipped already.
-
goToNextStep
protected final void goToNextStep()Proceeds to the next step in the procedure.This method assumes that the lock is held. See
acquireInputLock().This method is a wrapper around
Procedure.goToNextStep().- Throws:
IllegalStateException- If the lock is not held. SeeacquireInputLock().
-
insertStep
Inserts an existing, named step at the current position.This method assumes that the lock is held. See
acquireInputLock().This method is a wrapper around
Procedure.insertStep(String).- Parameters:
name- The name of the step to insert.- Throws:
NoSuchElementException- If no step can be found by that name.IllegalStateException- If the lock is not held. SeeacquireInputLock().
-
assertLockHeld
protected final void assertLockHeld()Ensures that the input lock is held.- Throws:
IllegalStateException- If the lock is not held.
-
toMinimalString
Returns a minimal string representation of this object.This method is intended for logging purposes, as the full string representation may be too verbose.
- Returns:
- A minimal string representation of this object.
-
toString
-
getPlayer
-