Skip to content

Commit

Permalink
Added custom command & validator example
Browse files Browse the repository at this point in the history
  • Loading branch information
Zffu committed Jul 16, 2024
1 parent 82bb67a commit 961d577
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package net.zffu.hardened.examples.api;

import net.zffu.hardened.api.commands.Command;
import net.zffu.hardened.api.commands.types.TypeGatedCommand;
import net.zffu.hardened.api.commands.validator.CommandValidator;
import net.zffu.hardened.api.context.CommandContext;
import net.zffu.hardened.api.invoker.InvokerType;

/**
* An example on how to create your own {@link net.zffu.hardened.api.commands.Command} implementation with the custom features you want.
*/

// In this example we create a invoker gated command.
public class CustomCommandExample implements Command, TypeGatedCommand {

private String[] names = new String[]{"test"};
private InvokerType[] allowedInvokers = new InvokerType[] {InvokerType.PLAYER}; // Only allows players to use our command, this will not matter at all if the custom validator doesn't include a check for it

@Override
public String[] getNames() {
return this.names;
}

@Override
public CommandValidator getValidator() {
return CustomCommandValidator.instance;
}

@Override
public void execute(CommandContext commandContext) {
System.out.println("Hello " + commandContext.getInvoker().getType().name());
}

@Override
public InvokerType[] getAllowedInvokers() {
return this.allowedInvokers;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package net.zffu.hardened.examples.api;

import net.zffu.hardened.api.commands.validator.CommandValidator;
import net.zffu.hardened.api.context.CommandContext;

import java.util.Arrays;

/**
* Example on how to make your own custom {@link net.zffu.hardened.api.commands.validator.CommandValidator}.
* @see {@link CustomCommandExample}
*/
public class CustomCommandValidator implements CommandValidator<CustomCommandExample> {

public static CustomCommandValidator instance = new CustomCommandValidator();

@Override
public boolean validate(CustomCommandExample command, CommandContext invoker) {
if(Arrays.asList(command.getAllowedInvokers()).contains(invoker.getInvoker().getType())) return true; // Could simplify that but its for the sake of the example
return false;
}

}

0 comments on commit 961d577

Please sign in to comment.