This repository has been archived by the owner on Dec 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
You can annotate commands with @command. Registering is done through the Server's CommandManager. Commands are fired through Reflection. Signed-off-by: Jadon Fowler <jadonflower@gmail.com>
- Loading branch information
Showing
5 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.fountainmc.api.command; | ||
|
||
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.METHOD) | ||
public @interface Command { | ||
|
||
public String name(); | ||
|
||
public String permission() default ""; | ||
|
||
public String noPermissionMessage() default "You do not have permission to run this command"; | ||
|
||
public String[] aliases() default {}; | ||
|
||
public String description() default ""; | ||
|
||
public String usage() default ""; | ||
|
||
public Class<? extends CommandSender> allow() default CommandSender.class; | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/org/fountainmc/api/command/CommandManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.fountainmc.api.command; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Modifier; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class CommandManager { | ||
|
||
private final List<CommandHandler> commands; | ||
|
||
public CommandManager() { | ||
this.commands = new ArrayList<CommandHandler>(); | ||
} | ||
|
||
public void registerCommands(Object source) { | ||
for (Method method : source.getClass().getDeclaredMethods()) { | ||
if (Modifier.isPublic(method.getModifiers())) { | ||
if (method.isAnnotationPresent(Command.class)) { | ||
Command command = method.getAnnotation(Command.class); | ||
commands.add(new CommandHandler(command, method, source)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public void fireCommand(String command, String[] arguments, CommandSender sender) { | ||
for (CommandHandler handler : commands) { | ||
if (handler.command.name().equalsIgnoreCase(command)) { | ||
Class<? extends CommandSender> senderClass = handler.command.allow(); | ||
if (sender.getClass().isInstance(senderClass)) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
try { | ||
handler.method.invoke(handler.source, arguments, senderClass.cast(sender)); | ||
} | ||
catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private class CommandHandler { | ||
|
||
private final Command command; | ||
private final Method method; | ||
private final Object source; | ||
|
||
public CommandHandler(Command command, Method method, Object source) { | ||
this.command = command; | ||
this.method = method; | ||
this.source = source; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package org.fountainmc.api.command; | ||
|
||
|
||
public interface CommandSender { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
senderClass.isAssignableFrom(sender)