diff --git a/api-examples/src/main/java/net/zffu/hardened/examples/api/CustomCommandExample.java b/api-examples/src/main/java/net/zffu/hardened/examples/api/CustomCommandExample.java new file mode 100644 index 0000000..955648d --- /dev/null +++ b/api-examples/src/main/java/net/zffu/hardened/examples/api/CustomCommandExample.java @@ -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; + } +} diff --git a/api-examples/src/main/java/net/zffu/hardened/examples/api/CustomCommandValidator.java b/api-examples/src/main/java/net/zffu/hardened/examples/api/CustomCommandValidator.java new file mode 100644 index 0000000..d2f4e40 --- /dev/null +++ b/api-examples/src/main/java/net/zffu/hardened/examples/api/CustomCommandValidator.java @@ -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 { + + 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; + } + +}