-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added custom command & validator example
- Loading branch information
Showing
2 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
api-examples/src/main/java/net/zffu/hardened/examples/api/CustomCommandExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
api-examples/src/main/java/net/zffu/hardened/examples/api/CustomCommandValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |