2019-02-27 02:50:18 +01:00
package com.songoda.ultimatemoderation.command ;
import com.songoda.ultimatemoderation.UltimateModeration ;
import com.songoda.ultimatemoderation.command.commands.* ;
import com.songoda.ultimatemoderation.utils.Methods ;
import org.bukkit.command.Command ;
import org.bukkit.command.CommandExecutor ;
import org.bukkit.command.CommandSender ;
import org.bukkit.entity.Player ;
import java.util.ArrayList ;
import java.util.Collections ;
import java.util.List ;
public class CommandManager implements CommandExecutor {
private UltimateModeration instance ;
private TabManager tabManager ;
private List < AbstractCommand > commands = new ArrayList < > ( ) ;
public CommandManager ( UltimateModeration instance ) {
this . instance = instance ;
this . tabManager = new TabManager ( this ) ;
instance . getCommand ( " UltimateModeration " ) . setExecutor ( this ) ;
instance . getCommand ( " ClearChat " ) . setExecutor ( this ) ;
instance . getCommand ( " ToggleChat " ) . setExecutor ( this ) ;
instance . getCommand ( " RandomPlayer " ) . setExecutor ( this ) ;
2019-02-27 04:08:19 +01:00
instance . getCommand ( " Vanish " ) . setExecutor ( this ) ;
2019-02-27 21:32:46 +01:00
instance . getCommand ( " ViewEnderChest " ) . setExecutor ( this ) ;
2019-02-27 21:59:54 +01:00
instance . getCommand ( " InvSee " ) . setExecutor ( this ) ;
2019-02-27 22:45:39 +01:00
instance . getCommand ( " Freeze " ) . setExecutor ( this ) ;
2019-02-28 20:06:37 +01:00
instance . getCommand ( " Revive " ) . setExecutor ( this ) ;
2019-02-28 22:57:39 +01:00
instance . getCommand ( " Spy " ) . setExecutor ( this ) ;
2019-03-01 00:00:22 +01:00
instance . getCommand ( " CommandSpy " ) . setExecutor ( this ) ;
2019-03-03 22:33:44 +01:00
instance . getCommand ( " Ban " ) . setExecutor ( this ) ;
instance . getCommand ( " UnBan " ) . setExecutor ( this ) ;
instance . getCommand ( " Kick " ) . setExecutor ( this ) ;
instance . getCommand ( " Mute " ) . setExecutor ( this ) ;
instance . getCommand ( " UnMute " ) . setExecutor ( this ) ;
instance . getCommand ( " Warn " ) . setExecutor ( this ) ;
instance . getCommand ( " RunTemplate " ) . setExecutor ( this ) ;
2019-03-07 05:31:30 +01:00
instance . getCommand ( " Ticket " ) . setExecutor ( this ) ;
2019-03-08 17:24:47 +01:00
instance . getCommand ( " StaffChat " ) . setExecutor ( this ) ;
2019-02-27 02:50:18 +01:00
AbstractCommand commandUltimateModeration = addCommand ( new CommandUltimateModeration ( ) ) ;
addCommand ( new CommandClearChat ( ) ) ;
addCommand ( new CommandToggleChat ( ) ) ;
addCommand ( new CommandRandomPlayer ( ) ) ;
2019-02-27 04:08:19 +01:00
addCommand ( new CommandVanish ( ) ) ;
2019-02-27 21:32:46 +01:00
addCommand ( new CommandViewEnderChest ( ) ) ;
2019-02-27 21:59:54 +01:00
addCommand ( new CommandInvSee ( ) ) ;
2019-02-27 22:45:39 +01:00
addCommand ( new CommandFreeze ( ) ) ;
2019-02-28 20:06:37 +01:00
addCommand ( new CommandRevive ( ) ) ;
2019-02-28 22:57:39 +01:00
addCommand ( new CommandSpy ( ) ) ;
2019-03-01 00:00:22 +01:00
addCommand ( new CommandCommandSpy ( ) ) ;
2019-03-03 22:33:44 +01:00
addCommand ( new CommandBan ( ) ) ;
addCommand ( new CommandUnBan ( ) ) ;
addCommand ( new CommandKick ( ) ) ;
addCommand ( new CommandMute ( ) ) ;
addCommand ( new CommandUnMute ( ) ) ;
addCommand ( new CommandWarn ( ) ) ;
addCommand ( new CommandRunTemplate ( ) ) ;
2019-03-07 05:31:30 +01:00
addCommand ( new CommandTicket ( ) ) ;
2019-03-08 17:24:47 +01:00
addCommand ( new CommandStaffChat ( ) ) ;
2019-02-27 02:50:18 +01:00
addCommand ( new CommandSettings ( commandUltimateModeration ) ) ;
2019-03-03 22:33:44 +01:00
addCommand ( new CommandHelp ( commandUltimateModeration ) ) ;
2019-02-27 02:50:18 +01:00
addCommand ( new CommandReload ( commandUltimateModeration ) ) ;
for ( AbstractCommand abstractCommand : commands ) {
if ( abstractCommand . getParent ( ) ! = null ) continue ;
instance . getCommand ( abstractCommand . getCommand ( ) ) . setTabCompleter ( tabManager ) ;
}
}
private AbstractCommand addCommand ( AbstractCommand abstractCommand ) {
commands . add ( abstractCommand ) ;
return abstractCommand ;
}
@Override
public boolean onCommand ( CommandSender commandSender , Command command , String s , String [ ] strings ) {
for ( AbstractCommand abstractCommand : commands ) {
2019-02-27 15:46:14 +01:00
if ( abstractCommand . getCommand ( ) ! = null & & abstractCommand . getCommand ( ) . equalsIgnoreCase ( command . getName ( ) . toLowerCase ( ) ) ) {
if ( strings . length = = 0 | | abstractCommand . hasArgs ( ) ) {
2019-02-27 02:50:18 +01:00
processRequirements ( abstractCommand , commandSender , strings ) ;
return true ;
}
2019-02-27 15:46:14 +01:00
} else if ( strings . length ! = 0 & & abstractCommand . getParent ( ) ! = null & & abstractCommand . getParent ( ) . getCommand ( ) . equalsIgnoreCase ( command . getName ( ) ) ) {
String cmd = strings [ 0 ] ;
String cmd2 = strings . length > = 2 ? String . join ( " " , strings [ 0 ] , strings [ 1 ] ) : null ;
for ( String cmds : abstractCommand . getSubCommand ( ) ) {
if ( cmd . equalsIgnoreCase ( cmds ) | | ( cmd2 ! = null & & cmd2 . equalsIgnoreCase ( cmds ) ) ) {
processRequirements ( abstractCommand , commandSender , strings ) ;
return true ;
}
}
2019-02-27 02:50:18 +01:00
}
}
commandSender . sendMessage ( instance . getReferences ( ) . getPrefix ( ) + Methods . formatText ( " &7The command you entered does not exist or is spelt incorrectly. " ) ) ;
return true ;
}
private void processRequirements ( AbstractCommand command , CommandSender sender , String [ ] strings ) {
if ( ! ( sender instanceof Player ) & & command . isNoConsole ( ) ) {
sender . sendMessage ( " You must be a player to use this command. " ) ;
return ;
}
if ( command . getPermissionNode ( ) = = null | | sender . hasPermission ( command . getPermissionNode ( ) ) ) {
AbstractCommand . ReturnType returnType = command . runCommand ( instance , sender , strings ) ;
if ( returnType = = AbstractCommand . ReturnType . SYNTAX_ERROR ) {
sender . sendMessage ( instance . getReferences ( ) . getPrefix ( ) + Methods . formatText ( " &cInvalid Syntax! " ) ) ;
sender . sendMessage ( instance . getReferences ( ) . getPrefix ( ) + Methods . formatText ( " &7The valid syntax is: &6 " + command . getSyntax ( ) + " &7. " ) ) ;
}
return ;
}
sender . sendMessage ( instance . getReferences ( ) . getPrefix ( ) + instance . getLocale ( ) . getMessage ( " event.general.nopermission " ) ) ;
}
public List < AbstractCommand > getCommands ( ) {
return Collections . unmodifiableList ( commands ) ;
}
}