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

194 lines
5.6 KiB
Java
Raw Normal View History

package me.totalfreedom.totalfreedommod.command;
2013-03-24 19:55:34 +00:00
2019-01-29 04:57:41 +00:00
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import me.totalfreedom.totalfreedommod.rank.Rank;
import org.apache.commons.lang.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.MOD, source = SourceType.BOTH)
@CommandParameters(description = "Enable, disable, or reload a specified plugin, as well as list all plugins on the server.", usage = "/<command> <<enable | disable | reload> <pluginname>> | list>", aliases = "plc")
public class Command_plugincontrol extends FreedomCommand
2013-03-24 19:55:34 +00:00
{
2020-05-29 10:14:21 +00:00
private final List<String> UNTOUCHABLE_PLUGINS = Arrays.asList(plugin.getName(), "BukkitTelnet");
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
2020-01-02 23:39:30 +00:00
if (args[0].equals("enable"))
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
2020-01-02 23:39:30 +00:00
if (args[0].equals("disable"))
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;
}
2020-05-29 10:14:21 +00:00
if (UNTOUCHABLE_PLUGINS.contains(target.getName()))
2014-05-13 14:32:01 +00:00
{
2020-05-29 10:14:21 +00:00
msg("You cannot disable " + target.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;
}
2020-01-02 23:39:30 +00:00
if (args[0].equals("reload"))
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!");
2013-03-24 19:55:34 +00:00
return true;
}
2014-05-13 14:32:01 +00:00
2020-05-29 10:14:21 +00:00
if (UNTOUCHABLE_PLUGINS.contains(target.getName()))
2013-03-24 19:55:34 +00:00
{
2020-05-29 10:14:21 +00:00
msg("You cannot reload " + target.getName());
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;
}
2019-01-29 04:57:41 +00:00
@Override
public List<String> getTabCompleteOptions(CommandSender sender, Command command, String alias, String[] args)
{
if (!plugin.sl.isStaff(sender))
2019-01-29 04:57:41 +00:00
{
return Collections.emptyList();
}
if (args.length == 1)
{
return Arrays.asList("enable", "disable", "reload", "list");
}
else if (args.length == 2)
{
if (!args[0].equals("list"))
{
return getAllPluginNames();
}
}
return Collections.emptyList();
}
public List<String> getAllPluginNames()
{
List<String> names = new ArrayList<>();
for (Plugin plugin : server.getPluginManager().getPlugins())
{
2020-05-29 10:14:21 +00:00
if (!UNTOUCHABLE_PLUGINS.contains(plugin.getName()))
{
names.add(plugin.getName());
}
2019-01-29 04:57:41 +00:00
}
names.remove(plugin.getName());
return names;
}
2014-05-13 14:32:01 +00:00
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
}
}