-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make commands and module annotations way better (#201)
- Loading branch information
1 parent
d989287
commit eeb0c7a
Showing
6 changed files
with
116 additions
and
136 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,20 +1,36 @@ | ||
package minevalley.core.api; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
@SuppressWarnings("unused") | ||
public abstract class CoreModule { | ||
|
||
private final Module moduleDescription; | ||
|
||
public CoreModule() { | ||
final Class<? extends CoreModule> clazz = getClass(); | ||
if (!clazz.isAnnotationPresent(Module.class)) { | ||
throw new IllegalArgumentException("Module annotation is missing in module '" + clazz.getSimpleName() + "'"); | ||
} | ||
this.moduleDescription = clazz.getAnnotation(Module.class); | ||
} | ||
|
||
/** | ||
* Is called when the module is enabled. | ||
*/ | ||
public abstract void onEnable(); | ||
public void onEnable() { | ||
// do nothing | ||
} | ||
|
||
/** | ||
* Is called when the module is disabled. | ||
*/ | ||
public void onDisable() { | ||
|
||
// do nothing | ||
} | ||
|
||
public void onCleanup() { | ||
|
||
// do nothing | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,48 +1,70 @@ | ||
package minevalley.core.api.command; | ||
|
||
import minevalley.core.api.users.OnlineUser; | ||
import lombok.NonNull; | ||
|
||
import java.util.List; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* This interface corresponds to the often used CommandExecutor-interface. It's used very similarly, but has some advantages. | ||
*/ | ||
public interface Command { | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE_USE) | ||
public @interface Command { | ||
|
||
/** | ||
* This is the equivalent to the onCommand-method of the CommandExecutor-interface | ||
* This list is used to make all the strings usable command-variations. It should contain each alias and the original spelling of the command | ||
* | ||
* @param user user who executed this command | ||
* @param args array of arguments | ||
* @return response-enum that decides what is sent to the user | ||
* @return array of all command varies and its aliases | ||
*/ | ||
CommandResponse onCommand(OnlineUser user, String[] args); | ||
@NonNull | ||
String[] commands(); | ||
|
||
/** | ||
* This method is used to define the completions displayed when the player presses TAB. | ||
* <p> | ||
* This string defines the correct syntax. | ||
* </p> | ||
* Begin with the mostly used alias of the command itself without "/" | ||
* Then add all possible arguments with "< >" | ||
* arguments that are not required are marked with "[< >]" | ||
* <br> | ||
* Different argument suggestions are separated by vertical bars (example: <i>see below</i>). | ||
* <br> | ||
* Literal argument suggestions are written in quotation marks (e. g. /restart <seconds | 'now'>) | ||
*/ | ||
@NonNull | ||
String syntax() default "<command>"; | ||
|
||
/** | ||
* This message defined here will be sent, when the onCommand-method returns SUCCESS. | ||
* <br> | ||
* Don't use this method, if the player shouldn't get printed any message on success. | ||
*/ | ||
String successMessage() default ""; | ||
|
||
/** | ||
* If this is enabled, users that are allowed to use this command are also allowed to tab it. | ||
* Disable this if you want to make custom permission-checks beyond the command-options! | ||
* | ||
* @param user user who pressed TAB | ||
* @param args array of the arguments | ||
* @return list of the completions to display | ||
* @return true, if everyone with permission can tab this command. | ||
*/ | ||
List<String> onTabComplete(OnlineUser user, String[] args); | ||
boolean allowTabbing() default true; | ||
|
||
/** | ||
* This method is used to define whether a user is permissioned to execute/tab this command. | ||
* This is checked, when a user tabs or executes the command. | ||
* <p> | ||
* <b>Note:</b> This method should not have any side effects! | ||
* If abuseWarning() is true, the team will be notified if a player tries to execute this command without permission | ||
* The return of ABUSE_WARNING in the onCommand method is not related to this setting and can also be carried out if nothing (or false) is specified here | ||
* | ||
* @param user user who tries to execute/tab the command | ||
* @return true, if the user is permissioned - false, if he isn't (error-message will be sent automatically) | ||
* @return true, if the team should be notified about illegal use of this command. | ||
*/ | ||
boolean abuseWarning() default false; | ||
|
||
/** | ||
* If a value is specified here, the user must execute the command with the specified number of arguments. | ||
* Otherwise, the command will not be invoked and the user will receive a syntax error. | ||
*/ | ||
int correctSyntaxLength() default -1; | ||
|
||
/** | ||
* If executableIfFrozen is true, this command can be executed even though the user is currently frozen by a team member. | ||
*/ | ||
boolean isPermissioned(OnlineUser user); | ||
|
||
enum CommandResponse { | ||
SUCCESS, | ||
ABUSE_WARNING, | ||
WRONG_SYNTAX, | ||
NO_PERMISSION, | ||
NONE | ||
} | ||
boolean executableIfFrozen() default false; | ||
} |
70 changes: 0 additions & 70 deletions
70
src/main/java/minevalley/core/api/command/CommandOptions.java
This file was deleted.
Oops, something went wrong.
50 changes: 41 additions & 9 deletions
50
src/main/java/minevalley/core/api/command/PlayerCommand.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 |
---|---|---|
@@ -1,30 +1,62 @@ | ||
package minevalley.core.api.command; | ||
|
||
import lombok.Getter; | ||
import minevalley.core.api.Core; | ||
import minevalley.core.api.users.OnlineUser; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
@Getter | ||
public abstract class PlayerCommand implements Command { | ||
@SuppressWarnings("unused") | ||
public abstract class PlayerCommand { | ||
|
||
private final CommandOptions description; | ||
private final Command description; | ||
|
||
public PlayerCommand() { | ||
this.description = Objects.requireNonNull( | ||
getClass().getAnnotation(CommandOptions.class), "CommandOptions-Annotation is missing!"); | ||
Core.registerCommand(this); | ||
final Class<? extends PlayerCommand> clazz = getClass(); | ||
if (!clazz.isAnnotationPresent(Command.class)) { | ||
throw new IllegalArgumentException("CommandOptions annotation is missing in " + clazz.getSimpleName()); | ||
} | ||
this.description = clazz.getAnnotation(Command.class); | ||
} | ||
|
||
@Override | ||
/** | ||
* This is the equivalent to the onCommand-method of the CommandExecutor-interface | ||
* | ||
* @param user user who executed this command | ||
* @param args array of arguments | ||
* @return response-enum that decides what is sent to the user | ||
*/ | ||
public abstract CommandResponse onCommand(OnlineUser user, String[] args); | ||
|
||
/** | ||
* This method is used to define the completions displayed when the player presses TAB. | ||
* | ||
* @param user user who pressed TAB | ||
* @param args array of the arguments | ||
* @return list of the completions to display | ||
*/ | ||
public List<String> onTabComplete(OnlineUser user, String[] args) { | ||
return null; | ||
} | ||
|
||
@Override | ||
/** | ||
* This method is used to define whether a user is permissioned to execute/tab this command. | ||
* This is checked, when a user tabs or executes the command. | ||
* <p> | ||
* <b>Note:</b> This method should not have any side effects! | ||
* | ||
* @param user user who tries to execute/tab the command | ||
* @return true, if the user is permissioned - false, if he isn't (error-message will be sent automatically) | ||
*/ | ||
public boolean isPermissioned(OnlineUser user) { | ||
return true; | ||
} | ||
|
||
public enum CommandResponse { | ||
SUCCESS, | ||
ABUSE_WARNING, | ||
WRONG_SYNTAX, | ||
NO_PERMISSION, | ||
NONE | ||
} | ||
} |
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