Skip to content

Commit 111059f

Browse files
author
damt
committed
[+] Added help command service
1 parent 54f51a2 commit 111059f

File tree

7 files changed

+94
-21
lines changed

7 files changed

+94
-21
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
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.help.HelpCommandService;
89
import xyz.damt.command.provider.CommandProvider;
910
import xyz.damt.command.provider.CommandProviderHandler;
1011

@@ -21,6 +22,8 @@ public class CommandHandler {
2122
private final CommandProviderHandler commandProviderHandler;
2223
private final CommandRegistery commandRegistery;
2324

25+
private HelpCommandService helpCommandService;
26+
2427
/**
2528
* Command Handler that handles the whole
2629
* registery process of the commands
@@ -69,7 +72,7 @@ public final Command getCommand(String name) {
6972
*/
7073

7174
public final CommandHandler register(Object object, String name) {
72-
commandMap.put(name, new CommandFinder(object, commandProviderHandler).getCommand(name));
75+
commandMap.put(name, new CommandFinder(object, commandProviderHandler, helpCommandService).getCommand(name));
7376
return this;
7477
}
7578

@@ -83,12 +86,24 @@ public final CommandHandler register(Object object, String name) {
8386

8487
public final CommandHandler register(Object... objects) {
8588
for (Object object : objects) {
86-
CommandFinder commandFinder = new CommandFinder(object, commandProviderHandler);
89+
CommandFinder commandFinder = new CommandFinder(object, commandProviderHandler, helpCommandService);
8790
commandFinder.getCommands().forEach(command -> commandMap.put(command.getName(), command));
8891
}
8992
return this;
9093
}
9194

95+
/**
96+
* Sets the help command service
97+
*
98+
* @param helpCommandService help command service to set to
99+
* @return {@link CommandHandler}
100+
*/
101+
102+
public final CommandHandler helpService(HelpCommandService helpCommandService) {
103+
this.helpCommandService = helpCommandService;
104+
return this;
105+
}
106+
92107
/**
93108
* Inputs and registers all commands
94109
* That are added to the list
@@ -110,4 +125,5 @@ public final CommandHandler registerCommands() {
110125
public JavaPlugin getJavaPlugin() {
111126
return javaPlugin;
112127
}
128+
113129
}

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
import xyz.damt.command.CommandHandler;
1111
import xyz.damt.command.exception.CommandProviderNullException;
1212
import xyz.damt.command.executor.CommandExecutor;
13+
import xyz.damt.command.help.HelpCommandService;
1314

1415
import java.lang.reflect.Method;
16+
import java.util.ArrayList;
1517
import java.util.LinkedList;
1618
import java.util.List;
1719

@@ -27,8 +29,10 @@ public class Command {
2729
private final Object object;
2830

2931
private final CommandExecutor commandExecutor;
30-
private final List<Command> subCommands = new LinkedList<>();
31-
private final List<CommandParameter> commandParameters = new LinkedList<>();
32+
private final HelpCommandService helpCommandService;
33+
34+
private final List<Command> subCommands = new ArrayList<>();
35+
private final List<CommandParameter> commandParameters = new ArrayList<>();
3236

3337
private boolean player;
3438
private String permission, permissionMessage;
@@ -43,7 +47,7 @@ public class Command {
4347
* @param async if the command should be ran async or not
4448
*/
4549

46-
public Command(Object object, String name, String[] aliases, Method method, String description, String usage, boolean async) {
50+
public Command(Object object, String name, String[] aliases, Method method, String description, String usage, HelpCommandService helpCommandService, boolean async) {
4751
this.object = object;
4852

4953
this.name = name;
@@ -53,6 +57,7 @@ public Command(Object object, String name, String[] aliases, Method method, Stri
5357
this.async = async;
5458
this.usage = usage;
5559

60+
this.helpCommandService = helpCommandService;
5661
this.commandExecutor = new CommandExecutor(this);
5762
}
5863

@@ -82,28 +87,30 @@ public void execute(CommandSender commandSender, String[] args) {
8287
return;
8388
}
8489

90+
if (args.length == 0 && !getSubCommands().isEmpty() && helpCommandService != null) {
91+
helpCommandService.get(this, getSubCommands()).forEach(commandSender::sendMessage);
92+
return;
93+
}
94+
8595
if (args.length < commandParameters.size()) {
8696
commandSender.sendMessage(ChatColor.RED + "Wrong Usage: " + usage);
8797
return;
8898
}
8999

90100
List<Object> objects = new LinkedList<>();
91101

92-
commandParameters.forEach(commandParameter -> {
93-
102+
for (CommandParameter commandParameter : commandParameters) {
94103
try {
95104
objects.add(commandParameter.getCommandProvider().provide(args[commandParameters.indexOf(commandParameter)]));
96105
} catch (CommandProviderNullException e) {
97106
if (e.getMessage() == null) {
98107
commandSender.sendMessage(ChatColor.RED + "The argument '" + commandParameter.getName() + "' is null!");
99108
return;
100109
}
101-
102110
commandSender.sendMessage(e.getMessage());
103111
return;
104112
}
105-
106-
});
113+
}
107114

108115
if (player) {
109116
Player player = (Player) commandSender;

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.bukkit.entity.Player;
44
import xyz.damt.command.annotation.Permission;
55
import xyz.damt.command.annotation.Sender;
6+
import xyz.damt.command.help.HelpCommandService;
67
import xyz.damt.command.provider.CommandProviderHandler;
78

89
import java.lang.reflect.Method;
@@ -14,7 +15,10 @@
1415
public class CommandFinder {
1516

1617
private final Object object;
18+
1719
private final CommandProviderHandler commandProviderHandler;
20+
private final HelpCommandService helpCommandService;
21+
1822
private final List<Command> commands = new ArrayList<>();
1923

2024
/**
@@ -24,9 +28,11 @@ public class CommandFinder {
2428
* @param object {@link Object}
2529
*/
2630

27-
public CommandFinder(Object object, CommandProviderHandler commandProviderHandler) {
31+
public CommandFinder(Object object, CommandProviderHandler commandProviderHandler, HelpCommandService helpCommandService) {
2832
this.object = object;
33+
2934
this.commandProviderHandler = commandProviderHandler;
35+
this.helpCommandService = helpCommandService;
3036

3137
this.init();
3238
this.edit();
@@ -95,7 +101,7 @@ private final void init() {
95101

96102
if (commandAnnotation != null) {
97103
Permission permissionAnnotation = method.getAnnotation(Permission.class);
98-
Command command = new Command(object, commandAnnotation.value(), commandAnnotation.aliases(), method, commandAnnotation.description(), commandAnnotation.usage(), commandAnnotation.async());
104+
Command command = new Command(object, commandAnnotation.value(), commandAnnotation.aliases(), method, commandAnnotation.description(), commandAnnotation.usage(), helpCommandService, commandAnnotation.async());
99105

100106
if (permissionAnnotation != null) {
101107
command.setPermission(permissionAnnotation.permission());

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package xyz.damt.command.executor;
22

3+
import com.google.common.collect.Lists;
34
import org.bukkit.command.CommandSender;
45
import org.bukkit.command.defaults.BukkitCommand;
56
import xyz.damt.command.command.Command;
@@ -59,16 +60,12 @@ public boolean execute(CommandSender commandSender, String s, String[] strings)
5960

6061
@Override
6162
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
62-
for (int i = 0; i < args.length; i++) {
63-
CommandParameter commandParameter = command.getCommandParameters().get(i);
63+
CommandParameter commandParameter = command.getCommandParameters().get(args.length);
6464

65-
if (commandParameter == null)
66-
return Collections.emptyList();
65+
if (commandParameter == null)
66+
return Collections.emptyList();
6767

68-
return commandParameter.getCommandProvider().suggestions(commandParameter);
69-
}
70-
71-
return Collections.emptyList();
68+
return commandParameter.getCommandProvider().suggestions(commandParameter);
7269
}
7370

7471
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package xyz.damt.command.help;
2+
3+
import xyz.damt.command.command.Command;
4+
5+
import java.util.List;
6+
7+
public interface HelpCommandService {
8+
9+
List<String> get(Command command, List<Command> subCommands);
10+
11+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package xyz.damt.command.help.impl;
2+
3+
import org.bukkit.ChatColor;
4+
import xyz.damt.command.command.Command;
5+
import xyz.damt.command.help.HelpCommandService;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.stream.Collectors;
10+
11+
public class DefaultHelpCommandService implements HelpCommandService {
12+
13+
private final String mainColor;
14+
private final String secondaryColor;
15+
16+
public DefaultHelpCommandService(String mainColor, String secondaryColor) {
17+
this.mainColor = mainColor;
18+
this.secondaryColor = secondaryColor;
19+
}
20+
21+
@Override
22+
public List<String> get(Command command, List<Command> subCommands) {
23+
final List<String> strings = new ArrayList<>();
24+
25+
strings.add(mainColor + command.getName() + secondaryColor + "'s Commands");
26+
subCommands.forEach(command1 -> strings.add(mainColor + command1.getUsage() + secondaryColor + " - " + command1.getDescription()));
27+
28+
return translate(strings);
29+
}
30+
31+
private final List<String> translate(List<String> strings) {
32+
return strings.stream().map(string -> ChatColor.translateAlternateColorCodes('&', string)).collect(Collectors.toList());
33+
}
34+
35+
}

src/test/java/xyz/damt/example/Main.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.bukkit.inventory.ItemStack;
55
import org.bukkit.plugin.java.JavaPlugin;
66
import xyz.damt.command.CommandHandler;
7+
import xyz.damt.command.help.impl.DefaultHelpCommandService;
78
import xyz.damt.example.command.FlyCommand;
89
import xyz.damt.example.command.ItemStackCommand;
910
import xyz.damt.example.provider.ItemStackProvider;
@@ -22,7 +23,7 @@ public void onLoad() {
2223
@Override
2324
public void onEnable() {
2425
new CommandHandler(this).bind(ItemStack.class, new ItemStackProvider())
25-
.register(new FlyCommand(), new ItemStackCommand()).registerCommands();
26+
.register(new FlyCommand(), new ItemStackCommand()).helpService(new DefaultHelpCommandService("&c", "&7")).registerCommands();
2627
}
2728

2829
}

0 commit comments

Comments
 (0)