Search in sources :

Example 11 with BlockCommandSender

use of org.bukkit.command.BlockCommandSender in project MagicPlugin by elBukkit.

the class MagicController method cast.

public boolean cast(Mage mage, String spellName, ConfigurationSection parameters, CommandSender sender, Entity entity) {
    Player usePermissions = (sender == entity && entity instanceof Player) ? (Player) entity : (sender instanceof Player ? (Player) sender : null);
    if (entity == null && sender instanceof Player) {
        entity = (Player) sender;
    }
    Location targetLocation = null;
    if (mage == null) {
        CommandSender mageController = (entity != null && entity instanceof Player) ? (Player) entity : sender;
        if (sender != null && sender instanceof BlockCommandSender) {
            targetLocation = ((BlockCommandSender) sender).getBlock().getLocation();
        }
        if (entity == null) {
            mage = getMage(mageController);
        } else {
            mage = getMageFromEntity(entity, mageController);
        }
    }
    // This is a bit of a hack to make automata maintain direction
    if (targetLocation != null) {
        Location mageLocation = mage.getLocation();
        if (mageLocation != null) {
            targetLocation.setPitch(mageLocation.getPitch());
            targetLocation.setYaw(mageLocation.getYaw());
        }
    }
    SpellTemplate template = getSpellTemplate(spellName);
    if (template == null || !template.hasCastPermission(usePermissions)) {
        if (sender != null) {
            sender.sendMessage("Spell " + spellName + " unknown");
        }
        return false;
    }
    com.elmakers.mine.bukkit.api.spell.Spell spell = mage.getSpell(spellName);
    if (spell == null) {
        if (sender != null) {
            sender.sendMessage("Spell " + spellName + " unknown");
        }
        return false;
    }
    // TODO: Load configured list of parameters!
    // Make it free and skip cooldowns, if configured to do so.
    toggleCastCommandOverrides(mage, sender, true);
    boolean success = false;
    try {
        success = spell.cast(parameters, targetLocation);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    toggleCastCommandOverrides(mage, sender, false);
    return success;
}
Also used : EffectPlayer(com.elmakers.mine.bukkit.effect.EffectPlayer) Player(org.bukkit.entity.Player) CommandSender(org.bukkit.command.CommandSender) BlockCommandSender(org.bukkit.command.BlockCommandSender) Spell(com.elmakers.mine.bukkit.api.spell.Spell) InvalidConfigurationException(org.bukkit.configuration.InvalidConfigurationException) IOException(java.io.IOException) ParseException(java.text.ParseException) Location(org.bukkit.Location) CastSourceLocation(com.elmakers.mine.bukkit.api.magic.CastSourceLocation) BlockCommandSender(org.bukkit.command.BlockCommandSender) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate)

Example 12 with BlockCommandSender

use of org.bukkit.command.BlockCommandSender in project MagicPlugin by elBukkit.

the class Mage method setCommandSender.

protected void setCommandSender(CommandSender sender) {
    if (sender != null) {
        this.commandSenderRef = new WeakReference<>(sender);
        if (sender instanceof BlockCommandSender) {
            BlockCommandSender commandBlock = (BlockCommandSender) sender;
            playerName = commandBlock.getName();
            Location location = getLocation();
            if (location == null) {
                location = commandBlock.getBlock().getLocation();
            } else {
                Location blockLocation = commandBlock.getBlock().getLocation();
                location.setX(blockLocation.getX());
                location.setY(blockLocation.getY());
                location.setZ(blockLocation.getZ());
            }
            setLocation(location, false);
        } else {
            setLocation(null);
        }
    } else {
        this.commandSenderRef.clear();
        setLocation(null);
    }
}
Also used : BlockCommandSender(org.bukkit.command.BlockCommandSender) Location(org.bukkit.Location) CastSourceLocation(com.elmakers.mine.bukkit.api.magic.CastSourceLocation)

Example 13 with BlockCommandSender

use of org.bukkit.command.BlockCommandSender in project MagicPlugin by elBukkit.

the class Mage method isOnline.

@Override
public boolean isOnline() {
    Player player = getPlayer();
    if (player != null) {
        return player.isOnline();
    }
    // Check for automata
    CommandSender sender = getCommandSender();
    if (sender == null || !(sender instanceof BlockCommandSender))
        return true;
    return lastCast > System.currentTimeMillis() - AUTOMATA_ONLINE_TIMEOUT;
}
Also used : Player(org.bukkit.entity.Player) CommandSender(org.bukkit.command.CommandSender) BlockCommandSender(org.bukkit.command.BlockCommandSender) BlockCommandSender(org.bukkit.command.BlockCommandSender)

Example 14 with BlockCommandSender

use of org.bukkit.command.BlockCommandSender in project MagicPlugin by elBukkit.

the class Targeting method findTarget.

