Search in sources :

Example 21 with Mage

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

the class AnimateSpell method onCast.

@Override
public SpellResult onCast(ConfigurationSection parameters) {
    if (parameters.getString("animate", null) != null) {
        return super.onCast(parameters);
    }
    final Block targetBlock = getTargetBlock();
    if (targetBlock == null) {
        return SpellResult.NO_TARGET;
    }
    if (!hasBuildPermission(targetBlock)) {
        return SpellResult.INSUFFICIENT_PERMISSION;
    }
    int seedRadius = parameters.getInt("seed_radius", 0);
    MaterialAndData targetMaterial = new MaterialAndData(targetBlock);
    List<String> materials = ConfigurationUtils.getStringList(parameters, "materials");
    if (seedRadius > 0 && materials != null && !materials.isEmpty()) {
        targetMaterial = new MaterialAndData(RandomUtils.getRandom(materials));
    } else if (parameters.contains("material")) {
        targetMaterial = ConfigurationUtils.getMaterialAndData(parameters, "material", targetMaterial);
        if (targetMaterial.isValid()) {
            addDestructible(targetMaterial);
        }
    }
    if (!mage.isSuperPowered() && seedRadius == 0) {
        MaterialSetManager materialSets = controller.getMaterialSetManager();
        MaterialSet restricted = materialSets.getMaterialSet("restricted");
        if (restricted != null && restricted.testMaterialAndData(targetMaterial)) {
            return SpellResult.FAIL;
        }
        if (parameters.contains("restricted")) {
            String customRestricted = parameters.getString("restricted");
            if (customRestricted != null && !customRestricted.equals("restricted")) {
                restricted = materialSets.fromConfigEmpty(customRestricted);
                if (restricted.testMaterialAndData(targetMaterial)) {
                    return SpellResult.FAIL;
                }
            }
        }
    }
    if (!isDestructible(targetBlock)) {
        return SpellResult.INSUFFICIENT_PERMISSION;
    }
    registerForUndo(targetBlock);
    if (seedRadius > 0) {
        for (int dx = -seedRadius; dx < seedRadius; dx++) {
            for (int dz = -seedRadius; dz < seedRadius; dz++) {
                for (int dy = -seedRadius; dy < seedRadius; dy++) {
                    Block seedBlock = targetBlock.getRelative(dx, dy, dz);
                    if (isDestructible(seedBlock)) {
                        registerForUndo(seedBlock);
                        targetMaterial.modify(seedBlock);
                    }
                }
            }
        }
    }
    // Look for randomized levels
    int level = 0;
    if (parameters.contains("level")) {
        level = parameters.getInt("level", level);
    } else if (levelWeights != null) {
        level = RandomUtils.weightedRandom(levelWeights);
    }
    boolean simCheckDestructible = parameters.getBoolean("sim_check_destructible", true);
    simCheckDestructible = parameters.getBoolean("scd", simCheckDestructible);
    final ConfigurationSection automataParameters = new MemoryConfiguration();
    automataParameters.set("target", "self");
    automataParameters.set("cooldown", 0);
    automataParameters.set("m", targetMaterial.getKey());
    automataParameters.set("cd", (simCheckDestructible ? true : false));
    automataParameters.set("level", level);
    String automataName = parameters.getString("name", "Automata");
    Messages messages = controller.getMessages();
    String automataType = parameters.getString("message_type", "evil");
    List<String> prefixes = messages.getAll("automata." + automataType + ".prefixes");
    List<String> suffixes = messages.getAll("automata." + automataType + ".suffixes");
    automataName = prefixes.get(random.nextInt(prefixes.size())) + " " + automataName + " " + suffixes.get(random.nextInt(suffixes.size()));
    if (level > 1) {
        automataName += " " + escapeLevel(messages, "automata.level", level);
    }
    String message = getMessage("cast_broadcast").replace("$name", automataName);
    if (message.length() > 0) {
        controller.sendToMages(message, targetBlock.getLocation());
    }
    automataParameters.set("animate", automataName);
    String automataId = UUID.randomUUID().toString();
    final Mage mage = controller.getAutomaton(automataId, automataName);
    mage.setLocation(targetBlock.getLocation());
    mage.setQuiet(true);
    mage.addTag(getKey());
    final Spell spell = mage.getSpell(getKey());
    Bukkit.getScheduler().runTaskLater(controller.getPlugin(), new Runnable() {

        @Override
        public void run() {
            spell.cast(automataParameters);
        }
    }, 1);
    registerForUndo();
    return SpellResult.CAST;
}
Also used : Messages(com.elmakers.mine.bukkit.api.magic.Messages) MaterialSet(com.elmakers.mine.bukkit.api.magic.MaterialSet) MemoryConfiguration(org.bukkit.configuration.MemoryConfiguration) Spell(com.elmakers.mine.bukkit.api.spell.Spell) Mage(com.elmakers.mine.bukkit.api.magic.Mage) MaterialAndData(com.elmakers.mine.bukkit.block.MaterialAndData) Block(org.bukkit.block.Block) MaterialSetManager(com.elmakers.mine.bukkit.api.magic.MaterialSetManager) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 22 with Mage

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

