/help now supports multiple permissions for a command in plugin.yml

Also minor cleanup
This commit is contained in:
snowleo 2011-08-30 03:15:10 +02:00
parent 5d4aa95fc3
commit 1cbdbc40a5

View file

@ -20,6 +20,9 @@ import java.util.logging.Level;
public class Commandhelp extends EssentialsCommand public class Commandhelp extends EssentialsCommand
{ {
private static final String DESCRIPTION = "description";
private static final String PERMISSION = "permission";
private static final String PERMISSIONS = "permissions";
public final Yaml yaml = new Yaml(new SafeConstructor()); public final Yaml yaml = new Yaml(new SafeConstructor());
public Commandhelp() public Commandhelp()
@ -28,7 +31,7 @@ public class Commandhelp extends EssentialsCommand
} }
@Override @Override
protected void run(Server server, User user, String commandLabel, String[] args) throws Exception protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
{ {
int page = 1; int page = 1;
String match = ""; String match = "";
@ -53,11 +56,14 @@ public class Commandhelp extends EssentialsCommand
} }
} }
List<String> lines = getHelpLines(user, match); final List<String> lines = getHelpLines(user, match);
if (lines.size() > 0) if (lines.isEmpty())
{ {
int start = (page - 1) * 9; throw new Exception(Util.i18n("noHelpFound"));
int pages = lines.size() / 9 + (lines.size() % 9 > 0 ? 1 : 0); }
final int start = (page - 1) * 9;
final 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++)
@ -65,22 +71,17 @@ public class Commandhelp extends EssentialsCommand
user.sendMessage(lines.get(i)); user.sendMessage(lines.get(i));
} }
} }
else
{
user.sendMessage(Util.i18n("noHelpFound"));
}
}
@Override @Override
protected void run(Server server, CommandSender sender, String commandLabel, String[] args) throws Exception protected void run(final Server server, final CommandSender sender, final String commandLabel, final 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(final User user, final String match) throws Exception
{ {
List<String> retval = new ArrayList<String>(); final 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())
{ {
@ -116,15 +117,16 @@ public class Commandhelp extends EssentialsCommand
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, Object>> cmds = (HashMap<String, HashMap<String, Object>>)desc.getCommands();
pluginName = p.getDescription().getName().toLowerCase(); pluginName = p.getDescription().getName().toLowerCase();
for (Entry<String, HashMap<String, String>> k : cmds.entrySet()) for (Entry<String, HashMap<String, Object>> k : cmds.entrySet())
{ {
try try
{ {
if ((!match.equalsIgnoreCase("")) if ((!match.equalsIgnoreCase(""))
&& (!k.getKey().toLowerCase().contains(match)) && (!k.getKey().toLowerCase().contains(match))
&& (!k.getValue().get("description").toLowerCase().contains(match)) && (!(k.getValue().get(DESCRIPTION) instanceof String
&& ((String)k.getValue().get(DESCRIPTION)).toLowerCase().contains(match)))
&& (!pluginName.contains(match))) && (!pluginName.contains(match)))
{ {
continue; continue;
@ -135,37 +137,69 @@ public class Commandhelp extends EssentialsCommand
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())
{ {
final HashMap<String, String> value = k.getValue(); final HashMap<String, Object> value = k.getValue();
if (value.containsKey("permission") && value.get("permission") != null && !(value.get("permission").equals(""))) if (value.containsKey(PERMISSION) && value.get(PERMISSION) instanceof String && !(value.get(PERMISSION).equals("")))
{ {
if (user.isAuthorized(value.get("permission"))) if (user.isAuthorized((String)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(PERMISSION) && value.get(PERMISSION) instanceof List && !((List<Object>)value.get(PERMISSION)).isEmpty())
{ {
if (user.isAuthorized(value.get("permissions"))) boolean enabled = false;
for (Object o : (List<Object>)value.get(PERMISSION))
{ {
retval.add("§c" + k.getKey() + "§7: " + value.get("description")); if (o instanceof String && user.isAuthorized((String)o))
{
enabled = true;
break;
}
}
if (enabled)
{
retval.add("§c" + k.getKey() + "§7: " + value.get(DESCRIPTION));
}
}
else if (value.containsKey(PERMISSIONS) && value.get(PERMISSIONS) instanceof String && !(value.get(PERMISSIONS).equals("")))
{
if (user.isAuthorized((String)value.get(PERMISSIONS)))
{
retval.add("§c" + k.getKey() + "§7: " + value.get(DESCRIPTION));
}
}
else if (value.containsKey(PERMISSIONS) && value.get(PERMISSIONS) instanceof List && !((List<Object>)value.get(PERMISSIONS)).isEmpty())
{
boolean enabled = false;
for (Object o : (List<Object>)value.get(PERMISSIONS))
{
if (o instanceof String && user.isAuthorized((String)o))
{
enabled = true;
break;
}
}
if (enabled)
{
retval.add("§c" + k.getKey() + "§7: " + value.get(DESCRIPTION));
} }
} }
else if (user.isAuthorized("essentials.help." + pluginName)) else if (user.isAuthorized("essentials.help." + pluginName))
{ {
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));
} }
} }
} }