Tweaked the command a bit. It works as it should now.

This commit is contained in:
Alco_Rs11 2024-01-19 19:45:48 -05:00
parent 3c1d0b5564
commit f7a75a5b34
1 changed files with 24 additions and 51 deletions

View File

@ -20,11 +20,9 @@ import java.util.*;
@CommandPermissions(permission = "plex.entitywipe", source = RequiredCommandSource.ANY) @CommandPermissions(permission = "plex.entitywipe", source = RequiredCommandSource.ANY)
@CommandParameters(name = "entitywipe", description = "Remove various server entities that may cause lag, such as dropped items, minecarts, and boats.", usage = "/<command> [entity] [radius]", aliases = "ew,rd") @CommandParameters(name = "entitywipe", description = "Remove various server entities that may cause lag, such as dropped items, minecarts, and boats.", usage = "/<command> [entity] [radius]", aliases = "ew,rd")
public class EntityWipeCMD extends PlexCommand public class EntityWipeCMD extends PlexCommand {
{
@Override @Override
protected Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, @NotNull String[] args) protected Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, @NotNull String[] args) {
{
List<String> entityBlacklist = plugin.config.getStringList("entitywipe_list"); List<String> entityBlacklist = plugin.config.getStringList("entitywipe_list");
List<String> entityWhitelist = new LinkedList<>(Arrays.asList(args)); List<String> entityWhitelist = new LinkedList<>(Arrays.asList(args));
@ -36,8 +34,7 @@ public class EntityWipeCMD extends PlexCommand
PlexLog.debug("using blacklist: " + useBlacklist); PlexLog.debug("using blacklist: " + useBlacklist);
PlexLog.debug("radius specified: " + radiusSpecified); PlexLog.debug("radius specified: " + radiusSpecified);
if (radiusSpecified) if (radiusSpecified) {
{
radius = parseInt(sender, args[entityWhitelist.size() - 1]); // get the args length as the size of the list radius = parseInt(sender, args[entityWhitelist.size() - 1]); // get the args length as the size of the list
radius *= radius; radius *= radius;
entityWhitelist.remove(entityWhitelist.size() - 1); // remove the radius from the list entityWhitelist.remove(entityWhitelist.size() - 1); // remove the radius from the list
@ -49,8 +46,7 @@ public class EntityWipeCMD extends PlexCommand
entityWhitelist.removeIf(name -> entityWhitelist.removeIf(name ->
{ {
boolean res = Arrays.stream(entityTypes).noneMatch(entityType -> name.equalsIgnoreCase(entityType.name())); boolean res = Arrays.stream(entityTypes).noneMatch(entityType -> name.equalsIgnoreCase(entityType.name()));
if (res) if (res) {
{
sender.sendMessage(messageComponent("invalidEntityType", name)); sender.sendMessage(messageComponent("invalidEntityType", name));
} }
return res; return res;
@ -58,21 +54,15 @@ public class EntityWipeCMD extends PlexCommand
HashMap<String, Integer> entityCounts = new HashMap<>(); HashMap<String, Integer> entityCounts = new HashMap<>();
for (World world : Bukkit.getWorlds()) for (World world : Bukkit.getWorlds()) {
{ for (Entity entity : world.getEntities()) {
for (Entity entity : world.getEntities()) if (entity.getType() != EntityType.PLAYER) {
{
if (entity.getType() != EntityType.PLAYER)
{
String type = entity.getType().name(); String type = entity.getType().name();
if (useBlacklist ? entityBlacklist.stream().noneMatch(entityName -> entityName.equalsIgnoreCase(type)) : entityWhitelist.stream().anyMatch(entityName -> entityName.equalsIgnoreCase(type))) if (useBlacklist ? entityBlacklist.stream().noneMatch(entityName -> entityName.equalsIgnoreCase(type)) : entityWhitelist.stream().anyMatch(entityName -> entityName.equalsIgnoreCase(type))) {
{ if (radius > 0) {
if (radius > 0)
{
PlexLog.debug("we got here, radius is > 0"); PlexLog.debug("we got here, radius is > 0");
if (playerSender != null && entity.getWorld() == playerSender.getWorld() && playerSender.getLocation().distanceSquared(entity.getLocation()) > radius) if (playerSender != null && entity.getWorld() == playerSender.getWorld() && playerSender.getLocation().distanceSquared(entity.getLocation()) > radius) {
{
PlexLog.debug("continuing"); PlexLog.debug("continuing");
continue; continue;
} }
@ -88,14 +78,10 @@ public class EntityWipeCMD extends PlexCommand
int entityCount = entityCounts.values().stream().mapToInt(a -> a).sum(); int entityCount = entityCounts.values().stream().mapToInt(a -> a).sum();
if (useBlacklist) if (useBlacklist) {
{
PlexUtils.broadcast(messageComponent("removedEntities", sender.getName(), entityCount)); PlexUtils.broadcast(messageComponent("removedEntities", sender.getName(), entityCount));
} } else {
else if (entityCount == 0) {
{
if (entityCount == 0)
{
sender.sendMessage(messageComponent("noRemovedEntities")); sender.sendMessage(messageComponent("noRemovedEntities"));
return null; return null;
} }
@ -106,15 +92,11 @@ public class EntityWipeCMD extends PlexCommand
return null; return null;
} }
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException {
{
List<String> entities = new ArrayList<>(); List<String> entities = new ArrayList<>();
for (World world : Bukkit.getWorlds()) for (World world : Bukkit.getWorlds()) {
{ for (Entity entity : world.getEntities()) {
for (Entity entity : world.getEntities()) if (entity.getType() != EntityType.PLAYER) {
{
if (entity.getType() != EntityType.PLAYER)
{
entities.add(entity.getType().name()); entities.add(entity.getType().name());
} }
} }
@ -122,31 +104,22 @@ public class EntityWipeCMD extends PlexCommand
return entities.stream().toList(); return entities.stream().toList();
} }
private Integer parseInt(CommandSender sender, String string) private Integer parseInt(CommandSender sender, String string) {
{ try {
try
{
return Integer.parseInt(string); return Integer.parseInt(string);
} } catch (NumberFormatException ex) {
catch (NumberFormatException ex)
{
sender.sendMessage(mmString("<red>" + string + "<red> is not a valid number!")); sender.sendMessage(mmString("<red>" + string + "<red> is not a valid number!"));
} }
return null; return null;
} }
private boolean isNumeric(String string) private boolean isNumeric(String string) {
{ if (string == null) {
if (string == null)
{
return false; return false;
} }
try try {
{
int num = Integer.parseInt(string); int num = Integer.parseInt(string);
} } catch (NumberFormatException nfe) {
catch (NumberFormatException nfe)
{
return false; return false;
} }
return true; return true;