From b168029df19d8e4e74661d47c5a39ec0c3fc7348 Mon Sep 17 00:00:00 2001 From: Alco_Rs11 Date: Fri, 19 Jan 2024 23:29:00 -0500 Subject: [PATCH] Fixed /mp and it now has the ability to purge one or all mobs + tab completion. (#79) * Added a tab completion to /mp because our genius Plex devs forgot to. Its shitty but works lmao * Rewrote /mp to actually work with or without args so now specific or all mobs can be purged, instead of just all mobs being purged. Why the REAL plex developers didn't include this (yet they did for /rd) defies any sort of logic. * Tweaked the command a bit. It works as it should now. * Tweaked the command a bit. It works as it should now. --- .../dev/plex/command/impl/EntityWipeCMD.java | 75 +++++-------- .../dev/plex/command/impl/MobPurgeCMD.java | 101 +++++++++++++----- server/src/main/resources/messages.yml | 5 + 3 files changed, 106 insertions(+), 75 deletions(-) diff --git a/server/src/main/java/dev/plex/command/impl/EntityWipeCMD.java b/server/src/main/java/dev/plex/command/impl/EntityWipeCMD.java index 1f556f5..93f570e 100644 --- a/server/src/main/java/dev/plex/command/impl/EntityWipeCMD.java +++ b/server/src/main/java/dev/plex/command/impl/EntityWipeCMD.java @@ -20,11 +20,9 @@ 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 = "/ [entity] [radius]", aliases = "ew,rd") -public class EntityWipeCMD extends PlexCommand -{ +public class EntityWipeCMD extends PlexCommand { @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 entityBlacklist = plugin.config.getStringList("entitywipe_list"); List entityWhitelist = new LinkedList<>(Arrays.asList(args)); @@ -36,8 +34,7 @@ public class EntityWipeCMD extends PlexCommand PlexLog.debug("using blacklist: " + useBlacklist); 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 *= radius; entityWhitelist.remove(entityWhitelist.size() - 1); // remove the radius from the list @@ -49,8 +46,7 @@ public class EntityWipeCMD extends PlexCommand entityWhitelist.removeIf(name -> { boolean res = Arrays.stream(entityTypes).noneMatch(entityType -> name.equalsIgnoreCase(entityType.name())); - if (res) - { + if (res) { sender.sendMessage(messageComponent("invalidEntityType", name)); } return res; @@ -58,21 +54,15 @@ public class EntityWipeCMD extends PlexCommand HashMap entityCounts = new HashMap<>(); - for (World world : Bukkit.getWorlds()) - { - for (Entity entity : world.getEntities()) - { - if (entity.getType() != EntityType.PLAYER) - { + for (World world : Bukkit.getWorlds()) { + for (Entity entity : world.getEntities()) { + if (entity.getType() != EntityType.PLAYER) { String type = entity.getType().name(); - if (useBlacklist ? entityBlacklist.stream().noneMatch(entityName -> entityName.equalsIgnoreCase(type)) : entityWhitelist.stream().anyMatch(entityName -> entityName.equalsIgnoreCase(type))) - { - if (radius > 0) - { + if (useBlacklist ? entityBlacklist.stream().noneMatch(entityName -> entityName.equalsIgnoreCase(type)) : entityWhitelist.stream().anyMatch(entityName -> entityName.equalsIgnoreCase(type))) { + if (radius > 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"); continue; } @@ -88,14 +78,10 @@ public class EntityWipeCMD extends PlexCommand int entityCount = entityCounts.values().stream().mapToInt(a -> a).sum(); - if (useBlacklist) - { + if (useBlacklist) { PlexUtils.broadcast(messageComponent("removedEntities", sender.getName(), entityCount)); - } - else - { - if (entityCount == 0) - { + } else { + if (entityCount == 0) { sender.sendMessage(messageComponent("noRemovedEntities")); return null; } @@ -106,15 +92,11 @@ public class EntityWipeCMD extends PlexCommand return null; } - public @NotNull List tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException - { + public @NotNull List tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException { List entities = new ArrayList<>(); - for (World world : Bukkit.getWorlds()) - { - for (Entity entity : world.getEntities()) - { - if (entity.getType() != EntityType.PLAYER) - { + for (World world : Bukkit.getWorlds()) { + for (Entity entity : world.getEntities()) { + if (entity.getType() != EntityType.PLAYER) { entities.add(entity.getType().name()); } } @@ -122,31 +104,22 @@ public class EntityWipeCMD extends PlexCommand return entities.stream().toList(); } - private Integer parseInt(CommandSender sender, String string) - { - try - { + private Integer parseInt(CommandSender sender, String string) { + try { return Integer.parseInt(string); - } - catch (NumberFormatException ex) - { + } catch (NumberFormatException ex) { sender.sendMessage(mmString("" + string + " is not a valid number!")); } return null; } - private boolean isNumeric(String string) - { - if (string == null) - { + private boolean isNumeric(String string) { + if (string == null) { return false; } - try - { + try { int num = Integer.parseInt(string); - } - catch (NumberFormatException nfe) - { + } catch (NumberFormatException nfe) { return false; } return true; diff --git a/server/src/main/java/dev/plex/command/impl/MobPurgeCMD.java b/server/src/main/java/dev/plex/command/impl/MobPurgeCMD.java index 9296453..3159d2d 100644 --- a/server/src/main/java/dev/plex/command/impl/MobPurgeCMD.java +++ b/server/src/main/java/dev/plex/command/impl/MobPurgeCMD.java @@ -4,50 +4,103 @@ 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.apache.commons.lang3.text.WordUtils; import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.command.CommandSender; import org.bukkit.entity.Entity; -import org.bukkit.entity.Mob; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.HashMap; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; @CommandPermissions(permission = "plex.mobpurge", source = RequiredCommandSource.ANY) -@CommandParameters(name = "mobpurge", description = "Purge all mobs.", usage = "/", aliases = "mp") -public class MobPurgeCMD extends PlexCommand -{ +@CommandParameters(name = "mobpurge", description = "Purge all mobs.", usage = "/ ", aliases = "mp") +public class MobPurgeCMD extends PlexCommand { + + public static final List MOB_TYPES = new ArrayList<>(); + @Override - protected Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, @NotNull String[] args) - { - HashMap entityCounts = new HashMap<>(); + protected Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, @NotNull String[] args) { + EntityType type = null; + String mobName = null; + if (args.length > 0) { + try { + type = EntityType.valueOf(args[0].toUpperCase()); + } catch (Exception e) { + PlexLog.debug("A genius tried and failed removing the following invalid mob: " + args[0].toUpperCase()); + send(sender, messageComponent("notAValidMob")); + return null; + } + if (!MOB_TYPES.contains(type)) { + PlexLog.debug(Arrays.deepToString(MOB_TYPES.toArray())); + PlexLog.debug("A genius tried to remove a mob that doesn't exist: " + args[0].toUpperCase()); + sender.sendMessage(messageComponent("notAValidMobButValidEntity")); + return null; + } + } + if (type != null) { + mobName = WordUtils.capitalizeFully(type.name().replace("_", " ")); + PlexLog.debug("The args aren't null so the mob is: " + mobName); + } + int count = purgeMobs(type); + if (type != null) { + PlexUtils.broadcast(messageComponent("removedEntitiesOfTypes", sender.getName(), count, mobName)); + PlexLog.debug("All " + count + " of " + mobName + " were removed"); + } else { + PlexUtils.broadcast(messageComponent("removedMobs", sender.getName(), count)); + PlexLog.debug("All " + count + " valid mobs were removed"); + } + sender.sendMessage(messageComponent("amountOfMobsRemoved", count, (type != null ? mobName : "mob") + multipleS(count))); + return null; + } - for (World world : Bukkit.getWorlds()) - { - for (Entity entity : world.getEntities()) - { - if (entity instanceof Mob) - { - String type = entity.getType().name(); + public static String multipleS(int count) { + return (count == 1 ? "" : "s"); + } + + // Removes the mobs. + + public int purgeMobs(EntityType type) { + int removed = 0; + for (World world : Bukkit.getWorlds()) { + for (Entity entity : world.getLivingEntities()) { + if (entity instanceof LivingEntity && !(entity instanceof Player)) { + if (type != null && !entity.getType().equals(type)) { + continue; + } entity.remove(); - - entityCounts.put(type, entityCounts.getOrDefault(type, 0) + 1); + removed++; } } } + return removed; + } - int entityCount = entityCounts.values().stream().mapToInt(a -> a).sum(); + // Adds a tab completion for /mp so players stop complaining we (mostly me) nuked all their mobs because a filter for some reason was never added by the REAL plex devs. Go figure. -Alco_Rs11 - PlexUtils.broadcast(messageComponent("removedMobs", sender.getName(), entityCount)); + public static List getAllMobs() { + List mobs = new ArrayList<>(); + Arrays.stream(EntityType.values()).filter(EntityType::isAlive).filter(EntityType::isSpawnable).forEach(MOB_TYPES::add); + for (EntityType entityType : MOB_TYPES) { + mobs.add(entityType.name()); + } + return mobs; + } - /*entityCounts.forEach((entityName, numRemoved) -> { - sender.sendMessage(messageComponent("removedEntitiesOfType", sender.getName(), numRemoved, entityName)); - });*/ - return null; + public @NotNull List tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException { + if (args.length == 1) { + return getAllMobs(); + } + return Collections.emptyList(); } } \ No newline at end of file diff --git a/server/src/main/resources/messages.yml b/server/src/main/resources/messages.yml index 99f9736..d9021ac 100644 --- a/server/src/main/resources/messages.yml +++ b/server/src/main/resources/messages.yml @@ -160,6 +160,11 @@ removedEntitiesOfType: "Removed {1} {2}" # 0 - Entity type that is invalid invalidEntityType: "Notice: Entity type {0} is invalid!" noRemovedEntities: "No entities were removed." +# 0 - Number of mobs removed +# 1 - Type of mob removed +amountOfMobsRemoved: "{0} {1} removed." +notAValidMob: "That is not a valid mob." +notAValidMobButValidEntity: "That is a valid entity, but is not a valid mob." # 0 - The command sender # 1 - Number of mobs removed removedMobs: "{0} - Removed {1} mobs"