summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/commands/Commandhelp.java
blob: c992eda19d1f662d5bcf01a85e600bda936b042d (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.earth2me.essentials.commands;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;
import com.earth2me.essentials.User;
import com.earth2me.essentials.Util;


public class Commandhelp extends EssentialsCommand
{
	public final Yaml yaml = new Yaml(new SafeConstructor());

	public Commandhelp()
	{
		super("help");
	}

	@Override
	protected void run(Server server, User user, String commandLabel, String[] args) throws Exception
	{
		int page;
		try
		{
			page = args.length > 0 ? Integer.parseInt(args[0]) : 1;
		}
		catch (Exception ex)
		{
			page = 1;
		}

		List<String> lines = getHelpLines(user);
		int start = (page - 1) * 9;
		int pages = lines.size() / 9 + (lines.size() % 9 > 0 ? 1 : 0);

		user.sendMessage("Page §c" + page + "§f of §c" + pages + "§f:");
		for (int i = start; i < lines.size() && i < start + 9; i++)
		{
			user.sendMessage(lines.get(i));
		}
	}

	@Override
	protected void run(Server server, CommandSender sender, String commandLabel, String[] args) throws Exception
	{
		sender.sendMessage("To view help from the console, type \"?\".");
	}

	@SuppressWarnings("CallToThreadDumpStack")
	private List<String> getHelpLines(User user) throws Exception
	{
		List<String> retval = new ArrayList<String>();
		File helpFile = new File(ess.getDataFolder(), "help_"+Util.sanitizeFileName(user.getName()) +".txt");
		if (!helpFile.exists())
		{
			helpFile = new File(ess.getDataFolder(), "help_"+Util.sanitizeFileName(user.getGroup()) +".txt");
		}
		if (!helpFile.exists())
		{
			helpFile = new File(ess.getDataFolder(), "help.txt");
		}
		if (helpFile.exists())
		{
			BufferedReader rx = new BufferedReader(new FileReader(helpFile));
			for (String l = null; rx.ready() && (l = rx.readLine()) != null;)
			{
				retval.add(l.replace('&', '§'));
			}
			return retval;
		}

		boolean reported = false;
		for (Plugin p : ess.getServer().getPluginManager().getPlugins())
		{
			try
			{
				PluginDescriptionFile desc = p.getDescription();
				HashMap<String, HashMap<String, String>> cmds = (HashMap<String, HashMap<String, String>>)desc.getCommands();
				for (String k : cmds.keySet())
				{
					if (p.getDescription().getName().toLowerCase().contains("essentials"))
					{
						String node = "essentials." + k;
						if (!ess.getSettings().isCommandDisabled(k) && user.isAuthorized(node))
						{
							HashMap<String, String> v = cmds.get(k);
							retval.add("§c" + k + "§7: " + v.get("description"));
						}
					}
					else
					{
						if (ess.getSettings().showNonEssCommandsInHelp())
						{
							HashMap<String, String> v = cmds.get(k);
							if (v.containsKey("permission") && v.get("permission") != null && !(v.get("permission").equals("")))
							{
								if (user.isAuthorized(v.get("permission")))
								{
									retval.add("§c" + k + "§7: " + v.get("description"));
								}
							}
							else
							{
								retval.add("§c" + k + "§7: " + v.get("description"));
							}
						}

					}
				}
			}
			catch (NullPointerException ex)
			{
				continue;
			}
			catch (Exception ex)
			{
				if (!reported)
				{
					ex.printStackTrace();
				}
				reported = true;
				continue;
			}
		}
		return retval;
	}
}