Skip to content

Commit

Permalink
Add support for multiple Annotation for commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Burchard36 committed Apr 24, 2022
1 parent 1a0e016 commit 97550ed
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 9 deletions.
47 changes: 38 additions & 9 deletions src/main/java/com/burchard36/BurchAPI.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.burchard36;

import com.burchard36.command.ApiCommand;
import com.burchard36.command.annotation.RegisterCommand;
import com.burchard36.command.annotation.*;
import com.burchard36.command.exceptions.CommandConstructorNotFoundException;
import com.burchard36.command.exceptions.CommandInterfaceNotFoundException;
import com.burchard36.command.exceptions.InvalidCommandAnnotationException;
Expand All @@ -17,6 +17,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

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

public BurchAPI() {}
public BurchAPI() {
}

/**
* Initializes the API to a plugin
Expand Down Expand Up @@ -75,15 +77,42 @@ public BurchAPI initializeApi(final JavaPlugin plugin) {
}

final RegisterCommand commandAnnotation = command.getClass().getAnnotation(RegisterCommand.class);
if (commandAnnotation == null) throw new CommandInterfaceNotFoundException("The command class: " + clazz.getName() + " does not have @RegisterCommand as a Annotation!");
final CommandName commandName = command.getClass().getAnnotation(CommandName.class);
final CommandDescription commandDescription = command.getClass().getAnnotation(CommandDescription.class);
final CommandUsage commandUsage = command.getClass().getAnnotation(CommandUsage.class);
final CommandAliases commandAliases = command.getClass().getAnnotation(CommandAliases.class);

if (commandAnnotation != null) {

if (!commandAnnotation.name().equalsIgnoreCase(""))
command.setCommandName(commandAnnotation.name());

if (commandAnnotation.name().equalsIgnoreCase("") && commandName != null
&& !commandName.name().equalsIgnoreCase("")) {
command.setCommandName(commandName.name());
} else throw new InvalidCommandAnnotationException("The class: " + command.getClass().getName() + " did not have a valid command name set!");

if (!commandAnnotation.description().equalsIgnoreCase(""))
command.setDescription(commandAnnotation.description());

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

command.setCommandName(commandAnnotation.name())
.setCommandDescription(commandAnnotation.description())
.setCommandAliases(commandAnnotation.aliases())
.setCommandUsage(commandAnnotation.usageMessage());
if (!commandAnnotation.usageMessage().equalsIgnoreCase(""))
command.setUsage(commandAnnotation.usageMessage());

if (commandAnnotation.usageMessage().equalsIgnoreCase("") && commandUsage != null
&& !commandUsage.usageMessage().equalsIgnoreCase(""))
command.setUsage(commandUsage.usageMessage());

if (commandAnnotation.aliases().length != 0)
command.setAliases(Arrays.asList(commandAnnotation.aliases()));

if (commandAnnotation.aliases().length <= 0 && commandAliases != null
&& commandAliases.aliases().length > 0)
command.setAliases(Arrays.asList(commandAliases.aliases()));
}
registerCommand(command);
}
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.burchard36.command.annotation;

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)
public @interface CommandAliases {
String[] aliases() default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.burchard36.command.annotation;

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)
public @interface CommandDescription {
String description();
}
13 changes: 13 additions & 0 deletions src/main/java/com/burchard36/command/annotation/CommandName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.burchard36.command.annotation;

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)
public @interface CommandName {

String name() default "";
}
12 changes: 12 additions & 0 deletions src/main/java/com/burchard36/command/annotation/CommandUsage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.burchard36.command.annotation;

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)
public @interface CommandUsage {
String usageMessage() default "";
}

0 comments on commit 97550ed

Please sign in to comment.