Skip to content

Commit 9cd7dc9

Browse files
author
damt
committed
[+] Started to add providers and more, not done yet.
1 parent f2f1944 commit 9cd7dc9

File tree

12 files changed

+260
-9
lines changed

12 files changed

+260
-9
lines changed

src/main/java/xyz/damt/command/CommandHandler.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import xyz.damt.command.command.Command;
66
import xyz.damt.command.command.CommandFinder;
77
import xyz.damt.command.command.CommandRegistery;
8+
import xyz.damt.command.provider.CommandProvider;
9+
import xyz.damt.command.provider.CommandProviderHandler;
810

911
import java.util.HashMap;
1012
import java.util.Map;
@@ -17,6 +19,7 @@ public class CommandHandler {
1719

1820
private final Map<String, Command> commandMap = new HashMap<>();
1921
private final JavaPlugin javaPlugin;
22+
private final CommandProviderHandler commandProviderHandler;
2023
private final CommandRegistery commandRegistery;
2124

2225
/**
@@ -28,9 +31,24 @@ public class CommandHandler {
2831

2932
public CommandHandler(JavaPlugin javaPlugin) {
3033
this.javaPlugin = javaPlugin;
34+
35+
this.commandProviderHandler = new CommandProviderHandler();
3136
this.commandRegistery = new CommandRegistery(javaPlugin);
3237
}
3338

39+
/**
40+
* Binds a class to a command provider
41+
*
42+
* @param clazz class to bind
43+
* @param commandProvider command provider
44+
* @return {@link CommandHandler}
45+
*/
46+
47+
public final CommandHandler bind(Class<?> clazz, CommandProvider<?> commandProvider) {
48+
commandProviderHandler.register(clazz, commandProvider);
49+
return this;
50+
}
51+
3452
/**
3553
* Gets a command based on it's name
3654
*
@@ -79,7 +97,7 @@ public final CommandHandler register(Object... objects) {
7997
*/
8098

8199
public final CommandHandler registerCommands() {
82-
commandMap.values().forEach(command -> commandRegistery.registerCommand(command));
100+
commandMap.values().forEach(commandRegistery::registerCommand);
83101
return this;
84102
}
85103

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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 Name {
11+
12+
String value();
13+
14+
}

src/main/java/xyz/damt/command/command/Command.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.bukkit.command.CommandSender;
99
import org.bukkit.entity.Player;
1010
import xyz.damt.command.CommandHandler;
11+
import xyz.damt.command.exception.CommandProviderNullException;
1112
import xyz.damt.command.executor.CommandExecutor;
1213

1314
import java.lang.reflect.Method;
@@ -27,6 +28,7 @@ public class Command {
2728

2829
private final CommandExecutor commandExecutor;
2930
private final List<Command> subCommands = new LinkedList<>();
31+
private final List<CommandParameter> commandParameters = new LinkedList<>();
3032

3133
private boolean player;
3234
private String permission, permissionMessage;
@@ -80,11 +82,34 @@ public void execute(CommandSender commandSender, String[] args) {
8082
return;
8183
}
8284

85+
if (args.length < commandParameters.size()) {
86+
commandSender.sendMessage(ChatColor.RED + "Wrong Usage: " + usage);
87+
return;
88+
}
89+
90+
List<Object> objects = new LinkedList<>();
91+
92+
commandParameters.forEach(commandParameter -> {
93+
94+
try {
95+
objects.add(commandParameter.getCommandProvider().provide(args[commandParameters.indexOf(commandParameter)]));
96+
} catch (CommandProviderNullException e) {
97+
if (e.getMessage() == null) {
98+
commandSender.sendMessage(ChatColor.RED + "The argument '" + commandParameter.getName() + "' is null!");
99+
return;
100+
}
101+
102+
commandSender.sendMessage(e.getMessage());
103+
return;
104+
}
105+
106+
});
107+
83108
if (player) {
84109
Player player = (Player) commandSender;
85-
method.invoke(object, player, args);
110+
method.invoke(object, player, objects.toArray());
86111
} else {
87-
method.invoke(object, commandSender, args);
112+
method.invoke(object, commandSender, objects.toArray());
88113
}
89114
}
90115

src/main/java/xyz/damt/command/command/CommandFinder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ private final void init() {
104104

105105
if (senderAnnotation != null) {
106106
command.setPlayer(parameter.getType().equals(Player.class));
107+
} else {
108+
command.getCommandParameters().add(new CommandParameter(command, parameter));
107109
}
108110
}
109111

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package xyz.damt.command.command;
2+
3+
import lombok.Getter;
4+
import xyz.damt.command.CommandHandler;
5+
import xyz.damt.command.annotation.Name;
6+
import xyz.damt.command.provider.CommandProvider;
7+
8+
import java.lang.reflect.Parameter;
9+
10+
@Getter
11+
public class CommandParameter {
12+
13+
private final Parameter parameter;
14+
private final Command command;
15+
16+
private CommandProvider<?> commandProvider;
17+
private String name;
18+
19+
public CommandParameter(Command command, Parameter parameter) {
20+
this.parameter = parameter;
21+
this.command = command;
22+
23+
this.init();
24+
}
25+
26+
private final void init() {
27+
Name name = parameter.getAnnotation(Name.class);
28+
29+
if (name == null)
30+
this.name = parameter.getName();
31+
else this.name = name.value();
32+
33+
this.commandProvider = CommandHandler.getInstance().getCommandProviderHandler().getProvider(parameter);
34+
}
35+
36+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package xyz.damt.command.exception;
2+
3+
public class CommandProviderNullException extends Exception {
4+
5+
public CommandProviderNullException(String message) {
6+
super(message);
7+
}
8+
9+
}

src/main/java/xyz/damt/command/executor/CommandExecutor.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import org.bukkit.command.CommandSender;
44
import org.bukkit.command.defaults.BukkitCommand;
55
import xyz.damt.command.command.Command;
6+
import xyz.damt.command.command.CommandParameter;
67

78
import java.util.Arrays;
9+
import java.util.Collections;
810
import java.util.LinkedList;
911
import java.util.List;
1012

@@ -58,14 +60,16 @@ public boolean execute(CommandSender commandSender, String s, String[] strings)
5860

5961
@Override
6062
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
61-
final List<String> strings = new LinkedList<>();
63+
for (int i = 0; i < args.length; i++) {
64+
CommandParameter commandParameter = command.getCommandParameters().get(i);
6265

63-
command.getSubCommands().forEach(command1 -> {
64-
String[] commandSplit = command1.getName().split("\\s+");
65-
strings.add(commandSplit[1]);
66-
});
66+
if (commandParameter == null)
67+
return Collections.emptyList();
6768

68-
return strings;
69+
return commandParameter.getCommandProvider().suggestions(commandParameter);
70+
}
71+
72+
return Collections.emptyList();
6973
}
7074

7175
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package xyz.damt.command.provider;
2+
3+
import xyz.damt.command.command.CommandParameter;
4+
import xyz.damt.command.exception.CommandProviderNullException;
5+
6+
import java.util.List;
7+
8+
public interface CommandProvider<T> {
9+
10+
T provide(String s) throws CommandProviderNullException;
11+
12+
List<String> suggestions(CommandParameter commandParameter);
13+
14+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package xyz.damt.command.provider;
2+
3+
import java.lang.reflect.Parameter;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
public class CommandProviderHandler {
8+
9+
private final Map<Class<?>, CommandProvider<?>> commandProviderMap = new HashMap<>();
10+
11+
public final void register(Class<?> clazz, CommandProvider<?> commandProvider) {
12+
commandProviderMap.put(clazz, commandProvider);
13+
}
14+
15+
public final CommandProvider<?> getProvider(Class<?> clazz) {
16+
return commandProviderMap.get(clazz);
17+
}
18+
19+
public final CommandProvider<?> getProvider(Parameter parameter) {
20+
return commandProviderMap.get(parameter.getType());
21+
}
22+
23+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package xyz.damt.command.provider.impl.bukkit;
2+
3+
import org.bukkit.ChatColor;
4+
import org.bukkit.OfflinePlayer;
5+
import org.bukkit.entity.HumanEntity;
6+
import org.bukkit.entity.Player;
7+
import org.bukkit.plugin.java.JavaPlugin;
8+
import xyz.damt.command.command.CommandParameter;
9+
import xyz.damt.command.exception.CommandProviderNullException;
10+
import xyz.damt.command.provider.CommandProvider;
11+
12+
import java.util.List;
13+
import java.util.stream.Collectors;
14+
15+
public class OfflinePlayerProvider implements CommandProvider<OfflinePlayer> {
16+
17+
private final JavaPlugin javaPlugin;
18+
19+
public OfflinePlayerProvider(JavaPlugin javaPlugin) {
20+
this.javaPlugin = javaPlugin;
21+
}
22+
23+
@Override
24+
public OfflinePlayer provide(String s) throws CommandProviderNullException {
25+
final OfflinePlayer player = javaPlugin.getServer().getOfflinePlayer(s);
26+
27+
if (!player.hasPlayedBefore() || player == null)
28+
throw new CommandProviderNullException(ChatColor.RED + "The player you specified never logged on the server before!");
29+
30+
return player;
31+
}
32+
33+
@Override
34+
public List<String> suggestions(CommandParameter commandParameter) {
35+
return javaPlugin.getServer().getOnlinePlayers().stream().map(HumanEntity::getName).collect(Collectors.toList());
36+
}
37+
38+
}

0 commit comments

Comments
 (0)