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

144 lines
5.4 KiB
Java
Raw Normal View History

package me.totalfreedom.totalfreedommod.command;
2013-03-24 19:55:34 +00:00
import java.util.*;
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;
2020-09-28 03:17:01 +00:00
@CommandPermissions(level = Rank.ADMIN, 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
{
private final List<String> UNTOUCHABLE_PLUGINS = Arrays.asList(plugin.getName());
2020-05-29 10:14:21 +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
final PluginManager pm = server.getPluginManager();
2013-03-24 19:55:34 +00:00
/* This is the way it is because there was too much "if the arguments aren't enough then return false" in the
* original code in addition to the stupid amount of "if something isn't right then do some boilerplate stuff
* then return true". Codacy complained, so I aggressively optimized this to keep it quiet. */
switch (args.length)
2013-03-24 19:55:34 +00:00
{
case 1 ->
2013-03-24 19:55:34 +00:00
{
if (args[0].equalsIgnoreCase("list"))
2014-05-13 14:32:01 +00:00
{
Arrays.stream(pm.getPlugins()).forEach(pl ->
{
final String version = pl.getDescription().getVersion();
msg(ChatColor.GRAY + "- " + (pl.isEnabled() ? ChatColor.GREEN : ChatColor.RED) + pl.getName()
+ ChatColor.GOLD + (!version.isEmpty() ? " v" + version : "") + " by "
+ StringUtils.join(pl.getDescription().getAuthors(), ", "));
});
return true;
2014-05-13 14:32:01 +00:00
}
2013-03-24 19:55:34 +00:00
}
case 2 ->
2013-03-24 19:55:34 +00:00
{
Plugin pl = pm.getPlugin(args[1]);
2013-03-24 19:55:34 +00:00
if (pl != null)
{
switch (args[0].toLowerCase())
{
case "enable" ->
{
if (pl.isEnabled())
{
msg(pl.getName() + " is already enabled.");
return true;
}
pm.enablePlugin(pl);
if (pl.isEnabled())
{
msg(pl.getName() + " is now enabled.");
}
else
{
msg("An error occurred whilst attempting to enable " + pl.getName() + ".");
}
return true;
}
case "disable" ->
{
if (!pl.isEnabled())
{
msg(pl.getName() + " is already disabled.");
return true;
}
else if (UNTOUCHABLE_PLUGINS.contains(pl.getName()))
{
msg(pl.getName() + " can't be disabled.");
return true;
}
pm.disablePlugin(pl);
msg(pl.getName() + " is now disabled.");
return true;
}
case "reload" ->
{
if (UNTOUCHABLE_PLUGINS.contains(pl.getName()))
{
msg(pl.getName() + " can't be reloaded.");
return true;
}
pm.disablePlugin(pl);
pm.enablePlugin(pl);
msg(pl.getName() + " has been reloaded.");
return true;
}
default ->
{
// Do nothing. This is here to please Codacy.
}
}
}
else
{
msg("Plugin not found!");
return true;
}
2013-03-24 19:55:34 +00:00
}
default ->
2013-03-24 19:55:34 +00:00
{
// Ditto
2014-05-13 14:32:01 +00:00
}
}
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.al.isAdmin(sender))
2019-01-29 04:57:41 +00:00
{
return Collections.emptyList();
}
if (args.length == 1)
{
return Arrays.asList("enable", "disable", "reload", "list");
}
2022-11-05 03:30:28 +00:00
else if (args.length == 2 && !args[0].equalsIgnoreCase("list"))
2019-01-29 04:57:41 +00:00
{
return Arrays.stream(server.getPluginManager().getPlugins()).map(Plugin::getName)
.filter(pl -> !UNTOUCHABLE_PLUGINS.contains(pl)).toList();
2019-01-29 04:57:41 +00:00
}
return Collections.emptyList();
}
}