Skip to content

Commit fd4efe9

Browse files
committed
Initial commit
0 parents  commit fd4efe9

File tree

11 files changed

+556
-0
lines changed

11 files changed

+556
-0
lines changed

pom.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>xyz.damt</groupId>
8+
<artifactId>CommandAPI</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>8</maven.compiler.source>
13+
<maven.compiler.target>8</maven.compiler.target>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.spigotmc</groupId>
19+
<artifactId>spigot-api</artifactId>
20+
<version>1.16.5-R0.1-SNAPSHOT</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.projectlombok</groupId>
24+
<artifactId>lombok</artifactId>
25+
<version>1.18.20</version>
26+
</dependency>
27+
</dependencies>
28+
29+
</project>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package xyz.damt.command;
2+
3+
import lombok.Getter;
4+
import org.bukkit.plugin.java.JavaPlugin;
5+
import xyz.damt.command.command.Command;
6+
import xyz.damt.command.command.CommandFinder;
7+
import xyz.damt.command.command.CommandRegistery;
8+
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
12+
@Getter
13+
public class CommandHandler {
14+
15+
@Getter
16+
private static CommandHandler instance;
17+
18+
private final Map<String, Command> commandMap = new HashMap<>();
19+
private final JavaPlugin javaPlugin;
20+
private final CommandRegistery commandRegistery;
21+
22+
/**
23+
* Command Handler that handles the whole
24+
* registery process of the commands
25+
*
26+
* @param javaPlugin {@link JavaPlugin}
27+
*/
28+
29+
public CommandHandler(JavaPlugin javaPlugin) {
30+
this.javaPlugin = javaPlugin;
31+
this.commandRegistery = new CommandRegistery(javaPlugin);
32+
}
33+
34+
/**
35+
* Gets a command based on it's name
36+
*
37+
* @param name {@link String}
38+
* @return {@link }
39+
*/
40+
41+
public final Command getCommand(String name) {
42+
return commandMap.get(name);
43+
}
44+
45+
/**
46+
* Registers a command based on it's name
47+
*
48+
* @param object class of the command
49+
* @param name name of the command
50+
* @return {@link CommandHandler}
51+
*/
52+
53+
public final CommandHandler register(Object object, String name) {
54+
commandMap.put(name, new CommandFinder(object).getCommand(name));
55+
return this;
56+
}
57+
58+
/**
59+
* Registers a whole class/finds all commands in a class
60+
* and registers all
61+
*
62+
* @param objects classes to find and register the commands from
63+
* @return {@link CommandHandler}
64+
*/
65+
66+
public final CommandHandler register(Object... objects) {
67+
for (Object object : objects) {
68+
CommandFinder commandFinder = new CommandFinder(object);
69+
commandFinder.getCommands().forEach(command -> commandMap.put(command.getName(), command));
70+
}
71+
return this;
72+
}
73+
74+
/**
75+
* Inputs and registers all commands
76+
* That are added to the list
77+
*
78+
* @return {@link CommandHandler}
79+
*/
80+
81+
public final CommandHandler registerCommands() {
82+
commandMap.values().forEach(command -> commandRegistery.registerCommand(command));
83+
return this;
84+
}
85+
86+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package xyz.damt.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.METHOD)
10+
public @interface Command {
11+
12+
String value();
13+
14+
String usage() default "";
15+
16+
boolean async() default false;
17+
18+
String description() default "";
19+
20+
String[] aliases() default {""};
21+
22+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package xyz.damt.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.METHOD)
10+
public @interface Permission {
11+
12+
String permission();
13+
14+
String message();
15+
16+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package xyz.damt.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.PARAMETER)
10+
public @interface Sender {
11+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package xyz.damt.command.command;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
import lombok.SneakyThrows;
7+
import org.bukkit.ChatColor;
8+
import org.bukkit.command.CommandSender;
9+
import org.bukkit.entity.Player;
10+
import xyz.damt.command.CommandHandler;
11+
import xyz.damt.command.executor.CommandExecutor;
12+
13+
import java.lang.reflect.Method;
14+
import java.util.LinkedList;
15+
import java.util.List;
16+
17+
@AllArgsConstructor
18+
@Setter
19+
@Getter
20+
public class Command {
21+
22+
private final String name, usage, description;
23+
private final String[] aliases;
24+
private final boolean async;
25+
private final Method method;
26+
27+
private final CommandExecutor commandExecutor;
28+
private final List<Command> subCommands = new LinkedList<>();
29+
30+
private boolean player;
31+
private String permission, permissionMessage;
32+
33+
/**
34+
* Constructs a new instance of {@link Command}
35+
*
36+
* @param name name of the command
37+
* @param aliases aliases of the command
38+
* @param method method of the command
39+
* @param description description of the command
40+
* @param async if the command should be ran async or not
41+
*/
42+
43+
public Command(String name, String[] aliases, Method method, String description, String usage, boolean async) {
44+
this.name = name;
45+
this.aliases = aliases;
46+
this.method = method;
47+
this.description = description;
48+
this.async = async;
49+
this.usage = usage;
50+
51+
this.commandExecutor = new CommandExecutor(this);
52+
}
53+
54+
/**
55+
* Executes the command
56+
*
57+
* @param commandSender sender of the command
58+
* @param args arguments of the command
59+
*/
60+
61+
@SneakyThrows
62+
public void execute(CommandSender commandSender, String[] args) {
63+
64+
if (async) {
65+
CommandHandler.getInstance().getJavaPlugin().getServer().getScheduler()
66+
.runTaskAsynchronously(CommandHandler.getInstance().getJavaPlugin(), () -> execute(commandSender, args));
67+
return;
68+
}
69+
70+
if (player && !(commandSender instanceof Player)) {
71+
commandSender.sendMessage(ChatColor.RED + "This is a player command only!");
72+
return;
73+
}
74+
75+
if (permission != null && !commandSender.hasPermission(permission)) {
76+
commandSender.sendMessage(permissionMessage);
77+
return;
78+
}
79+
80+
if (player) {
81+
Player player = (Player) commandSender;
82+
method.invoke(this, player, args);
83+
} else {
84+
method.invoke(this, commandSender, args);
85+
}
86+
}
87+
88+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package xyz.damt.command.command;
2+
3+
import org.bukkit.entity.Player;
4+
import xyz.damt.command.annotation.Permission;
5+
import xyz.damt.command.annotation.Sender;
6+
7+
import java.lang.reflect.Method;
8+
import java.lang.reflect.Parameter;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
import java.util.stream.Collectors;
12+
13+
public class CommandFinder {
14+
15+
private final Object object;
16+
private final List<Command> commands = new ArrayList<>();
17+
18+
/**
19+
* Constructs a new instance of {@link CommandFinder}
20+
* And finds all commands inside of a class
21+
*
22+
* @param object {@link Object}
23+
*/
24+
25+
public CommandFinder(Object object) {
26+
this.object = object;
27+
28+
this.init();
29+
this.edit();
30+
}
31+
32+
/**
33+
* Gets one command based on it's name
34+
*
35+
* @param name name of the command
36+
* @return {@link Command}
37+
*/
38+
39+
public final Command getCommand(String name) {
40+
return commands.stream().filter(command -> command.getName().equalsIgnoreCase(name)).findFirst().orElse(null);
41+
}
42+
43+
/**
44+
* Gets all commands inside of the class
45+
*
46+
* @return {@link List<Command>}*
47+
*/
48+
49+
public final List<Command> getCommands() {
50+
return commands;
51+
}
52+
53+
/**
54+
* Gets all the sub commands of a command
55+
*
56+
* @param name name of the command
57+
* @return {@link List<Command>}
58+
*/
59+
60+
public final List<Command> getCommands(String name) {
61+
return commands.stream().filter(command -> command.getName().split("\\s+")
62+
[0].equalsIgnoreCase(name)).collect(Collectors.toList());
63+
}
64+
65+
/**
66+
* Adds all sub commands to a command
67+
*/
68+
69+
private final void edit() {
70+
commands.forEach(command -> {
71+
String[] args = command.getName().split("\\s+");
72+
73+
if (args.length > 0)
74+
return;
75+
76+
List<Command> subCommands = getCommands(args[0]);
77+
command.getSubCommands().addAll(subCommands);
78+
});
79+
}
80+
81+
/**
82+
* Creates and finds all sub commands
83+
*/
84+
85+
private final void init() {
86+
final Class<?> clazz = object.getClass();
87+
88+
for (Method method : clazz.getMethods()) {
89+
method.setAccessible(true);
90+
91+
xyz.damt.command.annotation.Command commandAnnotation = method.getAnnotation(xyz.damt.command.annotation.Command.class);
92+
93+
if (commandAnnotation != null) {
94+
Permission permissionAnnotation = method.getAnnotation(Permission.class);
95+
Command command = new Command(commandAnnotation.value(), commandAnnotation.aliases(), method, commandAnnotation.description(), commandAnnotation.usage(), commandAnnotation.async());
96+
97+
if (permissionAnnotation != null) {
98+
command.setPermission(permissionAnnotation.permission());
99+
command.setPermissionMessage(permissionAnnotation.message());
100+
}
101+
102+
for (Parameter parameter : method.getParameters()) {
103+
Sender senderAnnotation = parameter.getAnnotation(Sender.class);
104+
105+
if (senderAnnotation != null) {
106+
command.setPlayer(parameter.getType().equals(Player.class));
107+
}
108+
}
109+
110+
commands.add(command);
111+
}
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)