TF-EssentialsX/Essentials/src/com/earth2me/essentials/commands/Commandremove.java

304 lines
6 KiB
Java
Raw Normal View History

2011-12-02 19:00:29 -05:00
package com.earth2me.essentials.commands;
import com.earth2me.essentials.CommandSource;
2011-12-02 19:00:29 -05:00
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.Mob;
2011-12-03 21:14:03 +00:00
import com.earth2me.essentials.User;
import java.util.ArrayList;
import java.util.List;
2011-12-03 21:14:03 +00:00
import java.util.Locale;
2011-12-02 19:00:29 -05:00
import org.bukkit.Chunk;
import org.bukkit.Server;
import org.bukkit.World;
2011-12-03 21:14:03 +00:00
import org.bukkit.entity.*;
2011-12-02 19:00:29 -05:00
// 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)
2011-12-02 19:00:29 -05:00
public class Commandremove extends EssentialsCommand
{
public Commandremove()
{
super("remove");
}
2011-12-02 19:00:29 -05:00
@Override
2011-12-03 21:14:03 +00:00
protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
2011-12-02 19:00:29 -05:00
{
World world = user.getWorld();
2011-12-03 21:14:03 +00:00
int radius = 0;
2013-12-07 18:46:25 +00:00
if (args.length < 1)
{
throw new NotEnoughArgumentsException();
}
if (args.length >= 2)
2011-12-02 19:00:29 -05:00
{
try
{
radius = Integer.parseInt(args[1]);
}
catch (NumberFormatException e)
{
throw new Exception(_("numberRequired"), e);
2011-12-02 19:00:29 -05:00
}
}
if (args.length >= 3)
{
world = ess.getWorld(args[2]);
}
parseCommand(server, user.getSource(), args, world, radius);
2011-12-03 21:14:03 +00:00
}
2011-12-03 21:14:03 +00:00
@Override
protected void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception
2011-12-03 21:14:03 +00:00
{
if (args.length < 2)
{
throw new NotEnoughArgumentsException();
}
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>();
2013-12-07 18:46:25 +00:00
if (args[0].contentEquals("*") || args[0].contentEquals("all"))
2011-12-02 19:00:29 -05:00
{
types.add(0, "ALL");
2011-12-02 19:00:29 -05:00
}
else
2011-12-02 19:00:29 -05:00
{
for (String entityType : args[0].split(","))
{
ToRemove toRemove;
try
{
toRemove = ToRemove.valueOf(entityType.toUpperCase(Locale.ENGLISH));
}
catch (Exception e)
{
try
{
toRemove = ToRemove.valueOf(entityType.concat("S").toUpperCase(Locale.ENGLISH));
}
catch (Exception ee)
{
toRemove = ToRemove.CUSTOM;
customTypes.add(entityType);
}
}
types.add(toRemove.toString());
}
2011-12-02 19:00:29 -05:00
}
removeHandler(sender, types, customTypes, world, radius);
2011-12-03 21:14:03 +00:00
}
private void removeHandler(CommandSource sender, List<String> types, List<String> customTypes, World world, int radius)
2011-12-03 21:14:03 +00:00
{
2011-12-02 19:00:29 -05:00
int removed = 0;
if (radius > 0)
{
radius *= radius;
2011-12-04 22:01:50 +01:00
}
ArrayList<ToRemove> removeTypes = new ArrayList<ToRemove>();
ArrayList<Mob> customRemoveTypes = new ArrayList<Mob>();
for (String s : types)
{
removeTypes.add(ToRemove.valueOf(s));
}
boolean warnUser = false;
for (String s : customTypes)
{
Mob mobType = Mob.fromName(s);
if (mobType == null)
{
warnUser = true;
}
else
{
customRemoveTypes.add(mobType);
}
}
if (warnUser)
{
sender.sendMessage(_("invalidMob"));
}
2011-12-02 19:00:29 -05:00
for (Chunk chunk : world.getLoadedChunks())
{
for (Entity e : chunk.getEntities())
{
2011-12-03 21:14:03 +00:00
if (radius > 0)
2011-12-02 19:00:29 -05:00
{
2013-10-18 00:04:17 +01:00
if (sender.getPlayer().getLocation().distanceSquared(e.getLocation()) > radius)
2011-12-02 19:00:29 -05:00
{
continue;
}
}
if (e instanceof HumanEntity)
2011-12-02 19:00:29 -05:00
{
continue;
}
for (ToRemove toRemove : removeTypes)
{
if (e instanceof Tameable && ((Tameable)e).isTamed())
{
if (toRemove == ToRemove.TAMED)
{
e.remove();
removed++;
}
else
{
continue;
}
}
switch (toRemove)
{
case DROPS:
if (e instanceof Item)
{
e.remove();
removed++;
}
;
break;
case ARROWS:
if (e instanceof Projectile)
{
e.remove();
removed++;
}
break;
case BOATS:
if (e instanceof Boat)
{
e.remove();
removed++;
}
break;
case MINECARTS:
if (e instanceof Minecart)
{
e.remove();
removed++;
}
break;
case XP:
if (e instanceof ExperienceOrb)
{
e.remove();
removed++;
}
break;
case PAINTINGS:
if (e instanceof Painting)
{
e.remove();
removed++;
}
break;
case ITEMFRAMES:
if (e instanceof ItemFrame)
{
e.remove();
removed++;
}
break;
case ENDERCRYSTALS:
if (e instanceof EnderCrystal)
{
e.remove();
removed++;
}
break;
case AMBIENT:
if (e instanceof Flying)
{
e.remove();
removed++;
}
break;
case HOSTILE:
case MONSTERS:
if (e instanceof Monster || e instanceof ComplexLivingEntity || e instanceof Flying || e instanceof Slime)
{
e.remove();
removed++;
}
break;
case PASSIVE:
case ANIMALS:
if (e instanceof Animals || e instanceof NPC || e instanceof Snowman || e instanceof WaterMob || e instanceof Ambient)
{
e.remove();
removed++;
}
break;
case MOBS:
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 Ambient)
{
e.remove();
removed++;
}
break;
case ENTITIES:
case ALL:
if (e instanceof Entity)
{
e.remove();
removed++;
}
break;
case CUSTOM:
for (Mob type : customRemoveTypes)
{
if (e.getType() == type.getType())
{
e.remove();
removed++;
}
}
break;
}
}
2011-12-02 19:00:29 -05:00
}
}
2011-12-03 21:14:03 +00:00
sender.sendMessage(_("removed", removed));
2011-12-02 19:00:29 -05:00
}
private enum ToRemove
{
DROPS,
ARROWS,
BOATS,
MINECARTS,
XP,
PAINTINGS,
ITEMFRAMES,
ENDERCRYSTALS,
HOSTILE,
MONSTERS,
PASSIVE,
ANIMALS,
AMBIENT,
MOBS,
ENTITIES,
ALL,
CUSTOM,
TAMED
}
}