TFM-4.3-Reloaded/src/main/java/me/StevenLawson/TotalFreedomMod/commands/Command_plugincontrol.java

146 lines
4.3 KiB
Java
Raw Normal View History

2022-03-20 12:35:43 +00:00
package me.StevenLawson.TotalFreedomMod.commands;
2013-03-24 19:55:34 +00:00
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.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
@CommandPermissions(level = AdminLevel.SENIOR, source = SourceType.BOTH)
@CommandParameters(description = "Manage plugins", usage = "/<command> <<enable | disable | reload> <pluginname>> | list>", aliases = "plc")
2022-03-20 12:35:43 +00:00
public class Command_plugincontrol extends FreedomCommand {
2013-03-24 19:55:34 +00:00
@Override
2022-03-20 12:35:43 +00:00
public boolean run(CommandSender sender, org.bukkit.entity.Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) {
if (args.length == 0 || args.length > 2) {
2014-05-13 14:32:01 +00:00
return false;
}
final PluginManager pm = server.getPluginManager();
2013-03-24 19:55:34 +00:00
2022-03-20 12:35:43 +00:00
if (args.length == 1) {
2013-03-24 19:55:34 +00:00
if (args[0].equalsIgnoreCase("list"))
{
2014-05-13 14:32:01 +00:00
for (Plugin serverPlugin : pm.getPlugins())
{
final String version = serverPlugin.getDescription().getVersion();
playerMsg(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
{
playerMsg("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
{
playerMsg("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))
{
playerMsg("Error enabling plugin " + target.getName());
2014-05-13 14:32:01 +00:00
return true;
}
2013-03-24 19:55:34 +00:00
playerMsg(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
{
playerMsg("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
{
playerMsg("Plugin is already disabled.");
2014-05-13 14:32:01 +00:00
return true;
}
if (target.getName().equals(plugin.getName()))
{
playerMsg("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
{
playerMsg("Error disabling plugin " + target.getName());
2014-05-13 14:32:01 +00:00
return true;
2013-03-24 19:55:34 +00:00
}
playerMsg(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
{
playerMsg("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
{
playerMsg("Use /tfm reload to reload instead.");
2014-05-13 14:32:01 +00:00
return true;
}
pm.disablePlugin(target);
pm.enablePlugin(target);
playerMsg(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
}
}