/**
 * Returns the block at the cursor, or null if out of range
 *
 * @return The target block
 */
protected Target findTarget(CastContext context, double range) {
    if (targetType == TargetType.NONE) {
        return new Target(source);
    }
    boolean isBlock = targetType == TargetType.BLOCK || targetType == TargetType.SELECT;
    Mage mage = context.getMage();
    final Entity mageEntity = mage.getEntity();
    if (targetType == TargetType.SELF && mageEntity != null) {
        result = TargetingResult.ENTITY;
        return new Target(source, mageEntity);
    }
    CommandSender sender = mage.getCommandSender();
    if (targetType == TargetType.SELF && mageEntity == null && sender != null && (sender instanceof BlockCommandSender)) {
        BlockCommandSender commandBlock = (BlockCommandSender) mage.getCommandSender();
        return new Target(commandBlock.getBlock().getLocation(), commandBlock.getBlock());
    }
    if (targetType == TargetType.SELF && source != null) {
        return new Target(source, source.getBlock());
    }
    if (targetType == TargetType.SELF) {
        return new Target(source);
    }
    Block block = null;
    if (!ignoreBlocks) {
        findTargetBlock(context, range);
        block = currentBlock;
    }
    if (isBlock) {
        return new Target(source, block, useHitbox, hitboxBlockPadding);
    }
    Target targetBlock = block == null ? null : new Target(source, block, useHitbox, hitboxBlockPadding);
    // Don't target entities beyond the block we just hit
    if (targetBlock != null && source != null && source.getWorld().equals(block.getWorld())) {
        range = Math.min(range, source.distance(targetBlock.getLocation()));
    }
    // Pick the closest candidate entity
    Target entityTarget = null;
    List<Target> scored = getAllTargetEntities(context, range);
    if (scored.size() > 0) {
        entityTarget = scored.get(0);
    }
    // Don't allow targeting entities in an area you couldn't cast the spell in
    if (entityTarget != null && !context.canCast(entityTarget.getLocation())) {
        entityTarget = null;
    }
    if (targetBlock != null && !context.canCast(targetBlock.getLocation())) {
        result = TargetingResult.MISS;
        targetBlock = null;
    }
    if (targetType == TargetType.OTHER_ENTITY && entityTarget == null) {
        result = TargetingResult.MISS;
        return new Target(source);
    }
    if (targetType == TargetType.ANY_ENTITY && entityTarget == null) {
        result = TargetingResult.ENTITY;
        return new Target(source, mageEntity);
    }
    if (entityTarget == null && targetType == TargetType.ANY && mageEntity != null) {
        result = TargetingResult.ENTITY;
        return new Target(source, mageEntity, targetBlock == null ? null : targetBlock.getBlock());
    }
    if (targetBlock != null && entityTarget != null) {
        if (targetBlock.getDistanceSquared() < entityTarget.getDistanceSquared() - hitboxPadding * hitboxPadding) {
            entityTarget = null;
        } else {
            targetBlock = null;
        }
    }
    if (entityTarget != null) {
        result = TargetingResult.ENTITY;
        return entityTarget;
    } else if (targetBlock != null) {
        return targetBlock;
    }
    result = TargetingResult.MISS;
    return new Target(source);
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Mage(com.elmakers.mine.bukkit.api.magic.Mage) Block(org.bukkit.block.Block) CommandSender(org.bukkit.command.CommandSender) BlockCommandSender(org.bukkit.command.BlockCommandSender) BlockCommandSender(org.bukkit.command.BlockCommandSender)

Example 15 with BlockCommandSender

use of org.bukkit.command.BlockCommandSender 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

BlockCommandSender (org.bukkit.command.BlockCommandSender)25 Player (org.bukkit.entity.Player)19 Location (org.bukkit.Location)13 Entity (org.bukkit.entity.Entity)10 ConsoleCommandSender (org.bukkit.command.ConsoleCommandSender)8 Block (org.bukkit.block.Block)5 CommandSender (org.bukkit.command.CommandSender)5 EventHandler (org.bukkit.event.EventHandler)5 ArrayList (java.util.ArrayList)4 World (org.bukkit.World)4 CommandBlock (org.bukkit.block.CommandBlock)4 CommandMinecart (org.bukkit.entity.minecart.CommandMinecart)4 CommandTarget (net.glowstone.command.CommandTarget)3 LivingEntity (org.bukkit.entity.LivingEntity)3 CastSourceLocation (com.elmakers.mine.bukkit.api.magic.CastSourceLocation)2 Mage (com.elmakers.mine.bukkit.api.magic.Mage)2 Spell (com.elmakers.mine.bukkit.api.spell.Spell)2 MCCommandSender (com.laytonsmith.abstraction.MCCommandSender)2 List (java.util.List)2 LocalizedStringImpl (net.glowstone.i18n.LocalizedStringImpl)2