Operations API
HuskTowns exposes API to let you register OperationTypes 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
OperationTypeRegistryis the registry containing registeredOperationTypes. - It also lets you access the
Handlerclass. - Get the
OperationTypeRegistrywithHuskTownsAPI#getOperationTypeRegistry
Example — Getting the OperationTypeRegistry
void getRegistry() {
final OperationTypeRegistry reg = HuskTowns.getOperationTypeRegistry();
}
2. Operations and OperationTypes
Operations 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
Operations with custom registeredOperationTypes and callHandler#isOperationAllowedto allow to determine whether your custom plugin/mod's actions should be permitted - Users can then add these
OperationTypes to their trust levels config.
3. Registering a custom OperationType
- Start by registering a custom
OperationType. We recommend doing thisonEnable(notonLoadbefore HuskTowns has loaded). - Use
OperationTypeRegistry#registerOperationType(@NotNull Key key, boolean silent)to create anOperationTypekey- 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 = HuskTowns.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
HandlerwithOperationTypeRegistry#getHandler - Create an
Operationwith your newly madeOperationTypeusingOperation#of()- There are different static
Operation#ofvariants 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#cancelOperationto get a booleantrueorfalsevalue 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 = HuskTowns.getOperationTypeRegistry();
final boolean cancelled = reg.getHandler().cancelOperation(Operation.of(
HuskTowns.getPlayer(bukkitPlayer), // OnlineUser implements OperationUser
releaseMonOpType,
HuskTowns.getPosition(releasedAt) // Position implements OperationPosition
));
if (cancelled) {
// Don't continue with the action
return;
}
// Logic would continue if the operation wasn't cancelled...
}