Skip to content
This repository has been archived by the owner on Dec 31, 2021. It is now read-only.

Commit

Permalink
Basic Command API
Browse files Browse the repository at this point in the history
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
phase committed Jun 13, 2016
1 parent ccbff71 commit 04db675
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/main/java/org/fountainmc/api/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import javax.annotation.Nonnull;

import org.fountainmc.api.plugin.PluginManager;
import org.fountainmc.api.command.CommandManager;;

import static com.google.common.base.Preconditions.*;

@Nonnull
public interface Server extends ServerInfo {

public PluginManager getPluginManager();

public CommandManager getCommandManager();

public String[] getLaunchArguments();

Expand Down
25 changes: 25 additions & 0 deletions src/main/java/org/fountainmc/api/command/Command.java
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 src/main/java/org/fountainmc/api/command/CommandManager.java
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.

Copy link
@PizzaCrust

PizzaCrust Jun 13, 2016

Contributor

senderClass.isAssignableFrom(sender)

This comment has been minimized.

Copy link
@phase
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;
}
}

}
6 changes: 6 additions & 0 deletions src/main/java/org/fountainmc/api/command/CommandSender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.fountainmc.api.command;


public interface CommandSender {

}
3 changes: 2 additions & 1 deletion src/main/java/org/fountainmc/api/entity/Player.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.fountainmc.api.entity;

import java.util.UUID;
import org.fountainmc.api.command.CommandSender;

public interface Player extends EntityLiving {
public interface Player extends EntityLiving, CommandSender {

String getName();

Expand Down

0 comments on commit 04db675

Please sign in to comment.