1
0
mirror of https://github.com/essentials/Essentials.git synced 2025-08-16 11:37:30 +02:00

minor cleanup

This commit is contained in:
ementalo
2011-07-22 23:33:33 +01:00
parent 172d6ee8a7
commit e1425f5e25

View File

@@ -1,185 +1,183 @@
package com.earth2me.essentials.commands; package com.earth2me.essentials.commands;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginDescriptionFile;
import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor; import org.yaml.snakeyaml.constructor.SafeConstructor;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import com.earth2me.essentials.Util; import com.earth2me.essentials.Util;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.logging.Level; import java.util.logging.Level;
public class Commandhelp extends EssentialsCommand public class Commandhelp extends EssentialsCommand
{ {
public final Yaml yaml = new Yaml(new SafeConstructor()); public final Yaml yaml = new Yaml(new SafeConstructor());
public Commandhelp() public Commandhelp()
{ {
super("help"); super("help");
} }
@Override @Override
protected void run(Server server, User user, String commandLabel, String[] args) throws Exception protected void run(Server server, User user, String commandLabel, String[] args) throws Exception
{ {
int page = 1; int page = 1;
String match = ""; String match = "";
try try
{ {
if (args.length > 0) if (args.length > 0)
{ {
match = args[0].toLowerCase(); match = args[0].toLowerCase();
page = Integer.parseInt(args[args.length - 1]); page = Integer.parseInt(args[args.length - 1]);
if (args.length == 1) if (args.length == 1)
{ {
match = ""; match = "";
} }
} }
}
} catch (Exception ex)
catch (Exception ex) {
{ if (args.length == 1)
if (args.length == 1) {
{ match = args[0].toLowerCase();
match = args[0].toLowerCase(); }
} }
}
List<String> lines = getHelpLines(user, match);
List<String> lines = getHelpLines(user, match); if (lines.size() > 0)
if (lines.size() > 0) {
{ int start = (page - 1) * 9;
int start = (page - 1) * 9; int pages = lines.size() / 9 + (lines.size() % 9 > 0 ? 1 : 0);
int pages = lines.size() / 9 + (lines.size() % 9 > 0 ? 1 : 0);
user.sendMessage(Util.format("helpPages", page, pages));
user.sendMessage(Util.format("helpPages", page, pages)); for (int i = start; i < lines.size() && i < start + 9; i++)
for (int i = start; i < lines.size() && i < start + 9; i++) {
{ user.sendMessage(lines.get(i));
user.sendMessage(lines.get(i)); }
} }
} else
else {
{ user.sendMessage(Util.i18n("noHelpFound"));
user.sendMessage(Util.i18n("noHelpFound")); }
} }
}
@Override
@Override protected void run(Server server, CommandSender sender, String commandLabel, String[] args) throws Exception
protected void run(Server server, CommandSender sender, String commandLabel, String[] args) throws Exception {
{ sender.sendMessage(Util.i18n("helpConsole"));
sender.sendMessage(Util.i18n("helpConsole")); }
}
@SuppressWarnings("CallToThreadDumpStack")
@SuppressWarnings("CallToThreadDumpStack") private List<String> getHelpLines(User user, String match) throws Exception
private List<String> getHelpLines(User user, String match) throws Exception {
{ List<String> retval = new ArrayList<String>();
List<String> retval = new ArrayList<String>(); File helpFile = new File(ess.getDataFolder(), "help_" + Util.sanitizeFileName(user.getName()) + ".txt");
File helpFile = new File(ess.getDataFolder(), "help_" + Util.sanitizeFileName(user.getName()) + ".txt"); if (!helpFile.exists())
if (!helpFile.exists()) {
{ helpFile = new File(ess.getDataFolder(), "help_" + Util.sanitizeFileName(user.getGroup()) + ".txt");
helpFile = new File(ess.getDataFolder(), "help_" + Util.sanitizeFileName(user.getGroup()) + ".txt"); }
} if (!helpFile.exists())
if (!helpFile.exists()) {
{ helpFile = new File(ess.getDataFolder(), "help.txt");
helpFile = new File(ess.getDataFolder(), "help.txt"); }
} if (helpFile.exists())
if (helpFile.exists()) {
{ final BufferedReader bufferedReader = new BufferedReader(new FileReader(helpFile));
final BufferedReader bufferedReader = new BufferedReader(new FileReader(helpFile)); try
try {
{
while (bufferedReader.ready())
while (bufferedReader.ready()) {
{ final String line = bufferedReader.readLine();
final String line = bufferedReader.readLine(); retval.add(line.replace('&', '§'));
retval.add(line.replace('&', '§')); }
} }
} finally
finally {
{ bufferedReader.close();
bufferedReader.close(); }
} return retval;
return retval; }
}
boolean reported = false;
boolean reported = false; String pluginName = "";
String pluginName = ""; for (Plugin p : ess.getServer().getPluginManager().getPlugins())
for (Plugin p : ess.getServer().getPluginManager().getPlugins()) {
{ try
try {
{ final PluginDescriptionFile desc = p.getDescription();
final PluginDescriptionFile desc = p.getDescription(); final HashMap<String, HashMap<String, String>> cmds = (HashMap<String, HashMap<String, String>>)desc.getCommands();
final HashMap<String, HashMap<String, String>> cmds = (HashMap<String, HashMap<String, String>>)desc.getCommands(); for (Entry<String, HashMap<String, String>> k : cmds.entrySet())
for (Entry<String, HashMap<String, String>> k : cmds.entrySet()) {
{ if ((!match.equalsIgnoreCase("")) && (!k.getKey().toLowerCase().contains(match))
if ((!match.equalsIgnoreCase("")) && (!k.getKey().toLowerCase().contains(match)) && (!k.getValue().get("description").toLowerCase().contains(match)))
&& (!k.getValue().get("description").toLowerCase().contains(match))) {
{ continue;
continue; }
}
if (p.getDescription().getName().toLowerCase().contains("essentials"))
if (p.getDescription().getName().toLowerCase().contains("essentials")) {
{ final String node = "essentials." + k.getKey();
final String node = "essentials." + k.getKey(); if (!ess.getSettings().isCommandDisabled(k.getKey()) && user.isAuthorized(node))
if (!ess.getSettings().isCommandDisabled(k.getKey()) && user.isAuthorized(node)) {
{ retval.add("§c" + k.getKey() + "§7: " + k.getValue().get("description"));
retval.add("§c" + k.getKey() + "§7: " + k.getValue().get("description")); }
} }
} else
else {
{ if (ess.getSettings().showNonEssCommandsInHelp())
if (ess.getSettings().showNonEssCommandsInHelp()) {
{ pluginName = p.getDescription().getName();
pluginName = p.getDescription().getName(); final HashMap<String, String> value = k.getValue();
final HashMap<String, String> value = k.getValue(); if (value.containsKey("permission") && value.get("permission") != null && !(value.get("permission").equals("")))
if (value.containsKey("permission") && value.get("permission") != null && !(value.get("permission").equals(""))) {
{ if (user.isAuthorized(value.get("permission")))
if (user.isAuthorized(value.get("permission"))) {
{ retval.add("§c" + k.getKey() + "§7: " + value.get("description"));
retval.add("§c" + k.getKey() + "§7: " + value.get("description")); }
} }
} else if (value.containsKey("permissions") && value.get("permissions") != null && !(value.get("permissions").equals("")))
else if (value.containsKey("permissions") && value.get("permissions") != null && !(value.get("permissions").equals(""))) {
{ if (user.isAuthorized(value.get("permissions")))
if (user.isAuthorized(value.get("permissions"))) {
{ retval.add("§c" + k.getKey() + "§7: " + value.get("description"));
retval.add("§c" + k.getKey() + "§7: " + value.get("description")); }
} }
} else
else {
{ if (!ess.getSettings().hidePermissionlessHelp())
if (!ess.getSettings().hidePermissionlessHelp()) {
{ retval.add("§c" + k.getKey() + "§7: " + value.get("description"));
retval.add("§c" + k.getKey() + "§7: " + value.get("description")); }
} }
} }
}
}
} }
} }
} catch (NullPointerException ex)
catch (NullPointerException ex) {
{ continue;
continue; }
} catch (Exception ex)
catch (Exception ex) {
{ if (!reported)
if (!reported) {
{ logger.log(Level.WARNING, Util.format("commandHelpFailedForPlugin", pluginName), ex);
// }
logger.log(Level.WARNING, Util.format("commandHelpFailedForPlugin", pluginName), ex); reported = true;
} continue;
reported = true; }
continue; }
} return retval;
} }
return retval; }
}
}