From 1fe6f0906635dbd3f98e48743ee46183be30098e Mon Sep 17 00:00:00 2001 From: sk89q Date: Fri, 18 Feb 2011 20:31:49 -0800 Subject: [PATCH] Added /remove command to remove entities (paintings, items, minecarts, boats, arrows, and primed TNT). --- plugin.yml | 4 + src/com/sk89q/worldedit/LocalWorld.java | 22 ++++++ .../sk89q/worldedit/bukkit/BukkitWorld.java | 76 ++++++++++++++++++- .../worldedit/commands/UtilityCommands.java | 48 ++++++++++++ 4 files changed, 148 insertions(+), 2 deletions(-) diff --git a/plugin.yml b/plugin.yml index 40cabd68b..f5c1929d4 100644 --- a/plugin.yml +++ b/plugin.yml @@ -262,6 +262,10 @@ commands: butcher: description: Kill all or nearby mobs usage: / [radius] + remove: + description: Remove all entities of a type + usage: / + aliases: ['rem', 'rement'] /fill: description: Fill a hole usage: / [depth] diff --git a/src/com/sk89q/worldedit/LocalWorld.java b/src/com/sk89q/worldedit/LocalWorld.java index 743dc95b7..3bd759822 100644 --- a/src/com/sk89q/worldedit/LocalWorld.java +++ b/src/com/sk89q/worldedit/LocalWorld.java @@ -29,6 +29,18 @@ import com.sk89q.worldedit.blocks.BaseItemStack; * @author sk89q */ public abstract class LocalWorld { + /** + * List of removable entity types. + */ + public enum EntityType { + ARROWS, + ITEMS, + PAINTINGS, + BOATS, + MINECARTS, + TNT, + } + /** * Random generator. */ @@ -298,6 +310,16 @@ public abstract class LocalWorld { */ public abstract int killMobs(Vector origin, int radius); + /** + * Remove entities in an area. + * + * @param type + * @param origin + * @param radius + * @return + */ + public abstract int removeEntities(EntityType type, Vector origin, int radius); + /** * Compare if the other world is equal. * diff --git a/src/com/sk89q/worldedit/bukkit/BukkitWorld.java b/src/com/sk89q/worldedit/bukkit/BukkitWorld.java index 9dfa93f36..5b0ef6c65 100644 --- a/src/com/sk89q/worldedit/bukkit/BukkitWorld.java +++ b/src/com/sk89q/worldedit/bukkit/BukkitWorld.java @@ -24,8 +24,17 @@ import org.bukkit.block.BlockState; import org.bukkit.block.Furnace; import org.bukkit.block.CreatureSpawner; import org.bukkit.block.Sign; +import org.bukkit.entity.Arrow; +import org.bukkit.entity.Boat; import org.bukkit.entity.Creature; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Ghast; +import org.bukkit.entity.Item; import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Minecart; +import org.bukkit.entity.Painting; +import org.bukkit.entity.Slime; +import org.bukkit.entity.TNTPrimed; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.Location; @@ -326,10 +335,10 @@ public class BukkitWorld extends LocalWorld { double radiusSq = Math.pow(radius, 2); for (LivingEntity ent : world.getLivingEntities()) { - if (ent instanceof Creature) { + if (ent instanceof Creature || ent instanceof Ghast || ent instanceof Slime) { if (radius == -1 || origin.distanceSq(BukkitUtil.toVector(ent.getLocation())) <= radiusSq) { - ent.setHealth(0); + ent.remove(); num++; } } @@ -338,6 +347,69 @@ public class BukkitWorld extends LocalWorld { return num; } + /** + * Remove entities in an area. + * + * @param origin + * @param radius + * @return + */ + @Override + public int removeEntities(EntityType type, Vector origin, int radius) { + int num = 0; + double radiusSq = Math.pow(radius, 2); + + for (Entity ent : world.getEntities()) { + if (radius != -1 + && origin.distanceSq(BukkitUtil.toVector(ent.getLocation())) > radiusSq) { + continue; + } + + switch (type) { + case ARROWS: + if (ent instanceof Arrow) { + ent.remove(); + num++; + } + break; + case BOATS: + if (ent instanceof Boat) { + ent.remove(); + num++; + } + break; + case ITEMS: + if (ent instanceof Item) { + ent.remove(); + num++; + } + break; + case MINECARTS: + if (ent instanceof Minecart) { + ent.remove(); + num++; + } + break; + case PAINTINGS: + if (ent instanceof Painting) { + ent.remove(); + num++; + } + break; + case TNT: + if (ent instanceof TNTPrimed) { + ent.remove(); + num++; + } + break; + default: + continue; + } + } + + return num; + } + private Location toLocation(Vector pt) { return new Location(world, pt.getX(), pt.getY(), pt.getZ()); } diff --git a/src/com/sk89q/worldedit/commands/UtilityCommands.java b/src/com/sk89q/worldedit/commands/UtilityCommands.java index c3ca4683b..a857e419e 100644 --- a/src/com/sk89q/worldedit/commands/UtilityCommands.java +++ b/src/com/sk89q/worldedit/commands/UtilityCommands.java @@ -24,6 +24,7 @@ import com.sk89q.minecraft.util.commands.Command; import com.sk89q.minecraft.util.commands.CommandContext; import com.sk89q.minecraft.util.commands.CommandPermissions; import com.sk89q.worldedit.*; +import com.sk89q.worldedit.LocalWorld.EntityType; import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.patterns.*; import com.sk89q.worldedit.regions.CuboidRegion; @@ -323,4 +324,51 @@ public class UtilityCommands { int killed = player.getWorld().killMobs(origin, radius); player.print("Killed " + killed + " mobs."); } + + @Command( + aliases = {"remove", "rem", "rement"}, + usage = " ", + desc = "Remove all entities of a type", + min = 2, + max = 2 + ) + @CommandPermissions({"worldedit.remove"}) + public static void remove(CommandContext args, WorldEdit we, + LocalSession session, LocalPlayer player, EditSession editSession) + throws WorldEditException { + + String typeStr = args.getString(0); + int radius = args.getInteger(1); + + if (radius < -1) { + player.printError("Use -1 to remove all entities in loaded chunks"); + return; + } + + EntityType type = null; + + if (typeStr.matches("arrows?")) { + type = EntityType.ARROWS; + } else if (typeStr.matches("items?") + || typeStr.matches("drops?")) { + type = EntityType.ITEMS; + } else if (typeStr.matches("paintings?") + || typeStr.matches("art")) { + type = EntityType.PAINTINGS; + } else if (typeStr.matches("boats?")) { + type = EntityType.BOATS; + } else if (typeStr.matches("minecarts?") + || typeStr.matches("carts?")) { + type = EntityType.MINECARTS; + } else if (typeStr.matches("tnt")) { + type = EntityType.TNT; + } else { + player.printError("Acceptable types: arrows, items, paintings, boats, minecarts, tnt"); + return; + } + + Vector origin = session.getPlacementPosition(player); + int removed = player.getWorld().removeEntities(type, origin, radius); + player.print("Marked " + removed + " entit(ies) for removal."); + } }