Skip to content

Commit eeb0c7a

Browse files
authored
feat: make commands and module annotations way better (#201)
1 parent d989287 commit eeb0c7a

File tree

6 files changed

+116
-136
lines changed

6 files changed

+116
-136
lines changed
Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
11
package minevalley.core.api;
22

3+
import lombok.Getter;
4+
5+
@Getter
6+
@SuppressWarnings("unused")
37
public abstract class CoreModule {
48

9+
private final Module moduleDescription;
10+
11+
public CoreModule() {
12+
final Class<? extends CoreModule> clazz = getClass();
13+
if (!clazz.isAnnotationPresent(Module.class)) {
14+
throw new IllegalArgumentException("Module annotation is missing in module '" + clazz.getSimpleName() + "'");
15+
}
16+
this.moduleDescription = clazz.getAnnotation(Module.class);
17+
}
18+
519
/**
620
* Is called when the module is enabled.
721
*/
8-
public abstract void onEnable();
22+
public void onEnable() {
23+
// do nothing
24+
}
925

1026
/**
1127
* Is called when the module is disabled.
1228
*/
1329
public void onDisable() {
14-
30+
// do nothing
1531
}
1632

1733
public void onCleanup() {
18-
34+
// do nothing
1935
}
2036
}

src/main/java/minevalley/core/api/Description.java renamed to src/main/java/minevalley/core/api/Module.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
@Retention(RetentionPolicy.RUNTIME)
88
@Target(java.lang.annotation.ElementType.TYPE)
9-
public @interface Description {
9+
public @interface Module {
1010

1111
String name();
1212

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,70 @@
11
package minevalley.core.api.command;
22

3-
import minevalley.core.api.users.OnlineUser;
3+
import lombok.NonNull;
44

5-
import java.util.List;
5+
import java.lang.annotation.ElementType;
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.RetentionPolicy;
8+
import java.lang.annotation.Target;
69

7-
/**
8-
* This interface corresponds to the often used CommandExecutor-interface. It's used very similarly, but has some advantages.
9-
*/
10-
public interface Command {
10+
@Retention(RetentionPolicy.RUNTIME)
11+
@Target(ElementType.TYPE_USE)
12+
public @interface Command {
1113

1214
/**
13-
* This is the equivalent to the onCommand-method of the CommandExecutor-interface
15+
* This list is used to make all the strings usable command-variations. It should contain each alias and the original spelling of the command
1416
*
15-
* @param user user who executed this command
16-
* @param args array of arguments
17-
* @return response-enum that decides what is sent to the user
17+
* @return array of all command varies and its aliases
1818
*/
19-
CommandResponse onCommand(OnlineUser user, String[] args);
19+
@NonNull
20+
String[] commands();
2021

2122
/**
22-
* This method is used to define the completions displayed when the player presses TAB.
23+
* <p>
24+
* This string defines the correct syntax.
25+
* </p>
26+
* Begin with the mostly used alias of the command itself without "/"
27+
* Then add all possible arguments with "&lt; &gt;"
28+
* arguments that are not required are marked with "[&lt; &gt;]"
29+
* <br>
30+
* Different argument suggestions are separated by vertical bars (example: <i>see below</i>).
31+
* <br>
32+
* Literal argument suggestions are written in quotation marks (e. g. /restart &lt;seconds | 'now'&gt;)
33+
*/
34+
@NonNull
35+
String syntax() default "<command>";
36+
37+
/**
38+
* This message defined here will be sent, when the onCommand-method returns SUCCESS.
39+
* <br>
40+
* Don't use this method, if the player shouldn't get printed any message on success.
41+
*/
42+
String successMessage() default "";
43+
44+
/**
45+
* If this is enabled, users that are allowed to use this command are also allowed to tab it.
46+
* Disable this if you want to make custom permission-checks beyond the command-options!
2347
*
24-
* @param user user who pressed TAB
25-
* @param args array of the arguments
26-
* @return list of the completions to display
48+
* @return true, if everyone with permission can tab this command.
2749
*/
28-
List<String> onTabComplete(OnlineUser user, String[] args);
50+
boolean allowTabbing() default true;
2951

3052
/**
31-
* This method is used to define whether a user is permissioned to execute/tab this command.
32-
* This is checked, when a user tabs or executes the command.
33-
* <p>
34-
* <b>Note:</b> This method should not have any side effects!
53+
* If abuseWarning() is true, the team will be notified if a player tries to execute this command without permission
54+
* 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
3555
*
36-
* @param user user who tries to execute/tab the command
37-
* @return true, if the user is permissioned - false, if he isn't (error-message will be sent automatically)
56+
* @return true, if the team should be notified about illegal use of this command.
57+
*/
58+
boolean abuseWarning() default false;
59+
60+
/**
61+
* If a value is specified here, the user must execute the command with the specified number of arguments.
62+
* Otherwise, the command will not be invoked and the user will receive a syntax error.
63+
*/
64+
int correctSyntaxLength() default -1;
65+
66+
/**
67+
* If executableIfFrozen is true, this command can be executed even though the user is currently frozen by a team member.
3868
*/
39-
boolean isPermissioned(OnlineUser user);
40-
41-
enum CommandResponse {
42-
SUCCESS,
43-
ABUSE_WARNING,
44-
WRONG_SYNTAX,
45-
NO_PERMISSION,
46-
NONE
47-
}
69+
boolean executableIfFrozen() default false;
4870
}

src/main/java/minevalley/core/api/command/CommandOptions.java

Lines changed: 0 additions & 70 deletions
This file was deleted.
Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,62 @@
11
package minevalley.core.api.command;
22

33
import lombok.Getter;
4-
import minevalley.core.api.Core;
54
import minevalley.core.api.users.OnlineUser;
65

76
import java.util.List;
8-
import java.util.Objects;
97

108
@Getter
11-
public abstract class PlayerCommand implements Command {
9+
@SuppressWarnings("unused")
10+
public abstract class PlayerCommand {
1211

13-
private final CommandOptions description;
12+
private final Command description;
1413

1514
public PlayerCommand() {
16-
this.description = Objects.requireNonNull(
17-
getClass().getAnnotation(CommandOptions.class), "CommandOptions-Annotation is missing!");
18-
Core.registerCommand(this);
15+
final Class<? extends PlayerCommand> clazz = getClass();
16+
if (!clazz.isAnnotationPresent(Command.class)) {
17+
throw new IllegalArgumentException("CommandOptions annotation is missing in " + clazz.getSimpleName());
18+
}
19+
this.description = clazz.getAnnotation(Command.class);
1920
}
2021

21-
@Override
22+
/**
23+
* This is the equivalent to the onCommand-method of the CommandExecutor-interface
24+
*
25+
* @param user user who executed this command
26+
* @param args array of arguments
27+
* @return response-enum that decides what is sent to the user
28+
*/
29+
public abstract CommandResponse onCommand(OnlineUser user, String[] args);
30+
31+
/**
32+
* This method is used to define the completions displayed when the player presses TAB.
33+
*
34+
* @param user user who pressed TAB
35+
* @param args array of the arguments
36+
* @return list of the completions to display
37+
*/
2238
public List<String> onTabComplete(OnlineUser user, String[] args) {
2339
return null;
2440
}
2541

26-
@Override
42+
/**
43+
* This method is used to define whether a user is permissioned to execute/tab this command.
44+
* This is checked, when a user tabs or executes the command.
45+
* <p>
46+
* <b>Note:</b> This method should not have any side effects!
47+
*
48+
* @param user user who tries to execute/tab the command
49+
* @return true, if the user is permissioned - false, if he isn't (error-message will be sent automatically)
50+
*/
2751
public boolean isPermissioned(OnlineUser user) {
2852
return true;
2953
}
54+
55+
public enum CommandResponse {
56+
SUCCESS,
57+
ABUSE_WARNING,
58+
WRONG_SYNTAX,
59+
NO_PERMISSION,
60+
NONE
61+
}
3062
}

src/main/java/minevalley/core/api/users/OnlineUser.java

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public interface OnlineUser extends User {
4444

4545
/**
4646
* Gets whether this user is currently logged in via labymod.
47+
* <br>
4748
* <b>Note:</b> When joining, this boolean isn't set immediately. Make sure to use a delay, when using this method in a PlayerJoinListener!
4849
*
4950
* @return true, if this user is using labymod
@@ -271,7 +272,7 @@ public interface OnlineUser extends User {
271272
*/
272273
void sendError();
273274

274-
// ChatInterface
275+
// ChatInput
275276

276277
/**
277278
* 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,34 +282,13 @@ public interface OnlineUser extends User {
281282
*/
282283
void input(@NonNull String message, @NonNull Consumer<String> callback);
283284

284-
/**
285-
* Gets whether the user is currently in a chat input.
286-
*
287-
* @return true, if the user is in a chat input
288-
* @deprecated use isInChatInput()!
289-
*/
290-
@Deprecated
291-
default boolean isInChatInterface() {
292-
return isInChatInput();
293-
}
294-
295285
/**
296286
* Gets whether the user is currently in a chat input.
297287
*
298288
* @return true, if the user is in a chat input
299289
*/
300290
boolean isInChatInput();
301291

302-
/**
303-
* Lets the user leave his current chat input
304-
*
305-
* @deprecated use leaveChatInput instead!
306-
*/
307-
@Deprecated
308-
default void leaveInterface() {
309-
leaveChatInput();
310-
}
311-
312292
/**
313293
* Lets the user leave his current chat input
314294
*/

0 commit comments

Comments
 (0)