Add radius to entitywipe command (#74)

* currently bugged

* Fixed radius not working

---------

Co-authored-by: Focusvity <nathan.curran10012@gmail.com>
This commit is contained in:
2023-11-25 23:43:27 -06:00
committed by GitHub
parent 4de796781d
commit 1cf262ff91
6 changed files with 92 additions and 29 deletions

View File

@ -4,7 +4,7 @@ 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.util.PlexLog;
import dev.plex.util.PlexUtils;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
@ -19,7 +19,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.*;
@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> [name]", 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
{
@Override
@ -29,6 +29,21 @@ public class EntityWipeCMD extends PlexCommand
List<String> entityWhitelist = new LinkedList<>(Arrays.asList(args));
boolean radiusSpecified = !entityWhitelist.isEmpty() && isNumeric(entityWhitelist.get(entityWhitelist.size() - 1)); // try and detect if the last argument of the command is a number
boolean useBlacklist = args.length == 0 || (args.length == 1 && radiusSpecified); // if there are no arguments or the one argument is a number
int radius = 0;
PlexLog.log("using blacklist: " + useBlacklist);
PlexLog.log("radius specified: " + radiusSpecified);
if (radiusSpecified)
{
radius = parseInt(sender, args[entityWhitelist.size() - 1]); // get the args length as the size of the list
entityWhitelist.remove(entityWhitelist.size() - 1); // remove the radius from the list
}
PlexLog.log("radius: " + radius);
EntityType[] entityTypes = EntityType.values();
entityWhitelist.removeIf(name ->
{
@ -40,8 +55,6 @@ public class EntityWipeCMD extends PlexCommand
return res;
});
boolean useBlacklist = args.length == 0;
HashMap<String, Integer> entityCounts = new HashMap<>();
for (World world : Bukkit.getWorlds())
@ -54,6 +67,16 @@ public class EntityWipeCMD extends PlexCommand
if (useBlacklist ? entityBlacklist.stream().noneMatch(entityName -> entityName.equalsIgnoreCase(type)) : entityWhitelist.stream().anyMatch(entityName -> entityName.equalsIgnoreCase(type)))
{
if (radius > 0)
{
PlexLog.log("we got here, radius is > 0");
if (playerSender != null && entity.getWorld() == playerSender.getWorld() && playerSender.getLocation().distance(entity.getLocation()) > radius)
{
PlexLog.log("continuing");
continue;
}
}
PlexLog.log("removed entity: " + entity.getType().name());
entity.remove();
entityCounts.put(type, entityCounts.getOrDefault(type, 0) + 1);
@ -97,4 +120,34 @@ public class EntityWipeCMD extends PlexCommand
}
return entities.stream().toList();
}
private Integer parseInt(CommandSender sender, String string)
{
try
{
return Integer.parseInt(string);
}
catch (NumberFormatException ex)
{
sender.sendMessage(mmString("<red>" + string + "<red> is not a valid number!"));
}
return null;
}
private boolean isNumeric(String string)
{
if (string == null)
{
return false;
}
try
{
int num = Integer.parseInt(string);
}
catch (NumberFormatException nfe)
{
return false;
}
return true;
}
}

View File

@ -5,7 +5,6 @@ import dev.plex.command.PlexCommand;
import dev.plex.command.annotation.CommandParameters;
import dev.plex.command.annotation.CommandPermissions;
import dev.plex.hook.VaultHook;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
@ -14,9 +13,10 @@ import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collections;
import java.util.List;
@CommandParameters(name = "list", description = "Show a list of all online players", aliases = "lsit,who,playerlist,online")
@CommandParameters(name = "list", description = "Show a list of all online players", usage = "/<command> [-d]", aliases = "lsit,who,playerlist,online")
@CommandPermissions(permission = "plex.list")
public class ListCMD extends PlexCommand
{
@ -37,7 +37,7 @@ public class ListCMD extends PlexCommand
.append(Component.space())
.append(Component.text(Bukkit.getMaxPlayers() == 1 ? "player." : "players.").color(NamedTextColor.GRAY));
send(sender, header);
if (players.size() == 0)
if (players.isEmpty())
{
return null;
}
@ -45,10 +45,18 @@ public class ListCMD extends PlexCommand
{
Player player = players.get(i);
Component prefix = VaultHook.getPrefix(getPlexPlayer(player));
if (prefix != null && !prefix.equals(Component.empty()) && !prefix.equals(Component.space())) {
if (prefix != null && !prefix.equals(Component.empty()) && !prefix.equals(Component.space()))
{
list = list.append(prefix).append(Component.space());
}
list = list.append(Component.text(player.getName()).color(NamedTextColor.WHITE));
if (args.length > 0 && args[0].equalsIgnoreCase("-d"))
{
list = list.append(Component.space());
list = list.append(Component.text("(").color(NamedTextColor.WHITE));
list = list.append(player.displayName());
list = list.append(Component.text(")").color(NamedTextColor.WHITE));
}
if (i != players.size() - 1)
{
list = list.append(Component.text(",")).append(Component.space());
@ -56,4 +64,13 @@ public class ListCMD extends PlexCommand
}
return list;
}
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
{
if (args.length == 1)
{
return Collections.singletonList("-d");
}
return Collections.emptyList();
}
}