Added /remove command to remove entities (paintings, items, minecarts, boats, arrows, and primed TNT).

This commit is contained in:
sk89q 2011-02-18 20:31:49 -08:00
parent a6042e76f9
commit 1fe6f09066
4 changed files with 148 additions and 2 deletions

View File

@ -262,6 +262,10 @@ commands:
butcher: butcher:
description: Kill all or nearby mobs description: Kill all or nearby mobs
usage: /<command> [radius] usage: /<command> [radius]
remove:
description: Remove all entities of a type
usage: /<command> <type> <radius>
aliases: ['rem', 'rement']
/fill: /fill:
description: Fill a hole description: Fill a hole
usage: /<command> <block> <radius> [depth] usage: /<command> <block> <radius> [depth]

View File

@ -29,6 +29,18 @@ import com.sk89q.worldedit.blocks.BaseItemStack;
* @author sk89q * @author sk89q
*/ */
public abstract class LocalWorld { public abstract class LocalWorld {
/**
* List of removable entity types.
*/
public enum EntityType {
ARROWS,
ITEMS,
PAINTINGS,
BOATS,
MINECARTS,
TNT,
}
/** /**
* Random generator. * Random generator.
*/ */
@ -298,6 +310,16 @@ public abstract class LocalWorld {
*/ */
public abstract int killMobs(Vector origin, int radius); 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. * Compare if the other world is equal.
* *

View File

@ -24,8 +24,17 @@ import org.bukkit.block.BlockState;
import org.bukkit.block.Furnace; import org.bukkit.block.Furnace;
import org.bukkit.block.CreatureSpawner; import org.bukkit.block.CreatureSpawner;
import org.bukkit.block.Sign; import org.bukkit.block.Sign;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.Boat;
import org.bukkit.entity.Creature; 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.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.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.Location; import org.bukkit.Location;
@ -326,10 +335,10 @@ public class BukkitWorld extends LocalWorld {
double radiusSq = Math.pow(radius, 2); double radiusSq = Math.pow(radius, 2);
for (LivingEntity ent : world.getLivingEntities()) { for (LivingEntity ent : world.getLivingEntities()) {
if (ent instanceof Creature) { if (ent instanceof Creature || ent instanceof Ghast || ent instanceof Slime) {
if (radius == -1 if (radius == -1
|| origin.distanceSq(BukkitUtil.toVector(ent.getLocation())) <= radiusSq) { || origin.distanceSq(BukkitUtil.toVector(ent.getLocation())) <= radiusSq) {
ent.setHealth(0); ent.remove();
num++; num++;
} }
} }
@ -338,6 +347,69 @@ public class BukkitWorld extends LocalWorld {
return num; 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) { private Location toLocation(Vector pt) {
return new Location(world, pt.getX(), pt.getY(), pt.getZ()); return new Location(world, pt.getX(), pt.getY(), pt.getZ());
} }

View File

@ -24,6 +24,7 @@ import com.sk89q.minecraft.util.commands.Command;
import com.sk89q.minecraft.util.commands.CommandContext; import com.sk89q.minecraft.util.commands.CommandContext;
import com.sk89q.minecraft.util.commands.CommandPermissions; import com.sk89q.minecraft.util.commands.CommandPermissions;
import com.sk89q.worldedit.*; import com.sk89q.worldedit.*;
import com.sk89q.worldedit.LocalWorld.EntityType;
import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.patterns.*; import com.sk89q.worldedit.patterns.*;
import com.sk89q.worldedit.regions.CuboidRegion; import com.sk89q.worldedit.regions.CuboidRegion;
@ -323,4 +324,51 @@ public class UtilityCommands {
int killed = player.getWorld().killMobs(origin, radius); int killed = player.getWorld().killMobs(origin, radius);
player.print("Killed " + killed + " mobs."); player.print("Killed " + killed + " mobs.");
} }
@Command(
aliases = {"remove", "rem", "rement"},
usage = "<type> <radius>",
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.");
}
} }