summaryrefslogtreewikicommitdiff
path: root/src/main/java/net/tylermurphy/hideAndSeek/command/Help.java
blob: 41db5a92f6716573b94eb50db8c2fb84ca9726a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package net.tylermurphy.hideAndSeek.command;

import net.tylermurphy.hideAndSeek.Main;
import net.tylermurphy.hideAndSeek.command.util.ICommand;
import net.tylermurphy.hideAndSeek.util.Pair;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static net.tylermurphy.hideAndSeek.configuration.Config.errorPrefix;
import static net.tylermurphy.hideAndSeek.configuration.Localization.message;

public class Help implements ICommand {

	public void execute(Player sender, String[] args) {
		final int pageSize = 4;
		List<Pair<String, ICommand>> commands = Main.getInstance().getCommandGroup().getCommands();
		int pages = (commands.size() - 1) / pageSize + 1;
		int page;
		try {
			if(args.length < 1) {
				page = 1;
			} else {
				page = Integer.parseInt(args[0]);
				if (page < 1) {
					throw new IllegalArgumentException("Inavlid Input");
				}
			}
		} catch (Exception e) {
			sender.sendMessage(errorPrefix + message("WORLDBORDER_INVALID_INPUT").addAmount(args[0]));
			return;
		}
		String spacer = ChatColor.GRAY + "?" + ChatColor.WHITE;
		StringBuilder message = new StringBuilder();
		message.append(String.format("%s================ %sHelp: Page (%s/%s) %s================",
				ChatColor.AQUA, ChatColor.WHITE, page, pages, ChatColor.AQUA));
		int lines = 0;
		for(Pair<String, ICommand> pair : commands.stream().skip((long) (page - 1) * pageSize).limit(pageSize).collect(Collectors.toList())) {
			ICommand command = pair.getRight();
			String label = pair.getLeft();
			String start = label.substring(0, label.indexOf(" "));
			String invoke = label.substring(label.indexOf(" ")+1);
			message.append(String.format("\n%s %s/%s %s%s %s%s\n%s  %s%s%s",
					spacer,
					ChatColor.AQUA,
					start,
					ChatColor.WHITE,
					invoke,
					ChatColor.BLUE,
					command.getUsage(),
					spacer,
					ChatColor.GRAY,
					ChatColor.ITALIC,
					command.getDescription()
			));
			lines += 2;
		}
		if(lines / 2 < pageSize) {
			for(int i = 0; i < pageSize * 2 - lines; i++) {
				message.append("\n").append(spacer);
			}
		}
		message.append("\n").append(ChatColor.AQUA).append("===============================================");
		sender.sendMessage(message.toString());
	}

	public String getLabel() {
		return "help";
	}

	public String getUsage() {
		return "<*page>";
	}

	public String getDescription() {
		return "Get the commands for the plugin";
	}

	public List<String> autoComplete(@NotNull String parameter, @NotNull String typed) {
		return Collections.singletonList(parameter);
	}

}