2022-06-14 16:18:45 +02:00
/ *
* This file is a part of the SteamWar software .
*
* Copyright ( C ) 2020 SteamWar . de - Serverteam
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU Affero General Public License for more details .
*
* You should have received a copy of the GNU Affero General Public License
* along with this program . If not , see < https : //www.gnu.org/licenses/>.
* /
package de.steamwar.command ;
import de.steamwar.bungeecore.BungeeCore ;
2022-06-16 21:09:50 +02:00
import de.steamwar.bungeecore.Message ;
2022-06-14 16:18:45 +02:00
import de.steamwar.messages.ChatSender ;
2022-06-16 22:11:24 +02:00
import net.md_5.bungee.BungeeCord ;
2022-06-14 16:18:45 +02:00
import net.md_5.bungee.api.CommandSender ;
import net.md_5.bungee.api.chat.ClickEvent ;
import net.md_5.bungee.api.connection.ProxiedPlayer ;
import net.md_5.bungee.api.plugin.Command ;
import net.md_5.bungee.api.plugin.TabExecutor ;
import java.util.ArrayList ;
import java.util.List ;
2022-06-16 22:11:24 +02:00
import java.util.concurrent.TimeUnit ;
2022-06-14 16:18:45 +02:00
import java.util.concurrent.atomic.AtomicInteger ;
import java.util.function.Supplier ;
import java.util.logging.Level ;
public class SWCommand extends AbstractSWCommand < CommandSender > {
static {
TypeUtils . init ( ) ;
}
2022-06-14 18:16:44 +02:00
private String permission ;
2022-06-14 16:18:45 +02:00
private Command command ;
private List < String > defaultHelpMessages = new ArrayList < > ( ) ;
2022-06-14 18:19:25 +02:00
protected SWCommand ( String command ) {
this ( command , null ) ;
}
2022-06-14 18:16:44 +02:00
protected SWCommand ( String command , String permission ) {
2022-06-14 16:18:45 +02:00
super ( CommandSender . class , command ) ;
2022-06-14 18:16:44 +02:00
this . permission = permission ;
2022-06-14 16:18:45 +02:00
}
2022-06-14 18:16:44 +02:00
protected SWCommand ( String command , String permission , String . . . aliases ) {
2022-06-14 16:18:45 +02:00
super ( CommandSender . class , command , aliases ) ;
2022-06-14 18:16:44 +02:00
this . permission = permission ;
2022-06-14 16:18:45 +02:00
}
@Override
protected void createAndSafeCommand ( String command , String [ ] aliases ) {
2022-06-16 22:14:40 +02:00
this . command = new TabCompletableCommand ( command , permission , aliases ) {
@Override
public void execute ( CommandSender commandSender , String [ ] strings ) {
SWCommand . this . execute ( commandSender , null , strings ) ;
}
2022-06-14 16:18:45 +02:00
2022-06-16 22:14:40 +02:00
@Override
public Iterable < String > onTabComplete ( CommandSender commandSender , String [ ] strings ) {
return SWCommand . this . tabComplete ( commandSender , null , strings ) ;
}
} ;
2022-06-14 16:18:45 +02:00
}
private abstract static class TabCompletableCommand extends Command implements TabExecutor {
2022-06-14 18:16:44 +02:00
public TabCompletableCommand ( String name , String permission , String . . . aliases ) {
super ( name , permission , aliases ) ;
2022-06-14 16:18:45 +02:00
}
}
@Override
public void unregister ( ) {
CommandRegistering . unregister ( this . command ) ;
}
@Override
public void register ( ) {
CommandRegistering . register ( this . command ) ;
}
@Override
protected void commandSystemError ( CommandSender sender , CommandFrameworkException e ) {
BungeeCore . get ( ) . getLogger ( ) . log ( Level . SEVERE , e . getMessage ( ) , e ) ;
ChatSender . of ( sender ) . prefixless ( " COMMAND_SYSTEM_ERROR " ) ;
}
@Override
protected void commandSystemWarning ( Supplier < String > message ) {
BungeeCore . get ( ) . getLogger ( ) . log ( Level . WARNING , message ) ;
}
public void addDefaultHelpMessage ( String message ) {
defaultHelpMessages . add ( message ) ;
}
2022-06-16 20:41:52 +02:00
@Override
protected void sendMessage ( CommandSender sender , String message , Object [ ] args ) {
ChatSender . of ( sender ) . system ( message , args ) ;
}
2022-06-14 16:18:45 +02:00
@Register ( help = true )
private void internalHelp ( ProxiedPlayer p , String . . . args ) {
ChatSender chatSender = ChatSender . of ( p ) ;
try {
chatSender . prefixless ( " COMMAND_HELP_HEAD " , command . getName ( ) ) ;
defaultHelpMessages . forEach ( chatSender : : prefixless ) ;
} catch ( Exception e ) {
BungeeCore . get ( ) . getLogger ( ) . log ( Level . WARNING , " Failed to send help message " , e ) ;
return ;
}
AtomicInteger atomicInteger = new AtomicInteger ( ) ;
if ( args . length ! = 0 ) {
commandList . forEach ( subCommand - > {
List < String > tabCompletes = subCommand . tabComplete ( p , args ) ;
if ( tabCompletes = = null | | tabCompletes . isEmpty ( ) ) {
atomicInteger . incrementAndGet ( ) ;
return ;
}
boolean hasTabCompletes = tabCompletes . stream ( )
. anyMatch ( s - > s . toLowerCase ( ) . startsWith ( args [ args . length - 1 ] . toLowerCase ( ) ) ) ;
if ( hasTabCompletes ) {
send ( chatSender , subCommand ) ;
} else {
atomicInteger . incrementAndGet ( ) ;
}
} ) ;
}
if ( args . length = = 0 | | atomicInteger . get ( ) = = commandList . size ( ) ) {
commandList . forEach ( subCommand - > {
if ( subCommand . guardChecker = = null | | subCommand . guardChecker . guard ( p , GuardCheckType . TAB_COMPLETE , new String [ 0 ] , null ) = = GuardResult . ALLOWED ) {
send ( chatSender , subCommand ) ;
}
} ) ;
}
}
private void send ( ChatSender chatSender , SubCommand < CommandSender > subCommand ) {
try {
for ( String s : subCommand . description ) {
String hover = " §8/§e " + command . getName ( ) + " " + String . join ( " " , subCommand . subCommand ) ;
String suggest = " / " + command . getName ( ) + " " + String . join ( " " , subCommand . subCommand ) ;
2022-06-16 21:09:50 +02:00
chatSender . prefixless ( s , new Message ( " PLAIN_STRING " , hover ) , new ClickEvent ( ClickEvent . Action . SUGGEST_COMMAND , suggest ) ) ;
2022-06-14 16:18:45 +02:00
}
} catch ( Exception e ) {
BungeeCore . get ( ) . getLogger ( ) . log ( Level . WARNING , " Failed to send description of registered method ' " + subCommand . method + " ' with description ' " + subCommand . description + " ' " , e ) ;
}
}
}