2022-04-06 22:43:49 +00:00
package dev.plex.command.impl ;
import dev.plex.command.PlexCommand ;
import dev.plex.command.annotation.CommandParameters ;
import dev.plex.command.annotation.CommandPermissions ;
import dev.plex.command.source.RequiredCommandSource ;
import dev.plex.rank.enums.Rank ;
import dev.plex.util.PlexUtils ;
2022-04-07 23:23:38 +00:00
import java.util.ArrayList ;
2022-04-06 22:43:49 +00:00
import net.kyori.adventure.text.Component ;
import org.bukkit.Bukkit ;
import org.bukkit.World ;
import org.bukkit.command.CommandSender ;
import org.bukkit.entity.Entity ;
import org.bukkit.entity.EntityType ;
import org.bukkit.entity.Player ;
import org.jetbrains.annotations.NotNull ;
import org.jetbrains.annotations.Nullable ;
import java.util.Arrays ;
import java.util.HashMap ;
import java.util.LinkedList ;
import java.util.List ;
@CommandPermissions ( level = Rank . ADMIN , permission = " plex.entitywipe " , source = RequiredCommandSource . ANY )
2022-04-06 23:42:47 +00:00
@CommandParameters ( name = " entitywipe " , description = " Remove various server entities that may cause lag, such as dropped items, minecarts, and boats. " , usage = " /<command> [name] " , aliases = " ew,rd " )
2022-04-06 22:43:49 +00:00
public class EntityWipeCMD extends PlexCommand
{
@Override
protected Component execute ( @NotNull CommandSender sender , @Nullable Player playerSender , @NotNull String [ ] args )
{
List < String > entityBlacklist = plugin . config . getStringList ( " entitywipe_list " ) ;
List < String > entityWhitelist = new LinkedList < > ( Arrays . asList ( args ) ) ;
EntityType [ ] entityTypes = EntityType . values ( ) ;
2022-04-07 23:23:38 +00:00
entityWhitelist . removeIf ( name - >
{
2022-04-07 14:59:04 +00:00
boolean res = Arrays . stream ( entityTypes ) . noneMatch ( entityType - > name . equalsIgnoreCase ( entityType . name ( ) ) ) ;
2022-04-06 22:43:49 +00:00
if ( res )
{
sender . sendMessage ( messageComponent ( " invalidEntityType " , name ) ) ;
}
return res ;
} ) ;
boolean useBlacklist = args . length = = 0 ;
HashMap < String , Integer > entityCounts = new HashMap < > ( ) ;
for ( World world : Bukkit . getWorlds ( ) )
{
for ( Entity entity : world . getEntities ( ) )
{
if ( entity . getType ( ) ! = EntityType . PLAYER )
{
2022-04-10 21:56:15 +00:00
String type = entity . getType ( ) . name ( ) ;
2022-04-06 22:43:49 +00:00
if ( useBlacklist ? entityBlacklist . stream ( ) . noneMatch ( entityName - > entityName . equalsIgnoreCase ( type ) ) : entityWhitelist . stream ( ) . anyMatch ( entityName - > entityName . equalsIgnoreCase ( type ) ) )
{
entity . remove ( ) ;
2022-04-07 23:23:38 +00:00
entityCounts . put ( type , entityCounts . getOrDefault ( type , 0 ) + 1 ) ;
2022-04-06 22:43:49 +00:00
}
}
}
}
int entityCount = entityCounts . values ( ) . stream ( ) . mapToInt ( a - > a ) . sum ( ) ;
if ( useBlacklist )
{
PlexUtils . broadcast ( messageComponent ( " removedEntities " , sender . getName ( ) , entityCount ) ) ;
}
else
{
if ( entityCount = = 0 )
{
sender . sendMessage ( messageComponent ( " noRemovedEntities " ) ) ;
return null ;
}
String list = String . join ( " , " , entityCounts . keySet ( ) ) ;
list = list . replaceAll ( " (, )(?!.* \ 1) " , ( list . indexOf ( " , " ) = = list . lastIndexOf ( " , " ) ? " " : " , " ) + " and " ) ;
PlexUtils . broadcast ( messageComponent ( " removedEntitiesOfTypes " , sender . getName ( ) , entityCount , list ) ) ;
}
2022-04-07 23:23:38 +00:00
/ * entityCounts . forEach ( ( entityName , numRemoved ) - > {
2022-04-06 22:43:49 +00:00
sender . sendMessage ( messageComponent ( " removedEntitiesOfType " , sender . getName ( ) , numRemoved , entityName ) ) ;
2022-04-07 23:23:38 +00:00
} ) ; * /
2022-04-06 22:43:49 +00:00
return null ;
}
2022-04-07 23:23:38 +00:00
public @NotNull List < String > tabComplete ( @NotNull CommandSender sender , @NotNull String alias , @NotNull String [ ] args ) throws IllegalArgumentException
{
List < String > entities = new ArrayList < > ( ) ;
for ( World world : Bukkit . getWorlds ( ) )
{
for ( Entity entity : world . getEntities ( ) )
{
if ( entity . getType ( ) ! = EntityType . PLAYER )
{
2022-04-10 22:01:02 +00:00
entities . add ( entity . getType ( ) . name ( ) ) ;
2022-04-07 23:23:38 +00:00
}
}
}
return entities . stream ( ) . toList ( ) ;
}
2022-04-06 22:43:49 +00:00
}