2016-03-02 19:28:01 +00:00
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 ;
2016-03-06 15:56:15 +00:00
import me.totalfreedom.totalfreedommod.rank.Rank ;
2019-07-31 16:19:23 +00:00
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 ;
2016-10-02 20:42:36 +00:00
@CommandPermissions ( level = Rank . TELNET_ADMIN , source = SourceType . BOTH )
2020-03-30 23:43:57 +00:00
@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 " )
2015-10-19 17:43:46 +00:00
public class Command_plugincontrol extends FreedomCommand
2013-03-24 19:55:34 +00:00
{
2015-11-22 18:26:47 +00:00
2013-03-24 19:55:34 +00:00
@Override
2015-11-22 18:26:47 +00:00
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 ( ) ;
2016-03-02 19:28:01 +00:00
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
{
2016-03-02 19:28:01 +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
{
2016-03-02 19:28:01 +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 ) )
{
2016-03-02 19:28:01 +00:00
msg ( " Error enabling plugin " + target . getName ( ) ) ;
2014-05-13 14:32:01 +00:00
return true ;
}
2013-03-24 19:55:34 +00:00
2016-03-02 19:28:01 +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
{
2016-03-02 19:28:01 +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
{
2016-03-02 19:28:01 +00:00
msg ( " Plugin is already disabled. " ) ;
2014-05-13 14:32:01 +00:00
return true ;
}
if ( target . getName ( ) . equals ( plugin . getName ( ) ) )
{
2016-03-02 19:28:01 +00:00
msg ( " 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
{
2016-03-02 19:28:01 +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
}
2016-03-02 19:28:01 +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
{
2016-03-02 19:28:01 +00:00
msg ( " 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
{
2016-03-02 19:28:01 +00:00
msg ( " Use /tfm reload to reload instead. " ) ;
2014-05-13 14:32:01 +00:00
return true ;
}
pm . disablePlugin ( target ) ;
pm . enablePlugin ( target ) ;
2016-03-02 19:28:01 +00:00
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 . al . isAdmin ( sender ) )
{
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 ( ) )
{
names . add ( plugin . getName ( ) ) ;
}
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
}
}