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

153 lines
4.4 KiB
Java
Raw Normal View History

package me.totalfreedom.totalfreedommod.commands;
2013-03-24 19:55:34 +00:00
import me.totalfreedom.totalfreedommod.rank.PlayerRank;
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 = PlayerRank.SENIOR_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
{
@Override
public boolean run(CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
{
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();
playerMsg(ChatColor.GRAY + "- " + (serverPlugin.isEnabled() ? ChatColor.GREEN : ChatColor.RED) + serverPlugin.getName()
+ 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
{
2014-05-13 14:32:01 +00:00
playerMsg("Plugin not found!");
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
{
2014-05-13 14:32:01 +00:00
playerMsg("Plugin is already enabled.");
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());
return true;
}
2013-03-24 19:55:34 +00:00
2014-05-13 14:32:01 +00:00
playerMsg(target.getName() + " is now enabled.");
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
{
2014-05-13 14:32:01 +00:00
playerMsg("Plugin not found!");
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
{
2014-05-13 14:32:01 +00:00
playerMsg("Plugin is already disabled.");
return true;
}
if (target.getName().equals(plugin.getName()))
{
playerMsg("You cannot disable " + plugin.getName());
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
{
2014-05-13 14:32:01 +00:00
playerMsg("Error disabling plugin " + target.getName());
return true;
2013-03-24 19:55:34 +00:00
}
2014-05-13 14:32:01 +00:00
playerMsg(target.getName() + " is now disabled.");
return true;
}
if ("reload".equals(args[0]))
{
final Plugin target = getPlugin(args[1]);
if (target == null)
2013-03-24 19:55:34 +00:00
{
2014-05-13 14:32:01 +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
{
2014-05-13 14:32:01 +00:00
playerMsg("Use /tfm reload to reload instead.");
return true;
}
pm.disablePlugin(target);
pm.enablePlugin(target);
playerMsg(target.getName() + " reloaded.");
return true;
}
return false;
}
@Override
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
}
}