From eeb0c7a7e8ddbcd98d620a4147454f05537ee1a7 Mon Sep 17 00:00:00 2001 From: "Vincent B." <79211348+Snabeldier@users.noreply.github.com> Date: Thu, 8 Aug 2024 02:19:23 +0200 Subject: [PATCH] feat: make commands and module annotations way better (#201) --- .../java/minevalley/core/api/CoreModule.java | 22 ++++- .../api/{Description.java => Module.java} | 2 +- .../minevalley/core/api/command/Command.java | 84 ++++++++++++------- .../core/api/command/CommandOptions.java | 70 ---------------- .../core/api/command/PlayerCommand.java | 50 +++++++++-- .../minevalley/core/api/users/OnlineUser.java | 24 +----- 6 files changed, 116 insertions(+), 136 deletions(-) rename src/main/java/minevalley/core/api/{Description.java => Module.java} (90%) delete mode 100644 src/main/java/minevalley/core/api/command/CommandOptions.java diff --git a/src/main/java/minevalley/core/api/CoreModule.java b/src/main/java/minevalley/core/api/CoreModule.java index d693ff03..399c682d 100644 --- a/src/main/java/minevalley/core/api/CoreModule.java +++ b/src/main/java/minevalley/core/api/CoreModule.java @@ -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 } } \ No newline at end of file diff --git a/src/main/java/minevalley/core/api/Description.java b/src/main/java/minevalley/core/api/Module.java similarity index 90% rename from src/main/java/minevalley/core/api/Description.java rename to src/main/java/minevalley/core/api/Module.java index 228e8516..6320f428 100644 --- a/src/main/java/minevalley/core/api/Description.java +++ b/src/main/java/minevalley/core/api/Module.java @@ -6,7 +6,7 @@ @Retention(RetentionPolicy.RUNTIME) @Target(java.lang.annotation.ElementType.TYPE) -public @interface Description { +public @interface Module { String name(); diff --git a/src/main/java/minevalley/core/api/command/Command.java b/src/main/java/minevalley/core/api/command/Command.java index f55dd322..49a3ab32 100644 --- a/src/main/java/minevalley/core/api/command/Command.java +++ b/src/main/java/minevalley/core/api/command/Command.java @@ -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. + *
+ * This string defines the correct syntax. + *
+ * 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 "[< >]" + *- * Note: 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; } \ No newline at end of file diff --git a/src/main/java/minevalley/core/api/command/CommandOptions.java b/src/main/java/minevalley/core/api/command/CommandOptions.java deleted file mode 100644 index 7738eca2..00000000 --- a/src/main/java/minevalley/core/api/command/CommandOptions.java +++ /dev/null @@ -1,70 +0,0 @@ -package minevalley.core.api.command; - -import lombok.NonNull; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.TYPE_USE) -public @interface CommandOptions { - - /** - * This list is used to make all the strings usable command-variations. It should contain each alias and the original spelling of the command - * - * @return array of all command varies and its aliases - */ - @NonNull - String[] commands(); - - /** - *
- * This string defines the correct syntax. - *
- * 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 "[< >]" - *
+ * Note: 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
+ }
}
\ No newline at end of file
diff --git a/src/main/java/minevalley/core/api/users/OnlineUser.java b/src/main/java/minevalley/core/api/users/OnlineUser.java
index 6ccde657..484d386d 100644
--- a/src/main/java/minevalley/core/api/users/OnlineUser.java
+++ b/src/main/java/minevalley/core/api/users/OnlineUser.java
@@ -44,6 +44,7 @@ public interface OnlineUser extends User {
/**
* Gets whether this user is currently logged in via labymod.
+ *
* Note: When joining, this boolean isn't set immediately. Make sure to use a delay, when using this method in a PlayerJoinListener!
*
* @return true, if this user is using labymod
@@ -271,7 +272,7 @@ public interface OnlineUser extends User {
*/
void sendError();
- // ChatInterface
+ // ChatInput
/**
* Asks the player for any type of input via a chat-interface. The player can leave this interface. If he writes something into this interface, the callback gets called.
@@ -281,17 +282,6 @@ public interface OnlineUser extends User {
*/
void input(@NonNull String message, @NonNull Consumer