Skip to content

Commit 97550ed

Browse files
committed
Add support for multiple Annotation for commands
1 parent 1a0e016 commit 97550ed

5 files changed

Lines changed: 87 additions & 9 deletions

File tree

src/main/java/com/burchard36/BurchAPI.java

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.burchard36;
22

33
import com.burchard36.command.ApiCommand;
4-
import com.burchard36.command.annotation.RegisterCommand;
4+
import com.burchard36.command.annotation.*;
55
import com.burchard36.command.exceptions.CommandConstructorNotFoundException;
66
import com.burchard36.command.exceptions.CommandInterfaceNotFoundException;
77
import com.burchard36.command.exceptions.InvalidCommandAnnotationException;
@@ -17,6 +17,7 @@
1717
import java.lang.reflect.Field;
1818
import java.lang.reflect.InvocationTargetException;
1919
import java.util.ArrayList;
20+
import java.util.Arrays;
2021
import java.util.List;
2122
import java.util.Set;
2223

@@ -26,7 +27,8 @@ public final class BurchAPI implements Api {
2627
public static JavaPlugin INSTANCE;
2728
private HologramManager hologramManager;
2829

29-
public BurchAPI() {}
30+
public BurchAPI() {
31+
}
3032

3133
/**
3234
* Initializes the API to a plugin
@@ -75,15 +77,42 @@ public BurchAPI initializeApi(final JavaPlugin plugin) {
7577
}
7678

7779
final RegisterCommand commandAnnotation = command.getClass().getAnnotation(RegisterCommand.class);
78-
if (commandAnnotation == null) throw new CommandInterfaceNotFoundException("The command class: " + clazz.getName() + " does not have @RegisterCommand as a Annotation!");
80+
final CommandName commandName = command.getClass().getAnnotation(CommandName.class);
81+
final CommandDescription commandDescription = command.getClass().getAnnotation(CommandDescription.class);
82+
final CommandUsage commandUsage = command.getClass().getAnnotation(CommandUsage.class);
83+
final CommandAliases commandAliases = command.getClass().getAnnotation(CommandAliases.class);
84+
85+
if (commandAnnotation != null) {
86+
87+
if (!commandAnnotation.name().equalsIgnoreCase(""))
88+
command.setCommandName(commandAnnotation.name());
89+
90+
if (commandAnnotation.name().equalsIgnoreCase("") && commandName != null
91+
&& !commandName.name().equalsIgnoreCase("")) {
92+
command.setCommandName(commandName.name());
93+
} else throw new InvalidCommandAnnotationException("The class: " + command.getClass().getName() + " did not have a valid command name set!");
94+
95+
if (!commandAnnotation.description().equalsIgnoreCase(""))
96+
command.setDescription(commandAnnotation.description());
7997

80-
if (commandAnnotation.name().equalsIgnoreCase(""))
81-
throw new InvalidCommandAnnotationException("The command class: " + clazz.getName() + " @RegisterCommand interface does not have a name parameter!");
98+
if (commandAnnotation.description().equalsIgnoreCase("") && commandDescription != null
99+
&& !commandDescription.description().equalsIgnoreCase(""))
100+
command.setDescription(commandDescription.description());
82101

83-
command.setCommandName(commandAnnotation.name())
84-
.setCommandDescription(commandAnnotation.description())
85-
.setCommandAliases(commandAnnotation.aliases())
86-
.setCommandUsage(commandAnnotation.usageMessage());
102+
if (!commandAnnotation.usageMessage().equalsIgnoreCase(""))
103+
command.setUsage(commandAnnotation.usageMessage());
104+
105+
if (commandAnnotation.usageMessage().equalsIgnoreCase("") && commandUsage != null
106+
&& !commandUsage.usageMessage().equalsIgnoreCase(""))
107+
command.setUsage(commandUsage.usageMessage());
108+
109+
if (commandAnnotation.aliases().length != 0)
110+
command.setAliases(Arrays.asList(commandAnnotation.aliases()));
111+
112+
if (commandAnnotation.aliases().length <= 0 && commandAliases != null
113+
&& commandAliases.aliases().length > 0)
114+
command.setAliases(Arrays.asList(commandAliases.aliases()));
115+
}
87116
registerCommand(command);
88117
}
89118
return this;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.burchard36.command.annotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.TYPE)
10+
public @interface CommandAliases {
11+
String[] aliases() default {};
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.burchard36.command.annotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.TYPE)
10+
public @interface CommandDescription {
11+
String description();
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.burchard36.command.annotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.TYPE)
10+
public @interface CommandName {
11+
12+
String name() default "";
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.burchard36.command.annotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.TYPE)
10+
public @interface CommandUsage {
11+
String usageMessage() default "";
12+
}

0 commit comments

Comments
 (0)