mirror of
https://github.com/TotalFreedomMC/TF-EssentialsX.git
synced 2025-02-15 13:36:31 +00:00
Merge remove and killall command, thanks to @Evonuts and @Iaccidentally.
This commit is contained in:
parent
b3bad6a35a
commit
dde8cd8f72
3 changed files with 182 additions and 306 deletions
|
@ -1,155 +0,0 @@
|
||||||
package com.earth2me.essentials.commands;
|
|
||||||
|
|
||||||
import com.earth2me.essentials.CommandSource;
|
|
||||||
import static com.earth2me.essentials.I18n._;
|
|
||||||
import com.earth2me.essentials.Mob;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Locale;
|
|
||||||
import org.bukkit.Chunk;
|
|
||||||
import org.bukkit.Server;
|
|
||||||
import org.bukkit.World;
|
|
||||||
import org.bukkit.entity.*;
|
|
||||||
import org.bukkit.event.entity.EntityDeathEvent;
|
|
||||||
import org.bukkit.inventory.ItemStack;
|
|
||||||
|
|
||||||
|
|
||||||
public class Commandkillall extends EssentialsCommand
|
|
||||||
{
|
|
||||||
public Commandkillall()
|
|
||||||
{
|
|
||||||
super("killall");
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO: Tidy - missed this during command cleanup
|
|
||||||
@Override
|
|
||||||
public void run(Server server, CommandSource sender, String commandLabel, String[] args) throws Exception
|
|
||||||
{
|
|
||||||
String type = "all";
|
|
||||||
int radius = -1;
|
|
||||||
World world;
|
|
||||||
if (sender.isPlayer())
|
|
||||||
{
|
|
||||||
world = sender.getPlayer().getWorld();
|
|
||||||
if (args.length == 1)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
radius = Integer.parseInt(args[0]);
|
|
||||||
}
|
|
||||||
catch (NumberFormatException e1)
|
|
||||||
{
|
|
||||||
type = args[0];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (args.length > 1)
|
|
||||||
{
|
|
||||||
type = args[0];
|
|
||||||
try
|
|
||||||
{
|
|
||||||
radius = Integer.parseInt(args[1]);
|
|
||||||
}
|
|
||||||
catch (NumberFormatException e)
|
|
||||||
{
|
|
||||||
throw new Exception(_("numberRequired"), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (args.length > 2)
|
|
||||||
{
|
|
||||||
world = ess.getWorld(args[2]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (args.length == 0)
|
|
||||||
{
|
|
||||||
throw new NotEnoughArgumentsException();
|
|
||||||
}
|
|
||||||
else if (args.length == 1)
|
|
||||||
{
|
|
||||||
world = ess.getWorld(args[0]);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
type = args[0];
|
|
||||||
world = ess.getWorld(args[1]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (radius >= 0)
|
|
||||||
{
|
|
||||||
radius *= radius;
|
|
||||||
}
|
|
||||||
String killType = type.toLowerCase(Locale.ENGLISH);
|
|
||||||
boolean animals = killType.startsWith("animal");
|
|
||||||
boolean monster = killType.startsWith("monster") || killType.startsWith("mob");
|
|
||||||
boolean all = killType.equals("all");
|
|
||||||
Class<? extends Entity> entityClass = null;
|
|
||||||
if (!animals && !monster && !all)
|
|
||||||
{
|
|
||||||
if (Mob.fromName(killType) == null)
|
|
||||||
{
|
|
||||||
throw new Exception(_("invalidMob"));
|
|
||||||
}
|
|
||||||
entityClass = Mob.fromName(killType).getType().getEntityClass();
|
|
||||||
}
|
|
||||||
int numKills = 0;
|
|
||||||
for (Chunk chunk : world.getLoadedChunks())
|
|
||||||
{
|
|
||||||
for (Entity entity : chunk.getEntities())
|
|
||||||
{
|
|
||||||
if (sender.isPlayer())
|
|
||||||
{
|
|
||||||
if (radius >= 0 && sender.getPlayer().getLocation().distanceSquared(entity.getLocation()) > radius)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (entity instanceof LivingEntity == false || entity instanceof HumanEntity)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (entity instanceof Tameable)
|
|
||||||
{
|
|
||||||
if (((Tameable)entity).isTamed())
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (animals)
|
|
||||||
{
|
|
||||||
if (entity instanceof Animals || entity instanceof NPC || entity instanceof Snowman || entity instanceof WaterMob)
|
|
||||||
{
|
|
||||||
EntityDeathEvent event = new EntityDeathEvent((LivingEntity)entity, new ArrayList<ItemStack>(0));
|
|
||||||
ess.getServer().getPluginManager().callEvent(event);
|
|
||||||
entity.remove();
|
|
||||||
numKills++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (monster)
|
|
||||||
{
|
|
||||||
if (entity instanceof Monster || entity instanceof ComplexLivingEntity || entity instanceof Flying || entity instanceof Slime)
|
|
||||||
{
|
|
||||||
EntityDeathEvent event = new EntityDeathEvent((LivingEntity)entity, new ArrayList<ItemStack>(0));
|
|
||||||
ess.getServer().getPluginManager().callEvent(event);
|
|
||||||
entity.remove();
|
|
||||||
numKills++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (all)
|
|
||||||
{
|
|
||||||
EntityDeathEvent event = new EntityDeathEvent((LivingEntity)entity, new ArrayList<ItemStack>(0));
|
|
||||||
ess.getServer().getPluginManager().callEvent(event);
|
|
||||||
entity.remove();
|
|
||||||
numKills++;
|
|
||||||
}
|
|
||||||
else if (entityClass != null && entityClass.isAssignableFrom(entity.getClass()))
|
|
||||||
{
|
|
||||||
EntityDeathEvent event = new EntityDeathEvent((LivingEntity)entity, new ArrayList<ItemStack>(0));
|
|
||||||
ess.getServer().getPluginManager().callEvent(event);
|
|
||||||
entity.remove();
|
|
||||||
numKills++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sender.sendMessage(_("kill", numKills));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -2,15 +2,19 @@ package com.earth2me.essentials.commands;
|
||||||
|
|
||||||
import com.earth2me.essentials.CommandSource;
|
import com.earth2me.essentials.CommandSource;
|
||||||
import static com.earth2me.essentials.I18n._;
|
import static com.earth2me.essentials.I18n._;
|
||||||
|
import com.earth2me.essentials.Mob;
|
||||||
import com.earth2me.essentials.User;
|
import com.earth2me.essentials.User;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Chunk;
|
import org.bukkit.Chunk;
|
||||||
import org.bukkit.Server;
|
import org.bukkit.Server;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.entity.*;
|
import org.bukkit.entity.*;
|
||||||
|
|
||||||
//Todo: Fix this up
|
// This could be rewritten in a simpler form if we made a mapping of all Entity names to their types (which would also provide possible mod support)
|
||||||
//Todo: now overlaps some functions of killall, which should be deprecated and removed once all functions are covered
|
|
||||||
public class Commandremove extends EssentialsCommand
|
public class Commandremove extends EssentialsCommand
|
||||||
{
|
{
|
||||||
public Commandremove()
|
public Commandremove()
|
||||||
|
@ -21,14 +25,9 @@ public class Commandremove extends EssentialsCommand
|
||||||
@Override
|
@Override
|
||||||
protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
|
protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
|
||||||
{
|
{
|
||||||
if (args.length < 1)
|
|
||||||
{
|
|
||||||
throw new NotEnoughArgumentsException();
|
|
||||||
}
|
|
||||||
ToRemove toRemove;
|
|
||||||
World world = user.getWorld();
|
World world = user.getWorld();
|
||||||
int radius = 0;
|
int radius = 0;
|
||||||
|
Bukkit.broadcastMessage("len: " + args.length);
|
||||||
if (args.length >= 2)
|
if (args.length >= 2)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
@ -40,28 +39,12 @@ public class Commandremove extends EssentialsCommand
|
||||||
throw new Exception(_("numberRequired"), e);
|
throw new Exception(_("numberRequired"), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (args.length >= 3)
|
if (args.length >= 3)
|
||||||
{
|
{
|
||||||
world = ess.getWorld(args[2]);
|
world = ess.getWorld(args[2]);
|
||||||
}
|
}
|
||||||
|
parseCommand(server, user.getSource(), args, world, radius);
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
toRemove = ToRemove.valueOf(args[0].toUpperCase(Locale.ENGLISH));
|
|
||||||
}
|
|
||||||
catch (IllegalArgumentException e)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
toRemove = ToRemove.valueOf(args[0].concat("S").toUpperCase(Locale.ENGLISH));
|
|
||||||
}
|
|
||||||
catch (IllegalArgumentException ee)
|
|
||||||
{
|
|
||||||
throw new NotEnoughArgumentsException(ee); //TODO: translate and list types
|
|
||||||
}
|
|
||||||
}
|
|
||||||
removeEntities(user.getSource(), world, toRemove, radius);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -72,33 +55,60 @@ public class Commandremove extends EssentialsCommand
|
||||||
throw new NotEnoughArgumentsException();
|
throw new NotEnoughArgumentsException();
|
||||||
}
|
}
|
||||||
World world = ess.getWorld(args[1]);
|
World world = ess.getWorld(args[1]);
|
||||||
|
parseCommand(server, sender, args, world, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void parseCommand(Server server, CommandSource sender, String[] args, World world, int radius) throws Exception
|
||||||
|
{
|
||||||
|
List<String> types = new ArrayList<String>();
|
||||||
|
List<String> customTypes = new ArrayList<String>();
|
||||||
|
|
||||||
|
if (args.length > 0 && (args[0].contentEquals("*") || args[0].contentEquals("all")))
|
||||||
|
{
|
||||||
|
types.add(0, "ALL");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (String s : args[0].split(","))
|
||||||
|
{
|
||||||
ToRemove toRemove;
|
ToRemove toRemove;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
toRemove = ToRemove.valueOf(args[0].toUpperCase(Locale.ENGLISH));
|
toRemove = ToRemove.valueOf(s.toUpperCase(Locale.ENGLISH));
|
||||||
}
|
}
|
||||||
catch (IllegalArgumentException e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
toRemove = ToRemove.valueOf(args[0].concat("S").toUpperCase(Locale.ENGLISH));
|
toRemove = ToRemove.valueOf(s.concat("S").toUpperCase(Locale.ENGLISH));
|
||||||
}
|
}
|
||||||
catch (IllegalArgumentException ee)
|
catch (Exception ee)
|
||||||
{
|
{
|
||||||
throw new NotEnoughArgumentsException(ee); //TODO: translate and list types
|
toRemove = ToRemove.CUSTOM;
|
||||||
|
customTypes.add(s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
removeEntities(sender, world, toRemove, 0);
|
types.add(toRemove.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
removeHandler(sender, types, customTypes, world, radius);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void removeEntities(final CommandSource sender, final World world, final ToRemove toRemove, int radius) throws Exception
|
private void removeHandler(CommandSource sender, List<String> types, List<String> customTypes, World world, int radius)
|
||||||
{
|
{
|
||||||
int removed = 0;
|
int removed = 0;
|
||||||
if (radius > 0)
|
if (radius > 0)
|
||||||
{
|
{
|
||||||
radius *= radius;
|
radius *= radius;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ArrayList<ToRemove> removeTypes = new ArrayList<ToRemove>();
|
||||||
|
|
||||||
|
for (String s : types)
|
||||||
|
{
|
||||||
|
removeTypes.add(ToRemove.valueOf(s));
|
||||||
|
}
|
||||||
|
|
||||||
for (Chunk chunk : world.getLoadedChunks())
|
for (Chunk chunk : world.getLoadedChunks())
|
||||||
{
|
{
|
||||||
for (Entity e : chunk.getEntities())
|
for (Entity e : chunk.getEntities())
|
||||||
|
@ -110,13 +120,27 @@ public class Commandremove extends EssentialsCommand
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (e instanceof Tameable)
|
if (e instanceof HumanEntity)
|
||||||
{
|
{
|
||||||
if (((Tameable)e).isTamed())
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (ToRemove toRemove : removeTypes)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (e instanceof Tameable && ((Tameable)e).isTamed())
|
||||||
|
{
|
||||||
|
if (toRemove == ToRemove.TAMED)
|
||||||
|
{
|
||||||
|
e.remove();
|
||||||
|
removed++;
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (toRemove)
|
switch (toRemove)
|
||||||
{
|
{
|
||||||
case DROPS:
|
case DROPS:
|
||||||
|
@ -193,7 +217,7 @@ public class Commandremove extends EssentialsCommand
|
||||||
break;
|
break;
|
||||||
case PASSIVE:
|
case PASSIVE:
|
||||||
case ANIMALS:
|
case ANIMALS:
|
||||||
if (e instanceof Animals || e instanceof NPC || e instanceof Snowman || e instanceof WaterMob)
|
if (e instanceof Animals || e instanceof NPC || e instanceof Snowman || e instanceof WaterMob || e instanceof Ambient)
|
||||||
{
|
{
|
||||||
e.remove();
|
e.remove();
|
||||||
removed++;
|
removed++;
|
||||||
|
@ -201,23 +225,31 @@ public class Commandremove extends EssentialsCommand
|
||||||
break;
|
break;
|
||||||
case MOBS:
|
case MOBS:
|
||||||
if (e instanceof Animals || e instanceof NPC || e instanceof Snowman || e instanceof WaterMob
|
if (e instanceof Animals || e instanceof NPC || e instanceof Snowman || e instanceof WaterMob
|
||||||
|| e instanceof Monster || e instanceof ComplexLivingEntity || e instanceof Flying || e instanceof Slime)
|
|| e instanceof Monster || e instanceof ComplexLivingEntity || e instanceof Flying || e instanceof Slime || e instanceof Ambient)
|
||||||
{
|
{
|
||||||
e.remove();
|
e.remove();
|
||||||
removed++;
|
removed++;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ENTITIES:
|
case ENTITIES:
|
||||||
|
case ALL:
|
||||||
if (e instanceof Entity)
|
if (e instanceof Entity)
|
||||||
{
|
{
|
||||||
if (e instanceof HumanEntity)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
e.remove();
|
e.remove();
|
||||||
removed++;
|
removed++;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case CUSTOM:
|
||||||
|
for (String type : customTypes)
|
||||||
|
{
|
||||||
|
if (e.getType() == Mob.fromName(type).getType())
|
||||||
|
{
|
||||||
|
e.remove();
|
||||||
|
removed++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -241,6 +273,9 @@ public class Commandremove extends EssentialsCommand
|
||||||
ANIMALS,
|
ANIMALS,
|
||||||
AMBIENT,
|
AMBIENT,
|
||||||
MOBS,
|
MOBS,
|
||||||
ENTITIES
|
ENTITIES,
|
||||||
|
ALL,
|
||||||
|
CUSTOM,
|
||||||
|
TAMED
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -206,10 +206,6 @@ commands:
|
||||||
description: Kills specified player.
|
description: Kills specified player.
|
||||||
usage: /<command> <player>
|
usage: /<command> <player>
|
||||||
aliases: [ekill]
|
aliases: [ekill]
|
||||||
killall:
|
|
||||||
description: Kill all mobs in a world.
|
|
||||||
usage: /<command> [mobType] [radius] [world]
|
|
||||||
aliases: [butcher,ebutcher,ekillall,mobkill,emobkill]
|
|
||||||
kit:
|
kit:
|
||||||
description: Obtains the specified kit or views all available kits.
|
description: Obtains the specified kit or views all available kits.
|
||||||
usage: /<command> [kit] [player]
|
usage: /<command> [kit] [player]
|
||||||
|
@ -305,7 +301,7 @@ commands:
|
||||||
remove:
|
remove:
|
||||||
description: Removes entities in your world.
|
description: Removes entities in your world.
|
||||||
usage: /<command> <drops|arrows|boats|minecarts|xp|paintings|itemframes|endercrystals|hostile|monsters|passive|animals|ambient|mobs|entities> [radius] [world]
|
usage: /<command> <drops|arrows|boats|minecarts|xp|paintings|itemframes|endercrystals|hostile|monsters|passive|animals|ambient|mobs|entities> [radius] [world]
|
||||||
aliases: [eremove]
|
aliases: [eremove,butcher,ebutcher,killall,ekillall,mobkill,emobkill]
|
||||||
repair:
|
repair:
|
||||||
description: Repairs the durability of one or all items.
|
description: Repairs the durability of one or all items.
|
||||||
usage: /<command> [hand|all]
|
usage: /<command> [hand|all]
|
||||||
|
|
Loading…
Reference in a new issue