Skip to content

Commit

Permalink
feat: make commands and module annotations way better (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
Snabeldier authored Aug 8, 2024
1 parent d989287 commit eeb0c7a
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 136 deletions.
22 changes: 19 additions & 3 deletions src/main/java/minevalley/core/api/CoreModule.java
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

@Retention(RetentionPolicy.RUNTIME)
@Target(java.lang.annotation.ElementType.TYPE)
public @interface Description {
public @interface Module {

String name();

Expand Down
84 changes: 53 additions & 31 deletions src/main/java/minevalley/core/api/command/Command.java
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 "&lt; &gt;"
* arguments that are not required are marked with "[&lt; &gt;]"
* <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 &lt;seconds | 'now'&gt;)
*/
@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 src/main/java/minevalley/core/api/command/CommandOptions.java

This file was deleted.

50 changes: 41 additions & 9 deletions src/main/java/minevalley/core/api/command/PlayerCommand.java
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
}
}
24 changes: 2 additions & 22 deletions src/main/java/minevalley/core/api/users/OnlineUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public interface OnlineUser extends User {

/**
* Gets whether this user is currently logged in via labymod.
* <br>
* <b>Note:</b> 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
Expand Down Expand Up @@ -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.
Expand All @@ -281,34 +282,13 @@ public interface OnlineUser extends User {
*/
void input(@NonNull String message, @NonNull Consumer<String> callback);

/**
* Gets whether the user is currently in a chat input.
*
* @return true, if the user is in a chat input
* @deprecated use isInChatInput()!
*/
@Deprecated
default boolean isInChatInterface() {
return isInChatInput();
}

/**
* Gets whether the user is currently in a chat input.
*
* @return true, if the user is in a chat input
*/
boolean isInChatInput();

/**
* Lets the user leave his current chat input
*
* @deprecated use leaveChatInput instead!
*/
@Deprecated
default void leaveInterface() {
leaveChatInput();
}

/**
* Lets the user leave his current chat input
*/
Expand Down

0 comments on commit eeb0c7a

Please sign in to comment.