TotalFreedomMod/src/main/java/me/totalfreedom/totalfreedommod/command/Command_plugincontrol.java

152 lines
4.3 KiB
Java
Raw Normal View History

package me.totalfreedom.totalfreedommod.command;
2013-03-24 19:55:34 +00:00
import me.totalfreedom.totalfreedommod.rank.Rank;
import org.apache.commons.lang3.StringUtils;
2014-05-13 14:32:01 +00:00
import org.bukkit.ChatColor;
2013-03-24 19:55:34 +00:00
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
@CommandPermissions(level = Rank.TELNET_ADMIN, source = SourceType.BOTH)
2014-05-13 14:32:01 +00:00
@CommandParameters(description = "Manage plugins", usage = "/<command> <<enable | disable | reload> <pluginname>> | list>", aliases = "plc")
public class Command_plugincontrol extends FreedomCommand
2013-03-24 19:55:34 +00:00
{
2013-03-24 19:55:34 +00:00
@Override
public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
2013-03-24 19:55:34 +00:00
{
2014-05-13 14:32:01 +00:00
if (args.length == 0 || args.length > 2)
{
return false;
}
final PluginManager pm = server.getPluginManager();
2013-03-24 19:55:34 +00:00
if (args.length == 1)
{
if (args[0].equalsIgnoreCase("list"))
{
2014-05-13 14:32:01 +00:00
for (Plugin serverPlugin : pm.getPlugins())
{
final String version = serverPlugin.getDescription().getVersion();
msg(ChatColor.GRAY + "- " + (serverPlugin.isEnabled() ? ChatColor.GREEN : ChatColor.RED) + serverPlugin.getName()
2014-05-13 14:32:01 +00:00
+ ChatColor.GOLD + (version != null && !version.isEmpty() ? " v" + version : "") + " by "
+ StringUtils.join(serverPlugin.getDescription().getAuthors(), ", "));
}
return true;
2013-03-24 19:55:34 +00:00
}
2014-05-13 14:32:01 +00:00
return false;
2013-03-24 19:55:34 +00:00
}
2014-05-13 14:32:01 +00:00
if ("enable".equals(args[0]))
2013-03-24 19:55:34 +00:00
{
2014-05-13 14:32:01 +00:00
final Plugin target = getPlugin(args[1]);
if (target == null)
2013-03-24 19:55:34 +00:00
{
msg("Plugin not found!");
2014-05-13 14:32:01 +00:00
return true;
2013-03-24 19:55:34 +00:00
}
2014-05-13 14:32:01 +00:00
if (target.isEnabled())
2013-03-24 19:55:34 +00:00
{
msg("Plugin is already enabled.");
2014-05-13 14:32:01 +00:00
return true;
2013-03-24 19:55:34 +00:00
}
2014-05-13 14:32:01 +00:00
pm.enablePlugin(target);
2013-03-24 19:55:34 +00:00
2014-05-13 14:32:01 +00:00
if (!pm.isPluginEnabled(target))
{
msg("Error enabling plugin " + target.getName());
2014-05-13 14:32:01 +00:00
return true;
}
2013-03-24 19:55:34 +00:00
msg(target.getName() + " is now enabled.");
2014-05-13 14:32:01 +00:00
return true;
2013-03-24 19:55:34 +00:00
}
2014-05-13 14:32:01 +00:00
if ("disable".equals(args[0]))
2013-03-24 19:55:34 +00:00
{
2014-05-13 14:32:01 +00:00
final Plugin target = getPlugin(args[1]);
if (target == null)
2013-03-24 19:55:34 +00:00
{
msg("Plugin not found!");
2014-05-13 14:32:01 +00:00
return true;
2013-03-24 19:55:34 +00:00
}
2014-05-13 14:32:01 +00:00
if (!target.isEnabled())
2013-03-24 19:55:34 +00:00
{
msg("Plugin is already disabled.");
2014-05-13 14:32:01 +00:00
return true;
}
if (target.getName().equals(plugin.getName()))
{
msg("You cannot disable " + plugin.getName());
2014-05-13 14:32:01 +00:00
return true;
2013-03-24 19:55:34 +00:00
}
2014-05-13 14:32:01 +00:00
pm.disablePlugin(target);
2013-03-24 19:55:34 +00:00
2014-05-13 14:32:01 +00:00
if (pm.isPluginEnabled(target))
2013-03-24 19:55:34 +00:00
{
msg("Error disabling plugin " + target.getName());
2014-05-13 14:32:01 +00:00
return true;
2013-03-24 19:55:34 +00:00
}
msg(target.getName() + " is now disabled.");
2014-05-13 14:32:01 +00:00
return true;
}
if ("reload".equals(args[0]))
{
final Plugin target = getPlugin(args[1]);
if (target == null)
2013-03-24 19:55:34 +00:00
{
msg("Plugin not found!");
2013-03-24 19:55:34 +00:00
return true;
}
2014-05-13 14:32:01 +00:00
if (target.getName().equals(plugin.getName()))
2013-03-24 19:55:34 +00:00
{
msg("Use /tfm reload to reload instead.");
2014-05-13 14:32:01 +00:00
return true;
}
pm.disablePlugin(target);
pm.enablePlugin(target);
msg(target.getName() + " reloaded.");
2014-05-13 14:32:01 +00:00
return true;
}
return false;
}
public Plugin getPlugin(String name)
{
for (Plugin serverPlugin : server.getPluginManager().getPlugins())
{
if (serverPlugin.getName().equalsIgnoreCase(name))
{
return serverPlugin;
}
}
if (name.length() >= 3)
{
for (Plugin serverPlugin : server.getPluginManager().getPlugins())
{
if (serverPlugin.getName().toLowerCase().contains(name.toLowerCase()))
2013-03-24 19:55:34 +00:00
{
2014-05-13 14:32:01 +00:00
return serverPlugin;
2013-03-24 19:55:34 +00:00
}
}
}
2014-05-13 14:32:01 +00:00
return null;
2013-03-24 19:55:34 +00:00
}
}