the class MagicMobCommandExecutor method onClearMobs.

protected void onClearMobs(CommandSender sender, String[] args) {
    String mobType = args.length > 1 ? args[1] : null;
    if (mobType != null && mobType.equalsIgnoreCase("all")) {
        mobType = null;
    }
    String worldName = null;
    Integer radiusSquared = null;
    Location targetLocation = null;
    Player player = (sender instanceof Player) ? (Player) sender : null;
    BlockCommandSender commandBlock = (sender instanceof BlockCommandSender) ? (BlockCommandSender) sender : null;
    if (args.length == 3) {
        if (!(sender instanceof ConsoleCommandSender) && Bukkit.getWorld(args[2]) == null) {
            try {
                int radius = Integer.parseInt(args[2]);
                radiusSquared = radius * radius;
            } catch (Exception ignored) {
            }
        }
        if (radiusSquared == null) {
            worldName = args[2];
        } else {
            if (player != null) {
                targetLocation = player.getLocation();
            } else if (commandBlock != null) {
                targetLocation = commandBlock.getBlock().getLocation();
            } else {
                sender.sendMessage(ChatColor.RED + "Invalid world: " + args[2]);
            }
        }
    } else if (args.length > 5) {
        World world = null;
        if (args.length > 6) {
            worldName = args[6];
            world = Bukkit.getWorld(worldName);
        }
        if (world == null) {
            sender.sendMessage(ChatColor.RED + "Invalid world: " + worldName);
        }
        try {
            int radius = Integer.parseInt(args[2]);
            radiusSquared = radius * radius;
            double currentX = 0;
            double currentY = 0;
            double currentZ = 0;
            if (player != null) {
                targetLocation = player.getLocation();
            } else if (commandBlock != null) {
                targetLocation = commandBlock.getBlock().getLocation();
            }
            if (targetLocation != null) {
                currentX = targetLocation.getX();
                currentY = targetLocation.getY();
                currentZ = targetLocation.getZ();
                if (world == null) {
                    world = targetLocation.getWorld();
                    worldName = world.getName();
                }
            }
            if (world == null) {
                sender.sendMessage(ChatColor.RED + "Usage: mmob clear <type> <radius> <x> <y> <z> <world>");
                return;
            }
            targetLocation = new Location(world, ConfigurationUtils.overrideDouble(args[3], currentX), ConfigurationUtils.overrideDouble(args[4], currentY), ConfigurationUtils.overrideDouble(args[5], currentZ));
        } catch (Exception ex) {
            sender.sendMessage(ChatColor.RED + "Usage: mmob clear <type> <radius> <x> <y> <z> <world>");
            return;
        }
    }
    Collection<Mage> mages = new ArrayList<>(api.getController().getMobMages());
    int removed = 0;
    for (Mage mage : mages) {
        EntityData entityData = mage.getEntityData();
        if (entityData == null)
            continue;
        if (worldName != null && !mage.getLocation().getWorld().getName().equals(worldName))
            continue;
        if (mobType != null && !entityData.getKey().equals(mobType))
            continue;
        if (radiusSquared != null && targetLocation != null && mage.getLocation().distanceSquared(targetLocation) > radiusSquared)
            continue;
        Entity entity = mage.getEntity();
        mage.undoScheduled();
        api.getController().removeMage(mage);
        if (entity != null) {
            entity.remove();
        }
        removed++;
    }
    List<World> worlds = new ArrayList<>();
    if (worldName != null) {
        World world = Bukkit.getWorld(worldName);
        if (world == null) {
            sender.sendMessage(ChatColor.RED + "Unknown world: " + ChatColor.WHITE + worldName);
            return;
        }
        worlds.add(world);
    } else {
        worlds.addAll(Bukkit.getWorlds());
    }
    Set<String> mobNames = new HashSet<>();
    if (mobType != null) {
        EntityData mob = api.getController().getMob(mobType);
        if (mob == null) {
            sender.sendMessage(ChatColor.RED + "Unknown mob type: " + ChatColor.WHITE + mobType);
            return;
        }
        mobNames.add(mob.getName());
    } else {
        Set<String> allKeys = api.getController().getMobKeys();
        for (String key : allKeys) {
            EntityData mob = api.getController().getMob(key);
            mobNames.add(mob.getName());
        }
    }
    for (World world : worlds) {
        List<Entity> entities = world.getEntities();
        for (Entity entity : entities) {
            String customName = entity.getCustomName();
            if (entity.isValid() && customName != null && mobNames.contains(customName)) {
                if (radiusSquared != null && targetLocation != null && entity.getLocation().distanceSquared(targetLocation) > radiusSquared)
                    continue;
                entity.remove();
                removed++;
            }
        }
    }
    sender.sendMessage("Removed " + removed + " magic mobs");
}
Also used : Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) ArrayList(java.util.ArrayList) EntityData(com.elmakers.mine.bukkit.api.entity.EntityData) World(org.bukkit.World) ConsoleCommandSender(org.bukkit.command.ConsoleCommandSender) Mage(com.elmakers.mine.bukkit.api.magic.Mage) Location(org.bukkit.Location) BlockCommandSender(org.bukkit.command.BlockCommandSender) HashSet(java.util.HashSet)

