Operations API
HuskClaims exposes API to let you register OperationType
s to call custom operations and check if they should be allowed with the Handler
.
Table of Contents
- 1. Getting the OperationTypeRegistry
- 2. Operations and OperationTypes
- 3. Registering a custom OperationType
- 4. Using Handler to cancel Operations with your custom OperationType
1. Getting the OperationTypeRegistry
- The
OperationTypeRegistry
is the registry containing registeredOperationType
s. - It also lets you access the
Handler
class. - Get the
OperationTypeRegistry
withHuskClaimsAPI#getOperationTypeRegistry
Example — Getting the OperationTypeRegistry
void getRegistry() {
final OperationTypeRegistry reg = huskClaims.getOperationTypeRegistry();
}
2. Operations and OperationTypes
Operation
s represent actions in a world that can be prevented from occuring entity based on:- Which game entity, if any, performed the action
- If the game entity is a player, their trust level (see Trust API)
- The location the action affects or occurred
- The game entity, if any, affected by the action
- The type of action that occurred is represented by an
OperationType
. - Your plugin can create
Operation
s with custom registeredOperationType
s and callHandler#isOperationAllowed
to allow to determine whether your custom plugin/mod's actions should be permitted - Users can then add these
OperationType
s to their trust levels config.
3. Registering a custom OperationType
- Start by registering a custom
OperationType
. We recommend doing thisonEnable
(notonLoad
before HuskClaims has loaded). - Use
OperationTypeRegistry#registerOperationType(@NotNull Key key, boolean silent)
to create anOperationType
key
- the key identifier of the OperationType (e.g.Key.of("my_plugin", "operation_name")
->my_plugin:operation_name
).silent
- whether players should be informed when this operation is cancelled (usually you want this on, unless your operation doesn't affect players or is particularly spammy such as pressure plate triggering operations)
- This method will create, then register an
OperationType
. You should save this Operation Type somewhere for later use.
Example — Registering a custom OperationType
private OperationType releaseMonOpType;
void getRegistry() {
final OperationTypeRegistry reg = huskClaims.getOperationTypeRegistry();
releaseMonOpType = reg.createOperationType(Key.of("mons_mod", "release_mon"));
reg.registerOperationType(releaseMonOpType);
}
4. Using Handler to cancel Operations with your custom OperationType
- Get the
Handler
withOperationTypeRegistry#getHandler
- Create an
Operation
with your newly madeOperationType
usingOperation#of()
- There are different static
Operation#of
variants letting you specify the performing user, operation type, operation position, silent state of this operation (overriding the default value of the operation type), and victim user in various combinations.
- There are different static
- Call
Handler#cancelOperation
to get a booleantrue
orfalse
value of whether the Operation should be allowed - Decide whether to perform logic based on that return value
Example — Checking if Operations are allowed
private OperationType releaseMonOpType;
void onMonReleased(Player bukkitPlayer, Location releasedAt) {
final OperationTypeRegistry reg = huskClaims.getOperationTypeRegistry();
final boolean cancelled = reg.getHandler().cancelOperation(Operation.of(
huskClaims.getPlayer(bukkitPlayer), // OnlineUser implements OperationUser
releaseMonOpType,
huskClaims.getPosition(releasedAt) // Position implements OperationPosition
));
if (cancelled) {
// Don't continue with the action
return;
}
// Logic would continue if the operation wasn't cancelled...
}