152 lines
6.5 KiB
Java
152 lines
6.5 KiB
Java
package me.totalfreedom.totalfreedommod.command;
|
|
|
|
import me.totalfreedom.totalfreedommod.command.handling.CommandParameters;
|
|
import me.totalfreedom.totalfreedommod.command.handling.CommandPermissions;
|
|
import me.totalfreedom.totalfreedommod.command.handling.FreedomCommand;
|
|
import me.totalfreedom.totalfreedommod.command.handling.SourceType;
|
|
import net.kyori.adventure.text.format.NamedTextColor;
|
|
import net.kyori.adventure.text.minimessage.tag.Tag;
|
|
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
|
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
|
|
import org.apache.commons.lang.StringUtils;
|
|
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;
|
|
|
|
import java.util.Arrays;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
|
|
@CommandPermissions(permission = "plugincontrol", 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
|
|
{
|
|
private final List<String> UNTOUCHABLE_PLUGINS = Arrays.asList(plugin.getName());
|
|
|
|
@Override
|
|
public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
|
|
{
|
|
final PluginManager pm = server.getPluginManager();
|
|
|
|
/* 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)
|
|
{
|
|
case 1 ->
|
|
{
|
|
if (args[0].equalsIgnoreCase("list"))
|
|
{
|
|
Arrays.stream(pm.getPlugins()).forEach(pl ->
|
|
{
|
|
final String version = pl.getDescription().getVersion();
|
|
msgNew("<gray>- <statuscolor><name><version> by <authors>",
|
|
TagResolver.resolver("statuscolor", Tag.styling(pl.isEnabled() ? NamedTextColor.GREEN : NamedTextColor.RED)),
|
|
Placeholder.unparsed("name", pl.getName()),
|
|
Placeholder.unparsed("version", version.isEmpty() ? " v" + version : ""),
|
|
Placeholder.unparsed("authors", StringUtils.join(pl.getDescription().getAuthors(), ", ")));
|
|
});
|
|
|
|
return true;
|
|
}
|
|
}
|
|
case 2 ->
|
|
{
|
|
Plugin pl = pm.getPlugin(args[1]);
|
|
|
|
if (pl != null)
|
|
{
|
|
switch (args[0].toLowerCase())
|
|
{
|
|
case "enable" ->
|
|
{
|
|
if (pl.isEnabled())
|
|
{
|
|
msgNew("<red><plugin> is already enabled.", Placeholder.unparsed("plugin", pl.getName()));
|
|
return true;
|
|
}
|
|
|
|
pm.enablePlugin(pl);
|
|
|
|
if (pl.isEnabled())
|
|
{
|
|
msgNew("<green><plugin> is now enabled.", Placeholder.unparsed("plugin", pl.getName()));
|
|
} else
|
|
{
|
|
msgNew("<red>An error occurred whilst attempting to enable <plugin>", Placeholder.unparsed("plugin", pl.getName()));
|
|
}
|
|
return true;
|
|
}
|
|
case "disable" ->
|
|
{
|
|
if (!pl.isEnabled())
|
|
{
|
|
msgNew("<red><plugin> is already disabled.", Placeholder.unparsed("plugin", pl.getName()));
|
|
return true;
|
|
} else if (UNTOUCHABLE_PLUGINS.contains(pl.getName()))
|
|
{
|
|
msgNew("<red><plugin> can't be disabled.", Placeholder.unparsed("plugin", pl.getName()));
|
|
return true;
|
|
}
|
|
|
|
pm.disablePlugin(pl);
|
|
|
|
msgNew("<green><plugin> is now disabled.", Placeholder.unparsed("plugin", pl.getName()));
|
|
return true;
|
|
}
|
|
case "reload" ->
|
|
{
|
|
if (UNTOUCHABLE_PLUGINS.contains(pl.getName()))
|
|
{
|
|
msgNew("<red><plugin> can't be reloaded.", Placeholder.unparsed("plugin", pl.getName()));
|
|
return true;
|
|
}
|
|
|
|
pm.disablePlugin(pl);
|
|
pm.enablePlugin(pl);
|
|
|
|
msgNew("<green><plugin> has been reloaded.", Placeholder.unparsed("plugin", pl.getName()));
|
|
;
|
|
return true;
|
|
}
|
|
default ->
|
|
{
|
|
// Do nothing. This is here to please Codacy.
|
|
}
|
|
}
|
|
} else
|
|
{
|
|
msgNew("<red>Plugin not found!");
|
|
return true;
|
|
}
|
|
}
|
|
default ->
|
|
{
|
|
// Ditto
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public List<String> getTabCompleteOptions(CommandSender sender, Command command, String alias, String[] args)
|
|
{
|
|
if (!plugin.al.isAdmin(sender))
|
|
{
|
|
return Collections.emptyList();
|
|
}
|
|
if (args.length == 1)
|
|
{
|
|
return Arrays.asList("enable", "disable", "reload", "list");
|
|
} else if (args.length == 2 && !args[0].equalsIgnoreCase("list"))
|
|
{
|
|
return Arrays.stream(server.getPluginManager().getPlugins()).map(Plugin::getName)
|
|
.filter(pl -> !UNTOUCHABLE_PLUGINS.contains(pl)).toList();
|
|
}
|
|
|
|
return Collections.emptyList();
|
|
}
|
|
} |