Search in sources :

Example 76 with MageController

use of com.elmakers.mine.bukkit.api.magic.MageController in project MagicPlugin by elBukkit.

the class MagicMobCommandExecutor method onCommand.

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    if (!api.hasPermission(sender, "Magic.commands.mmob")) {
        sendNoPermission(sender);
        return true;
    }
    if (args.length == 0) {
        sender.sendMessage(ChatColor.RED + "Usage: mmob [spawn|list] <type> [count]");
        return true;
    }
    if (args[0].equalsIgnoreCase("list")) {
        onListMobs(sender);
        return true;
    }
    if (args[0].equalsIgnoreCase("clear")) {
        onClearMobs(sender, args);
        return true;
    }
    if (!args[0].equalsIgnoreCase("spawn") || args.length < 2) {
        return false;
    }
    if (!(sender instanceof Player) && !(sender instanceof BlockCommandSender) && args.length < 6) {
        sender.sendMessage(ChatColor.RED + "Usage: mmob spawn <type> <x> <y> <z> <world> [count]");
        return true;
    }
    Location targetLocation = null;
    World targetWorld = null;
    Player player = (sender instanceof Player) ? (Player) sender : null;
    BlockCommandSender commandBlock = (sender instanceof BlockCommandSender) ? (BlockCommandSender) sender : null;
    if (args.length >= 6) {
        targetWorld = Bukkit.getWorld(args[5]);
        if (targetWorld == null) {
            sender.sendMessage(ChatColor.RED + "Invalid world: " + ChatColor.GRAY + args[5]);
            return true;
        }
    } else if (player != null) {
        targetWorld = player.getWorld();
    } else if (commandBlock != null) {
        Block block = commandBlock.getBlock();
        targetWorld = block.getWorld();
        targetLocation = block.getLocation();
    }
    if (args.length >= 5) {
        try {
            double currentX = 0;
            double currentY = 0;
            double currentZ = 0;
            if (player != null) {
                Location currentLocation = player.getLocation();
                currentX = currentLocation.getX();
                currentY = currentLocation.getY();
                currentZ = currentLocation.getZ();
            } else if (commandBlock != null) {
                Block blockLocation = commandBlock.getBlock();
                currentX = blockLocation.getX();
                currentY = blockLocation.getY();
                currentZ = blockLocation.getZ();
            }
            targetLocation = new Location(targetWorld, ConfigurationUtils.overrideDouble(args[2], currentX), ConfigurationUtils.overrideDouble(args[3], currentY), ConfigurationUtils.overrideDouble(args[4], currentZ));
        } catch (Exception ex) {
            sender.sendMessage(ChatColor.RED + "Usage: mmob spawn <type> <x> <y> <z> <world> [count]");
            return true;
        }
    } else if (player != null) {
        Location location = player.getEyeLocation();
        BlockIterator iterator = new BlockIterator(location.getWorld(), location.toVector(), location.getDirection(), 0, 64);
        Block block = location.getBlock();
        while (block.getType() == Material.AIR && iterator.hasNext()) {
            block = iterator.next();
        }
        block = block.getRelative(BlockFace.UP);
        targetLocation = block.getLocation();
    }
    if (targetLocation == null || targetLocation.getWorld() == null) {
        sender.sendMessage(ChatColor.RED + "Usage: mmob spawn <type> <x> <y> <z> <world> [count]");
        return true;
    }
    String mobKey = args[1];
    int count = 1;
    String countString = null;
    if (args.length == 7) {
        countString = args[6];
    } else if (args.length == 3) {
        countString = args[2];
    }
    if (countString != null) {
        try {
            count = Integer.parseInt(countString);
        } catch (Exception ex) {
            sender.sendMessage(ChatColor.RED + "Invalid count: " + countString);
            return true;
        }
    }
    if (count <= 0)
        return true;
    MageController controller = api.getController();
    Entity spawned = null;
    for (int i = 0; i < count; i++) {
        spawned = controller.spawnMob(mobKey, targetLocation);
    }
    if (spawned == null) {
        sender.sendMessage(ChatColor.RED + "Unknown mob type " + mobKey);
        return true;
    }
    String name = spawned.getName();
    if (name == null) {
        name = mobKey;
    }
    sender.sendMessage(ChatColor.AQUA + "Spawned mob: " + ChatColor.LIGHT_PURPLE + name);
    return true;
}
Also used : BlockIterator(org.bukkit.util.BlockIterator) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) Block(org.bukkit.block.Block) World(org.bukkit.World) BlockCommandSender(org.bukkit.command.BlockCommandSender) Location(org.bukkit.Location)

Aggregations

MageController (com.elmakers.mine.bukkit.api.magic.MageController)76 Mage (com.elmakers.mine.bukkit.api.magic.Mage)45 Entity (org.bukkit.entity.Entity)27 Player (org.bukkit.entity.Player)26 Location (org.bukkit.Location)16 ItemStack (org.bukkit.inventory.ItemStack)16 Wand (com.elmakers.mine.bukkit.api.wand.Wand)14 Block (org.bukkit.block.Block)11 LivingEntity (org.bukkit.entity.LivingEntity)11 ArrayList (java.util.ArrayList)10 Inventory (org.bukkit.inventory.Inventory)9 ItemMeta (org.bukkit.inventory.meta.ItemMeta)8 Spell (com.elmakers.mine.bukkit.api.spell.Spell)5 MagicController (com.elmakers.mine.bukkit.magic.MagicController)5 Nullable (javax.annotation.Nullable)5 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)5 ItemData (com.elmakers.mine.bukkit.api.item.ItemData)4 SpellTemplate (com.elmakers.mine.bukkit.api.spell.SpellTemplate)4 WandUpgradePath (com.elmakers.mine.bukkit.api.wand.WandUpgradePath)4 File (java.io.File)4