Example 23 with Mage

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

the class MagicSaveCommandExecutor method onCommand.

@Override
public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) {
    Player player;
    if (args.length < 1) {
        return false;
    }
    if (sender instanceof Player) {
        player = (Player) sender;
        if (!player.hasPermission("Magic.commands.msave")) {
            return false;
        }
    }
    player = DeprecatedUtils.getPlayer(args[0]);
    if (player == null) {
        return false;
    }
    String executeCommand = "";
    for (int i = 1; i < args.length; i++) {
        executeCommand = executeCommand + args[i] + " ";
    }
    MagicController controller = (MagicController) api.getController();
    Mage mage = controller.getMage(player);
    final String cmd = executeCommand.trim().replace("@p", mage.getName());
    final Plugin plugin = controller.getPlugin();
    controller.saveMage(mage, true, new MageDataCallback() {

        @Override
        public void run(MageData data) {
            if (cmd.length() > 0) {
                plugin.getServer().dispatchCommand(sender, cmd);
            }
        }
    });
    return true;
}
Also used : Player(org.bukkit.entity.Player) MageDataCallback(com.elmakers.mine.bukkit.api.data.MageDataCallback) MageData(com.elmakers.mine.bukkit.api.data.MageData) Mage(com.elmakers.mine.bukkit.api.magic.Mage) MagicController(com.elmakers.mine.bukkit.magic.MagicController) Plugin(org.bukkit.plugin.Plugin)

Example 24 with Mage

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

the class MagicSkillsCommandExecutor method onCommand.

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    if (!api.hasPermission(sender, "Magic.commands.mskills")) {
        sendNoPermission(sender);
        return true;
    }
    if (!(sender instanceof Player)) {
        sender.sendMessage(ChatColor.RED + "This command may only be used in-game");
        return true;
    }
    Mage mage = controller.getMage(sender);
    SkillSelectorAction selector = new SkillSelectorAction();
    int page = 1;
    if (args.length > 0) {
        try {
            page = Integer.parseInt(args[0]);
        } catch (Exception ex) {
            sender.sendMessage(ChatColor.RED + "Expect page number, got " + args[0]);
            return true;
        }
    }
    selector.setPage(page);
    selector.perform(new CastContext(mage));
    return true;
}
Also used : Player(org.bukkit.entity.Player) CastContext(com.elmakers.mine.bukkit.action.CastContext) Mage(com.elmakers.mine.bukkit.api.magic.Mage) SkillSelectorAction(com.elmakers.mine.bukkit.action.builtin.SkillSelectorAction)

Example 25 with Mage

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

the class MagicController method sendPlayerToServer.

@Override
public void sendPlayerToServer(final Player player, final String server) {
    MageDataCallback callback = new MageDataCallback() {

        @Override
        public void run(MageData data) {
            Bukkit.getScheduler().runTaskLater(plugin, new ChangeServerTask(plugin, player, server), 1);
        }
    };
    info("Moving " + player.getName() + " to server " + server, 1);
    Mage mage = getRegisteredMage(player);
    if (mage != null) {
        playerQuit(mage, callback);
    } else {
        callback.run(null);
    }
}
Also used : MageDataCallback(com.elmakers.mine.bukkit.api.data.MageDataCallback) MageData(com.elmakers.mine.bukkit.api.data.MageData) Mage(com.elmakers.mine.bukkit.api.magic.Mage)

Aggregations

Mage (com.elmakers.mine.bukkit.api.magic.Mage)187 Player (org.bukkit.entity.Player)62 Entity (org.bukkit.entity.Entity)56 Wand (com.elmakers.mine.bukkit.api.wand.Wand)47 MageController (com.elmakers.mine.bukkit.api.magic.MageController)45 ItemStack (org.bukkit.inventory.ItemStack)38 Location (org.bukkit.Location)33 LivingEntity (org.bukkit.entity.LivingEntity)31 ArrayList (java.util.ArrayList)25 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)18 Inventory (org.bukkit.inventory.Inventory)16 MageClass (com.elmakers.mine.bukkit.api.magic.MageClass)15 Spell (com.elmakers.mine.bukkit.api.spell.Spell)14 SpellTemplate (com.elmakers.mine.bukkit.api.spell.SpellTemplate)14 Block (org.bukkit.block.Block)14 Target (com.elmakers.mine.bukkit.utility.Target)13 EventHandler (org.bukkit.event.EventHandler)13 ItemMeta (org.bukkit.inventory.meta.ItemMeta)12 CasterProperties (com.elmakers.mine.bukkit.api.magic.CasterProperties)10 Vector (org.bukkit.util.Vector)10