Search in sources :

Example 46 with Mage

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

the class ProjectileAction method start.

@Override
public SpellResult start(CastContext context) {
    MageController controller = context.getController();
    Mage mage = context.getMage();
    // Modify with wand power
    // Turned some of this off for now
    // int count = this.count * mage.getRadiusMultiplier();
    // int speed = this.speed * damageMultiplier;
    int size = (int) (mage.getRadiusMultiplier() * this.size);
    double damageMultiplier = mage.getDamageMultiplier("projectile");
    double damage = damageMultiplier * this.damage;
    float radiusMultiplier = mage.getRadiusMultiplier();
    float spread = this.spread;
    if (radiusMultiplier > 1) {
        spread = spread / radiusMultiplier;
    }
    Random random = context.getRandom();
    Class<?> projectileType = NMSUtils.getBukkitClass("net.minecraft.server.Entity" + projectileTypeName);
    if (!CompatibilityUtils.isValidProjectileClass(projectileType)) {
        controller.getLogger().warning("Bad projectile class: " + projectileTypeName);
        return SpellResult.FAIL;
    }
    // Prepare parameters
    Location location = sourceLocation.getLocation(context);
    Vector direction = location.getDirection();
    if (startDistance > 0) {
        location = location.clone().add(direction.clone().multiply(startDistance));
    }
    // Spawn projectiles
    LivingEntity shootingEntity = context.getLivingEntity();
    ProjectileSource source = null;
    if (shootingEntity != null) {
        source = shootingEntity;
    }
    for (int i = 0; i < count; i++) {
        try {
            // Spawn a new projectile
            Projectile projectile = CompatibilityUtils.spawnProjectile(projectileType, location, direction, source, speed, spread, i > 0 ? spread : 0, random);
            if (projectile == null) {
                return SpellResult.FAIL;
            }
            if (shootingEntity != null) {
                projectile.setShooter(shootingEntity);
            }
            if (projectile instanceof Fireball) {
                Fireball fireball = (Fireball) projectile;
                fireball.setIsIncendiary(useFire);
                fireball.setYield(size);
            }
            if (projectile instanceof Arrow) {
                Arrow arrow = (Arrow) projectile;
                if (useFire) {
                    arrow.setFireTicks(300);
                }
                if (damage > 0) {
                    CompatibilityUtils.setDamage(projectile, damage);
                }
                if (tickIncrease > 0) {
                    CompatibilityUtils.decreaseLifespan(projectile, tickIncrease);
                }
                if (pickupStatus != null && !pickupStatus.isEmpty()) {
                    CompatibilityUtils.setPickupStatus(arrow, pickupStatus);
                }
            }
            if (!breakBlocks) {
                projectile.setMetadata("cancel_explosion", new FixedMetadataValue(controller.getPlugin(), true));
            }
            track(context, projectile);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    return checkTracking(context);
}
Also used : Arrow(org.bukkit.entity.Arrow) FixedMetadataValue(org.bukkit.metadata.FixedMetadataValue) Projectile(org.bukkit.entity.Projectile) LivingEntity(org.bukkit.entity.LivingEntity) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Fireball(org.bukkit.entity.Fireball) Random(java.util.Random) Mage(com.elmakers.mine.bukkit.api.magic.Mage) ProjectileSource(org.bukkit.projectiles.ProjectileSource) Vector(org.bukkit.util.Vector) Location(org.bukkit.Location) SourceLocation(com.elmakers.mine.bukkit.magic.SourceLocation)

Example 47 with Mage

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

the class RecallAction method getWaypoint.

@Nullable
protected Waypoint getWaypoint(Player player, RecallType type, int index, ConfigurationSection parameters, CastContext context) {
    Mage mage = context.getMage();
    MageController controller = context.getController();
    switch(type) {
        case MARKER:
            Location location = ConfigurationUtils.getLocation(mage.getData(), markerKey);
            return new Waypoint(type, location, context.getMessage("title_marker"), context.getMessage("cast_marker"), context.getMessage("no_target_marker"), context.getMessage("description_marker", ""), getIcon(context, parameters, "icon_marker"), true);
        case DEATH:
            Waypoint death = new Waypoint(type, mage.getLastDeathLocation(), "Last Death", context.getMessage("cast_death"), context.getMessage("no_target_death"), context.getMessage("description_death", ""), getIcon(context, parameters, "icon_death"), true);
            death.safe = false;
            return death;
        case SPAWN:
            return new Waypoint(type, context.getWorld().getSpawnLocation(), context.getMessage("title_spawn"), context.getMessage("cast_spawn"), context.getMessage("no_target_spawn"), context.getMessage("description_spawn", ""), getIcon(context, parameters, "icon_spawn"), false);
        case TOWN:
            return new Waypoint(type, controller.getTownLocation(player), context.getMessage("title_town"), context.getMessage("cast_town"), context.getMessage("no_target_town"), context.getMessage("description_town", ""), getIcon(context, parameters, "icon_town"), false);
        case HOME:
            Location bedLocation = player == null ? null : player.getBedSpawnLocation();
            if (bedLocation != null) {
                bedLocation.setX(bedLocation.getX() + 0.5);
                bedLocation.setZ(bedLocation.getZ() + 0.5);
            }
            return new Waypoint(type, bedLocation, context.getMessage("title_home"), context.getMessage("cast_home"), context.getMessage("no_target_home"), context.getMessage("description_home", ""), getIcon(context, parameters, "icon_home"), false);
        case WAND:
            List<LostWand> lostWands = mage.getLostWands();
            if (lostWands == null || index < 0 || index >= lostWands.size())
                return null;
            return new Waypoint(type, lostWands.get(index).getLocation(), context.getMessage("title_wand"), context.getMessage("cast_wand"), context.getMessage("no_target_wand"), context.getMessage("description_wand", ""), getIcon(context, parameters, "icon_wand"), true);
        default:
            return null;
    }
}
Also used : MageController(com.elmakers.mine.bukkit.api.magic.MageController) LostWand(com.elmakers.mine.bukkit.api.wand.LostWand) Mage(com.elmakers.mine.bukkit.api.magic.Mage) Location(org.bukkit.Location) Nullable(javax.annotation.Nullable)

Example 48 with Mage

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

the class RecallAction method tryTeleport.

protected boolean tryTeleport(final Player player, final Waypoint waypoint) {
    Mage mage = context.getMage();
    if (waypoint == null)
        return false;
    if (waypoint.isCommand()) {
        if (waypoint.asConsole) {
            try {
                player.getServer().dispatchCommand(Bukkit.getConsoleSender(), waypoint.command);
            } catch (Exception ex) {
                context.getLogger().log(Level.WARNING, "Error running command as console " + waypoint.command, ex);
            }
        } else {
            CommandSender sender = mage.getCommandSender();
            boolean isOp = sender.isOp();
            if (waypoint.opPlayer && !isOp) {
                sender.setOp(true);
            }
            try {
                player.getServer().dispatchCommand(sender, waypoint.command);
            } catch (Exception ex) {
                context.getLogger().log(Level.WARNING, "Error running command " + waypoint.command, ex);
            }
            if (waypoint.opPlayer && !isOp) {
                sender.setOp(false);
            }
        }
        mage.enableSuperProtection(protectionTime);
        return true;
    }
    Location targetLocation = waypoint.location;
    if (targetLocation == null) {
        String serverName = waypoint.serverName;
        String warpName = waypoint.warpName;
        if (warpName != null && serverName != null) {
            context.getController().warpPlayerToServer(player, serverName, warpName);
        } else {
            context.sendMessage(waypoint.failMessage);
        }
        return false;
    }
    if (!allowCrossWorld && !mage.getLocation().getWorld().equals(targetLocation.getWorld())) {
        context.sendMessageKey("cross_world_disallowed");
        return false;
    }
    if (waypoint.maintainDirection) {
        Location playerLocation = player.getLocation();
        targetLocation.setYaw(playerLocation.getYaw());
        targetLocation.setPitch(playerLocation.getPitch());
    }
    mage.enableSuperProtection(protectionTime);
    if (context.teleport(player, targetLocation, verticalSearchDistance, waypoint.safe, waypoint.safe)) {
        context.castMessage(waypoint.message);
    } else {
        context.castMessage(waypoint.failMessage);
    }
    return true;
}
Also used : Mage(com.elmakers.mine.bukkit.api.magic.Mage) CommandSender(org.bukkit.command.CommandSender) Location(org.bukkit.Location)

Example 49 with Mage

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

the class RecallAction method removeMarker.

protected boolean removeMarker() {
    Mage mage = context.getMage();
    ConfigurationSection mageData = mage.getData();
    Location location = ConfigurationUtils.getLocation(mageData, markerKey);
    if (location == null)
        return false;
    mageData.set(markerKey, null);
    return true;
}
Also used : Mage(com.elmakers.mine.bukkit.api.magic.Mage) ConfigurationSection(org.bukkit.configuration.ConfigurationSection) Location(org.bukkit.Location)

Example 50 with Mage

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

the class RecallAction method perform.

@Override
public SpellResult perform(CastContext context) {
    this.context = context;
    enabledTypes.clear();
    warps.clear();
    commands.clear();
    Mage mage = context.getMage();
    MageController controller = context.getController();
    Player player = mage.getPlayer();
    if (player == null) {
        return SpellResult.PLAYER_REQUIRED;
    }
    Set<String> unlockedWarps = new HashSet<>();
    ConfigurationSection mageData = mage.getData();
    String unlockedString = mageData.getString(unlockKey);
    if (unlockedString != null && !unlockedString.isEmpty()) {
        unlockedWarps.addAll(Arrays.asList(StringUtils.split(unlockedString, ',')));
    }
    Set<String> friends = new HashSet<>();
    String friendString = mageData.getString(friendKey);
    if (friendString != null && !friendString.isEmpty()) {
        friends.addAll(Arrays.asList(StringUtils.split(friendString, ',')));
    }
    ConfigurationSection warpConfig = null;
    if (parameters.contains("warps")) {
        warpConfig = ConfigurationUtils.getConfigurationSection(parameters, "warps");
    }
    ConfigurationSection commandConfig = null;
    if (parameters.contains("commands")) {
        commandConfig = ConfigurationUtils.getConfigurationSection(parameters, "commands");
    }
    if (parameters.contains("unlock")) {
        String unlockWarp = parameters.getString("unlock");
        if (unlockWarp == null || unlockWarp.isEmpty() || unlockedWarps.contains(unlockWarp)) {
            return SpellResult.NO_ACTION;
        }
        if (warpConfig == null && commandConfig == null) {
            return SpellResult.FAIL;
        }
        unlockedWarps.add(unlockWarp);
        unlockedString = StringUtils.join(unlockedWarps, ",");
        mageData.set(unlockKey, unlockedString);
        String warpName = unlockWarp;
        ConfigurationSection config = warpConfig == null ? null : warpConfig.getConfigurationSection(unlockWarp);
        if (config != null) {
            warpName = config.getString("name", warpName);
        } else {
            config = commandConfig == null ? null : commandConfig.getConfigurationSection(unlockWarp);
            if (config != null) {
                warpName = config.getString("name", warpName);
            }
        }
        String unlockMessage = context.getMessage("unlock_warp").replace("$name", warpName);
        context.sendMessage(unlockMessage);
        return SpellResult.CAST;
    }
    if (parameters.contains("lock")) {
        String lockWarpString = parameters.getString("lock");
        String[] lockWarps = StringUtils.split(lockWarpString, ',');
        boolean locked = false;
        for (String lockWarp : lockWarps) {
            if (unlockedWarps.contains(lockWarp)) {
                locked = true;
                unlockedWarps.remove(lockWarp);
            }
        }
        if (locked) {
            unlockedString = StringUtils.join(unlockedWarps, ",");
            mageData.set(unlockKey, unlockedString);
        }
        return locked ? SpellResult.DEACTIVATE : SpellResult.NO_ACTION;
    }
    if (parameters.contains("addfriend")) {
        String friendName = parameters.getString("addfriend");
        if (friendName == null || friendName.isEmpty()) {
            return SpellResult.NO_ACTION;
        }
        Player online = DeprecatedUtils.getPlayer(friendName);
        if (online == null) {
            return SpellResult.FAIL;
        }
        String uuid = online.getUniqueId().toString();
        if (friends.contains(uuid)) {
            return SpellResult.NO_ACTION;
        }
        friends.add(uuid);
        friendString = StringUtils.join(friends, ",");
        mageData.set(friendKey, friendString);
        String message = context.getMessage("add_friend").replace("$name", online.getDisplayName());
        context.sendMessage(message);
        return SpellResult.CAST;
    }
    if (parameters.contains("removefriend")) {
        String friendName = parameters.getString("removefriend");
        Player online = DeprecatedUtils.getPlayer(friendName);
        if (online == null) {
            return SpellResult.FAIL;
        }
        String uuid = online.getUniqueId().toString();
        if (!friends.contains(uuid)) {
            return SpellResult.NO_ACTION;
        }
        friends.remove(uuid);
        friendString = StringUtils.join(friends, ",");
        mageData.set(friendKey, friendString);
        String message = context.getMessage("remove_friend").replace("$name", online.getDisplayName());
        context.sendMessage(message);
        return SpellResult.DEACTIVATE;
    }
    Location playerLocation = mage.getLocation();
    for (RecallType testType : RecallType.values()) {
        // Special-case for warps
        if (testType == RecallType.WARP) {
            if (warpConfig != null) {
                Collection<String> warpKeys = warpConfig.getKeys(false);
                for (String warpKey : warpKeys) {
                    ConfigurationSection config = warpConfig.getConfigurationSection(warpKey);
                    boolean isLocked = config.getBoolean("locked", false);
                    if (!isLocked || unlockedWarps.contains(warpKey)) {
                        warps.put(warpKey, config);
                    }
                }
            }
        } else // Special-case for commands
        if (testType == RecallType.COMMAND) {
            if (commandConfig != null) {
                Collection<String> commandKeys = commandConfig.getKeys(false);
                for (String commandKey : commandKeys) {
                    ConfigurationSection config = commandConfig.getConfigurationSection(commandKey);
                    boolean isLocked = config.getBoolean("locked", false);
                    if (!isLocked || unlockedWarps.contains(commandKey)) {
                        commands.put(commandKey, config);
                    }
                }
            }
        } else {
            if (parameters.getBoolean("allow_" + testType.name().toLowerCase(), true)) {
                enabledTypes.add(testType);
            }
        }
    }
    if (warps.size() > 0) {
        enabledTypes.add(RecallType.WARP);
    }
    if (commands.size() > 0) {
        enabledTypes.add(RecallType.COMMAND);
    }
    if (parameters.contains("warp")) {
        String warpName = parameters.getString("warp");
        Waypoint waypoint = getWarp(warpName);
        if (tryTeleport(player, waypoint)) {
            return SpellResult.CAST;
        }
        return SpellResult.FAIL;
    } else if (parameters.contains("command")) {
        String commandName = parameters.getString("command");
        Waypoint waypoint = getCommand(context, commandName);
        if (tryTeleport(player, waypoint)) {
            return SpellResult.CAST;
        }
        return SpellResult.FAIL;
    } else if (parameters.contains("type")) {
        String typeString = parameters.getString("type", "");
        if (parameters.getBoolean("allow_marker", true)) {
            if (typeString.equalsIgnoreCase("remove")) {
                if (removeMarker()) {
                    return SpellResult.TARGET_SELECTED;
                }
                return SpellResult.FAIL;
            }
            if (typeString.equalsIgnoreCase("place")) {
                Block block = context.getLocation().getBlock();
                if (parameters.getBoolean("marker_requires_build", true) && !context.hasBuildPermission(block)) {
                    return SpellResult.NO_TARGET;
                }
                if (hasMarker() && parameters.getBoolean("confirm_marker", true)) {
                    showMarkerConfirm(context);
                    return SpellResult.CAST;
                }
                if (placeMarker(block)) {
                    return SpellResult.TARGET_SELECTED;
                }
                return SpellResult.FAIL;
            }
        }
        RecallType recallType = RecallType.valueOf(typeString.toUpperCase());
        Waypoint location = getWaypoint(player, recallType, 0, parameters, context);
        if (tryTeleport(player, location)) {
            return SpellResult.CAST;
        }
        return SpellResult.FAIL;
    }
    List<Waypoint> allWaypoints = new ArrayList<>();
    for (RecallType selectedType : enabledTypes) {
        if (selectedType == RecallType.FRIENDS) {
            for (String friendId : friends) {
                Waypoint targetLocation = getFriend(friendId);
                if (targetLocation != null && targetLocation.isValid(allowCrossWorld, playerLocation)) {
                    allWaypoints.add(targetLocation);
                }
            }
        } else if (selectedType == RecallType.WARP) {
            for (String warpKey : warps.keySet()) {
                Waypoint targetLocation = getWarp(warpKey);
                if (targetLocation != null && targetLocation.isValid(allowCrossWorld, playerLocation)) {
                    allWaypoints.add(targetLocation);
                }
            }
        } else if (selectedType == RecallType.COMMAND) {
            for (String commandKey : commands.keySet()) {
                Waypoint targetLocation = getCommand(context, commandKey);
                if (targetLocation != null && targetLocation.isValid(allowCrossWorld, playerLocation)) {
                    allWaypoints.add(targetLocation);
                }
            }
        } else if (selectedType == RecallType.WAND) {
            List<LostWand> lostWands = mage.getLostWands();
            for (int i = 0; i < lostWands.size(); i++) {
                Waypoint targetLocation = getWaypoint(player, selectedType, i, parameters, context);
                if (targetLocation != null && targetLocation.isValid(allowCrossWorld, playerLocation)) {
                    allWaypoints.add(targetLocation);
                }
            }
        } else if (selectedType == RecallType.FIELDS) {
            Map<String, Location> fields = controller.getHomeLocations(player);
            if (fields != null) {
                for (Map.Entry<String, Location> fieldEntry : fields.entrySet()) {
                    Location location = fieldEntry.getValue().clone();
                    location.setX(location.getX() + 0.5);
                    location.setZ(location.getZ() + 0.5);
                    allWaypoints.add(new Waypoint(RecallType.FIELDS, location, fieldEntry.getKey(), context.getMessage("cast_field"), context.getMessage("no_target_field"), context.getMessage("description_field", ""), getIcon(context, parameters, "icon_field"), true));
                }
            }
        } else {
            Waypoint targetLocation = getWaypoint(player, selectedType, 0, parameters, context);
            if (targetLocation != null && targetLocation.isValid(allowCrossWorld, playerLocation)) {
                allWaypoints.add(targetLocation);
            }
        }
    }
    if (allWaypoints.size() == 0) {
        return SpellResult.NO_TARGET;
    }
    options.clear();
    Collections.sort(allWaypoints);
    String inventoryTitle = context.getMessage("title", "Recall");
    int invSize = (int) Math.ceil(allWaypoints.size() / 9.0f) * 9;
    Inventory displayInventory = CompatibilityUtils.createInventory(null, invSize, inventoryTitle);
    int index = 0;
    for (Waypoint waypoint : allWaypoints) {
        ItemStack waypointItem;
        if (waypoint.iconURL != null && !waypoint.iconURL.isEmpty()) {
            waypointItem = InventoryUtils.getURLSkull(waypoint.iconURL);
        } else {
            waypointItem = waypoint.icon.getItemStack(1);
        }
        ItemMeta meta = waypointItem == null ? null : waypointItem.getItemMeta();
        if (meta == null) {
            waypointItem = new ItemStack(DefaultWaypointMaterial);
            meta = waypointItem.getItemMeta();
            controller.getLogger().warning("Invalid waypoint icon for " + waypoint.name);
        }
        meta.setDisplayName(waypoint.name);
        if (waypoint.description != null && waypoint.description.length() > 0) {
            List<String> lore = new ArrayList<>();
            InventoryUtils.wrapText(waypoint.description, lore);
            meta.setLore(lore);
        }
        waypointItem.setItemMeta(meta);
        waypointItem = InventoryUtils.makeReal(waypointItem);
        InventoryUtils.hideFlags(waypointItem, (byte) 63);
        InventoryUtils.setMeta(waypointItem, "waypoint", "true");
        CompatibilityUtils.makeUnbreakable(waypointItem);
        displayInventory.setItem(index, waypointItem);
        options.put(index, waypoint);
        index++;
    }
    mage.activateGUI(this, displayInventory);
    return SpellResult.CAST;
}
Also used : ArrayList(java.util.ArrayList) MageController(com.elmakers.mine.bukkit.api.magic.MageController) ArrayList(java.util.ArrayList) List(java.util.List) ItemMeta(org.bukkit.inventory.meta.ItemMeta) HashSet(java.util.HashSet) Player(org.bukkit.entity.Player) Mage(com.elmakers.mine.bukkit.api.magic.Mage) Collection(java.util.Collection) Block(org.bukkit.block.Block) ItemStack(org.bukkit.inventory.ItemStack) HashMap(java.util.HashMap) Map(java.util.Map) Inventory(org.bukkit.inventory.Inventory) ConfigurationSection(org.bukkit.configuration.ConfigurationSection) Location(org.bukkit.Location)

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