Revamp /plugincontrol. Fixes #159

This commit is contained in:
unknown 2014-05-13 16:32:01 +02:00
parent bd9d6114ba
commit 7b8ba0bdf5
2 changed files with 116 additions and 95 deletions

View File

@ -1,3 +1,3 @@
#Build Number for ANT. Do not edit! #Build Number for ANT. Do not edit!
#Sun May 11 19:02:31 CEST 2014 #Tue May 13 16:30:53 CEST 2014
build.number=832 build.number=837

View File

@ -1,6 +1,7 @@
package me.StevenLawson.TotalFreedomMod.Commands; package me.StevenLawson.TotalFreedomMod.Commands;
import net.minecraft.util.org.apache.commons.lang3.StringUtils; import net.minecraft.util.org.apache.commons.lang3.StringUtils;
import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -8,122 +9,142 @@ import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
@CommandPermissions(level = AdminLevel.SENIOR, source = SourceType.BOTH) @CommandPermissions(level = AdminLevel.SENIOR, source = SourceType.BOTH)
@CommandParameters(description = "Enable / disable plugins.", usage = "/<command> < <enable | disable> <pluginname> | list >", aliases = "plc") @CommandParameters(description = "Manage plugins", usage = "/<command> <<enable | disable | reload> <pluginname>> | list>", aliases = "plc")
public class Command_plugincontrol extends TFM_Command public class Command_plugincontrol extends TFM_Command
{ {
private enum CommandMode
{
ENABLE, DISABLE, LIST, RELOAD
}
@Override @Override
public boolean run(CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) public boolean run(CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
{ {
CommandMode commandMode = null; if (args.length == 0 || args.length > 2)
{
return false;
}
final PluginManager pm = server.getPluginManager();
if (args.length == 1) if (args.length == 1)
{ {
if (args[0].equalsIgnoreCase("list")) if (args[0].equalsIgnoreCase("list"))
{ {
commandMode = CommandMode.LIST; for (Plugin serverPlugin : pm.getPlugins())
}
else if (args[0].equalsIgnoreCase("reload"))
{ {
commandMode = CommandMode.RELOAD; final String version = serverPlugin.getDescription().getVersion();
} playerMsg(ChatColor.GRAY + "- " + (serverPlugin.isEnabled() ? ChatColor.GREEN : ChatColor.RED) + serverPlugin.getName()
} + ChatColor.GOLD + (version != null && !version.isEmpty() ? " v" + version : "") + " by "
else if (args.length >= 2) + StringUtils.join(serverPlugin.getDescription().getAuthors(), ", "));
{ }
if (args[0].equalsIgnoreCase("enable"))
{ return true;
commandMode = CommandMode.ENABLE;
}
else if (args[0].equalsIgnoreCase("disable"))
{
commandMode = CommandMode.DISABLE;
}
} }
if (commandMode == null)
{
return false; return false;
} }
PluginManager pluginManager = plugin.getServer().getPluginManager(); if ("enable".equals(args[0]))
if (commandMode == CommandMode.LIST)
{ {
playerMsg("Plugins: " + StringUtils.join(pluginManager.getPlugins(), ", ")); final Plugin target = getPlugin(args[1]);
} if (target == null)
else if (commandMode == CommandMode.RELOAD)
{ {
playerMsg("Disabling all plugins."); playerMsg("Plugin not found!");
for (Plugin targetPlugin : pluginManager.getPlugins())
{
if (!targetPlugin.getName().toLowerCase().startsWith("totalfreedommod"))
{
pluginManager.disablePlugin(targetPlugin);
}
}
playerMsg("Enabling all plugins.");
for (Plugin targetPlugin : pluginManager.getPlugins())
{
if (!targetPlugin.getName().toLowerCase().startsWith("totalfreedommod"))
{
pluginManager.enablePlugin(targetPlugin);
}
}
}
else
{
final String searchPluginName = args[1].toLowerCase().trim();
Plugin targetPlugin = null;
for (Plugin serverPlugin : pluginManager.getPlugins())
{
if (searchPluginName.equalsIgnoreCase(serverPlugin.getName().toLowerCase().trim()))
{
targetPlugin = serverPlugin;
break;
}
}
if (targetPlugin == null)
{
playerMsg("Plugin \"" + searchPluginName + "\" is not installed.");
return true; return true;
} }
else
if (target.isEnabled())
{ {
if (commandMode == CommandMode.ENABLE) playerMsg("Plugin is already enabled.");
{ return true;
pluginManager.enablePlugin(targetPlugin);
if (targetPlugin.isEnabled())
{
playerMsg("Plugin \"" + targetPlugin.getName() + "\" enabled.");
} }
else
pm.enablePlugin(target);
if (!pm.isPluginEnabled(target))
{ {
playerMsg("Error enabling plugin \"" + targetPlugin.getName() + "\"."); playerMsg("Error enabling plugin " + target.getName());
return true;
}
playerMsg(target.getName() + " is now enabled.");
return true;
}
if ("disable".equals(args[0]))
{
final Plugin target = getPlugin(args[1]);
if (target == null)
{
playerMsg("Plugin not found!");
return true;
}
if (!target.isEnabled())
{
playerMsg("Plugin is already disabled.");
return true;
}
if (target.getName().equals(plugin.getName()))
{
playerMsg("You cannot disable " + plugin.getName());
return true;
}
pm.disablePlugin(target);
if (pm.isPluginEnabled(target))
{
playerMsg("Error disabling plugin " + target.getName());
return true;
}
playerMsg(target.getName() + " is now disabled.");
return true;
}
if ("reload".equals(args[0]))
{
final Plugin target = getPlugin(args[1]);
if (target == null)
{
playerMsg("Plugin not found!");
return true;
}
if (target.getName().equals(plugin.getName()))
{
playerMsg("Use /tfm reload to reload instead.");
return true;
}
pm.disablePlugin(target);
pm.enablePlugin(target);
playerMsg(target.getName() + " reloaded.");
return true;
}
return false;
}
public Plugin getPlugin(String name)
{
for (Plugin serverPlugin : server.getPluginManager().getPlugins())
{
if (serverPlugin.getName().equalsIgnoreCase(name))
{
return serverPlugin;
} }
} }
else
if (name.length() >= 3)
{ {
pluginManager.disablePlugin(targetPlugin); for (Plugin serverPlugin : server.getPluginManager().getPlugins())
if (!targetPlugin.isEnabled())
{ {
playerMsg("Plugin \"" + targetPlugin.getName() + "\" disabled."); if (serverPlugin.getName().toLowerCase().contains(name.toLowerCase()))
}
else
{ {
playerMsg("Error disabling plugin \"" + targetPlugin.getName() + "\"."); return serverPlugin;
}
} }
} }
} }
return true; return null;
} }
} }