From 8bf772b5027300a8336c64170079ed1241bedf99 Mon Sep 17 00:00:00 2001 From: Alco_Rs11 Date: Fri, 19 Jan 2024 15:38:19 -0500 Subject: [PATCH] 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. --- .../dev/plex/command/impl/MobPurgeCMD.java | 64 ++++++++++++++----- server/src/main/resources/messages.yml | 3 + 2 files changed, 50 insertions(+), 17 deletions(-) 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 b0acadf..52e4394 100644 --- a/server/src/main/java/dev/plex/command/impl/MobPurgeCMD.java +++ b/server/src/main/java/dev/plex/command/impl/MobPurgeCMD.java @@ -4,19 +4,24 @@ 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.EntityType; -import org.bukkit.entity.Mob; +import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.*; +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") @@ -26,27 +31,52 @@ public class MobPurgeCMD extends PlexCommand { @Override protected Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, @NotNull String[] args) { - HashMap entityCounts = new HashMap<>(); + 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 + " valid mobs were removed"); + } else { + PlexUtils.broadcast(messageComponent("removedMobs", sender.getName(), count)); + PlexLog.debug("All " + count + " valid mobs were removed"); + } + return null; + } + public int purgeMobs(EntityType type) { + int removed = 0; for (World world : Bukkit.getWorlds()) { - for (Entity entity : world.getEntities()) { - if (entity instanceof Mob) { - String type = entity.getType().name(); + 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++; } } } - - int entityCount = entityCounts.values().stream().mapToInt(a -> a).sum(); - - PlexUtils.broadcast(messageComponent("removedMobs", sender.getName(), entityCount)); - - /*entityCounts.forEach((entityName, numRemoved) -> { - sender.sendMessage(messageComponent("removedEntitiesOfType", sender.getName(), numRemoved, entityName)); - });*/ - return null; + return removed; } // 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 diff --git a/server/src/main/resources/messages.yml b/server/src/main/resources/messages.yml index 99f9736..1d9e800 100644 --- a/server/src/main/resources/messages.yml +++ b/server/src/main/resources/messages.yml @@ -160,6 +160,9 @@ removedEntitiesOfType: "Removed {1} {2}" # 0 - Entity type that is invalid invalidEntityType: "Notice: Entity type {0} is invalid!" noRemovedEntities: "No entities were removed." +noRemovedMobs: "No mobs were 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"