Search in sources :

Example 1 with DeathLocation

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

the class RecallAction method perform.

@Override
public SpellResult perform(CastContext context) {
    if (pendingTeleport != null) {
        return doTeleport();
    }
    if (isActive) {
        if (context.getMage().getActiveGUI() != this) {
            if (context.getTargetLocation() == null) {
                isActive = false;
                return SpellResult.NO_TARGET;
            }
            if (delayExpiration > 0 && System.currentTimeMillis() < delayExpiration) {
                return SpellResult.PENDING;
            }
            if (delayExpiration == 0 && delay > 0) {
                context.playEffects("wait");
                delayExpiration = System.currentTimeMillis() + delay;
                return SpellResult.PENDING;
            }
            isActive = false;
            if (teleport) {
                return doTeleport();
            }
            return SpellResult.CAST;
        }
        return SpellResult.PENDING;
    }
    this.context = context;
    enabledTypes.clear();
    options.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");
    }
    if (parameters.getBoolean("allow_magic_warps", true) && controller instanceof MagicController) {
        String group = parameters.getString("group", null);
        MagicController magic = (MagicController) controller;
        Collection<MagicWarp> warps = magic.getWarps().getMagicWarps();
        if (!warps.isEmpty() && warpConfig == null) {
            warpConfig = ConfigurationUtils.newConfigurationSection();
        }
        for (MagicWarp warp : warps) {
            String icon = warp.getIcon();
            if (icon == null || icon.isEmpty())
                continue;
            String warpGroup = warp.getGroup();
            if (group != null && !group.isEmpty() && warpGroup != null && !warpGroup.isEmpty() && !warpGroup.equalsIgnoreCase(group))
                continue;
            ConfigurationSection magicWarpConfig = ConfigurationUtils.newConfigurationSection();
            magicWarpConfig.set("name", warp.getName());
            magicWarpConfig.set("icon", icon);
            magicWarpConfig.set("description", warp.getDescription());
            magicWarpConfig.set("locked", warp.isLocked());
            warpConfig.set(warp.getKey(), magicWarpConfig);
        }
    }
    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);
            }
        }
        warpName = CompatibilityLib.getCompatibilityUtils().translateColors(warpName);
        String unlockMessage = context.getMessage("unlock_warp").replace("$name", warpName);
        context.sendMessageKey("unlock_warp", 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 = null;
        if (friendName.equals("target")) {
            Entity targetEntity = context.getTargetEntity();
            if (targetEntity != null && targetEntity instanceof Player) {
                online = (Player) targetEntity;
            }
        } else if (friendName.equals("source")) {
            Entity sourceEntity = context.getEntity();
            if (sourceEntity != null && sourceEntity instanceof Player) {
                online = (Player) sourceEntity;
            }
        } else {
            online = CompatibilityLib.getDeprecatedUtils().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.sendMessageKey("add_friend", message);
        return SpellResult.CAST;
    }
    if (parameters.contains("removefriend")) {
        String friendName = parameters.getString("removefriend");
        Player online = CompatibilityLib.getDeprecatedUtils().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.sendMessageKey("remove_friend", message);
        return SpellResult.DEACTIVATE;
    }
    // Add configured options
    Location playerLocation = mage.getLocation();
    Set<RecallType> optionTypes = new HashSet<>();
    Collection<ConfigurationSection> optionConfiguration = ConfigurationUtils.getNodeList(parameters, "options");
    if (optionConfiguration != null) {
        for (ConfigurationSection optionConfig : optionConfiguration) {
            Waypoint newWaypoint = new Waypoint(context, optionConfig);
            options.add(newWaypoint);
            optionTypes.add(newWaypoint.type);
        }
    }
    // Automatically append enabled types if not defined in options
    boolean allowAll = parameters.getBoolean("allow_all", true);
    for (RecallType testType : RecallType.values()) {
        if (!optionTypes.contains(testType) && parameters.getBoolean("allow_" + testType.name().toLowerCase(), allowAll && testType.showByDefault)) {
            switch(testType) {
                case FRIENDS:
                    for (String friendId : friends) {
                        Waypoint targetLocation = getFriend(friendId);
                        if (targetLocation != null && targetLocation.isValid(allowCrossWorld, playerLocation)) {
                            options.add(targetLocation);
                        }
                    }
                    break;
                case REMOVE_FRIENDS:
                    for (String friendId : friends) {
                        Waypoint targetFriend = getRemoveFriend(friendId);
                        if (targetFriend != null) {
                            options.add(targetFriend);
                        }
                    }
                    break;
                case WARP:
                    // Legacy warp config
                    if (warpConfig != null) {
                        Collection<String> warpKeys = warpConfig.getKeys(false);
                        for (String warpKey : warpKeys) {
                            ConfigurationSection config = warpConfig.getConfigurationSection(warpKey);
                            config.set("warp", warpKey);
                            Waypoint warp = new Waypoint(context, config);
                            options.add(warp);
                        }
                    }
                    break;
                case COMMAND:
                    // Legacy command config
                    if (commandConfig != null) {
                        Collection<String> commandKeys = commandConfig.getKeys(false);
                        for (String commandKey : commandKeys) {
                            ConfigurationSection config = commandConfig.getConfigurationSection(commandKey);
                            Waypoint command = new Waypoint(context, config);
                            options.add(command);
                        }
                    }
                    break;
                case WAND:
                    List<LostWand> lostWands = mage.getLostWands();
                    for (LostWand lostWand : lostWands) {
                        Waypoint targetLocation = getWaypoint(player, testType, lostWand.getLocation(), parameters, context);
                        if (targetLocation != null && targetLocation.isValid(allowCrossWorld, playerLocation)) {
                            options.add(targetLocation);
                        }
                    }
                    break;
                case SOULS:
                    List<DeathLocation> deathLocations = controller.getDeathLocations(player);
                    if (deathLocations != null) {
                        for (DeathLocation death : deathLocations) {
                            Waypoint targetLocation = getWaypoint(player, testType, death.getLocation(), parameters, context);
                            if (targetLocation != null && targetLocation.isValid(allowCrossWorld, playerLocation)) {
                                ItemStack[] items = death.getItems();
                                int itemCount = items == null ? 0 : items.length;
                                targetLocation.description = targetLocation.description.replace("$items", Integer.toString(itemCount)).replace("$xp", Integer.toString(death.getExperiencePoints()));
                                options.add(targetLocation);
                            }
                        }
                    }
                    break;
                case ALL_REGIONS:
                case REGIONS:
                    boolean allRegions = testType == RecallType.ALL_REGIONS;
                    Set<String> warpProviders = controller.getPlayerWarpProviderKeys();
                    for (String key : warpProviders) {
                        String allowKey = allRegions ? "all_" : "";
                        boolean allowDefault = allRegions ? false : true;
                        if (parameters.getBoolean("allow_" + allowKey + key.toLowerCase(), allowDefault)) {
                            Collection<PlayerWarp> warps = allRegions ? controller.getAllPlayerWarps(key) : controller.getPlayerWarps(player, key);
                            if (warps == null) {
                                break;
                            }
                            for (PlayerWarp warp : warps) {
                                Location location = warp.getLocation();
                                String description = warp.getDescription();
                                if (description == null) {
                                    description = context.getMessage("description_" + key, context.getMessage("description_regions"));
                                }
                                ItemData icon;
                                MaterialAndData warpIcon = (MaterialAndData) warp.getIcon();
                                if (warpIcon != null) {
                                    icon = context.getController().getOrCreateItem(warpIcon);
                                } else {
                                    icon = getIcon(context, parameters, "icon_" + key);
                                    if (icon == null) {
                                        icon = getIcon(context, parameters, "icon_regions");
                                    }
                                }
                                Waypoint waypoint = new Waypoint(context, RecallType.REGIONS, location, warp.getName(), context.getMessage("cast_" + key, context.getMessage("cast_regions")), context.getMessage("no_target_" + key, context.getMessage("no_target_regions")), description, icon, true);
                                if (waypoint.isValid(allowCrossWorld, playerLocation)) {
                                    options.add(waypoint);
                                }
                            }
                        }
                    }
                    break;
                default:
                    Waypoint targetLocation = getWaypoint(player, testType, null, parameters, context);
                    if (targetLocation != null && targetLocation.isValid(allowCrossWorld, playerLocation)) {
                        options.add(targetLocation);
                    }
                    break;
            }
        }
    }
    // Process special commands
    if (parameters.contains("warp")) {
        String warpName = parameters.getString("warp");
        Waypoint waypoint = getWarp(warpName);
        if (tryTeleport(player, waypoint)) {
            if (teleport) {
                return doTeleport();
            }
            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, 1)) {
                    return SpellResult.TARGET_SELECTED;
                }
                return SpellResult.FAIL;
            }
        }
        RecallType recallType;
        try {
            recallType = RecallType.valueOf(typeString.toUpperCase());
            ;
        } catch (Exception ex) {
            controller.getLogger().warning("Invalid recall type: " + typeString);
            return SpellResult.FAIL;
        }
        Waypoint location = getWaypoint(player, recallType, null, parameters, context);
        if (tryTeleport(player, location)) {
            if (teleport) {
                return doTeleport();
            }
            return SpellResult.CAST;
        }
        return SpellResult.FAIL;
    }
    if (options.size() == 0) {
        return SpellResult.NO_TARGET;
    }
    String inventoryTitle = context.getMessage(titleKey, parameters.getString("title", "Recall"));
    int invSize = (int) Math.ceil(options.size() / 9.0f) * 9;
    Inventory displayInventory = CompatibilityLib.getCompatibilityUtils().createInventory(null, invSize, inventoryTitle);
    int index = 0;
    for (Waypoint waypoint : options) {
        if (waypoint.permission != null && !player.hasPermission(waypoint.permission))
            continue;
        boolean isPlaceholder = waypoint.type == RecallType.PLACEHOLDER;
        boolean isValid = !isPlaceholder && waypoint.isValid(allowCrossWorld, playerLocation);
        boolean isUnavailable = false;
        if (!isPlaceholder && !parameters.getBoolean("allow_" + waypoint.type.name().toLowerCase(), true)) {
            isUnavailable = true;
        }
        if (!isUnavailable && waypoint.locked && (waypoint.warpName == null || !unlockedWarps.contains(waypoint.warpName))) {
            if (!waypoint.showUnavailable) {
                continue;
            }
            isUnavailable = true;
        }
        if (!isValid) {
            isUnavailable = true;
        }
        if (isUnavailable && !waypoint.showUnavailable) {
            waypoint.unavailable = true;
            isPlaceholder = true;
        }
        ItemStack waypointItem = null;
        if (isPlaceholder) {
            String iconPlaceholderKey = parameters.getString("placeholder_icon", "air");
            waypointItem = controller.createItem(iconPlaceholderKey);
            if (waypointItem == null) {
                waypointItem = new ItemStack(DefaultWaypointMaterial);
            }
        } else if (isUnavailable) {
            if (waypoint.unavailableIcon != null) {
                waypointItem = waypoint.unavailableIcon.getItemStack(1);
            } else if (waypoint.iconURL != null && !waypoint.iconURL.isEmpty()) {
                waypointItem = controller.getURLSkull(waypoint.iconURL);
            } else if (waypoint.icon != null) {
                waypointItem = waypoint.icon.getItemStack(1);
            }
        } else {
            if (waypoint.iconURL != null && !waypoint.iconURL.isEmpty()) {
                waypointItem = controller.getURLSkull(waypoint.iconURL);
            } else if (waypoint.icon != null) {
                waypointItem = waypoint.icon.getItemStack(1);
            }
        }
        ItemMeta meta = waypointItem == null ? null : waypointItem.getItemMeta();
        if (meta == null && !isPlaceholder) {
            waypointItem = new ItemStack(DefaultWaypointMaterial);
            meta = waypointItem.getItemMeta();
            controller.getLogger().warning("Invalid waypoint icon for " + waypoint.name);
        }
        if (meta != null) {
            String name = waypoint.name;
            if (!isValid || isUnavailable || isPlaceholder) {
                name = context.getMessage("unavailable_name", " ").replace("$name", name);
            }
            meta.setDisplayName(name);
            if (waypoint.description != null && waypoint.description.length() > 0) {
                List<String> lore = new ArrayList<>();
                CompatibilityLib.getInventoryUtils().wrapText(context.parameterize(waypoint.description), lore);
                meta.setLore(lore);
            }
            String invalidMessage = context.getMessage("invalid_description");
            if (!isValid) {
                List<String> lore = meta.getLore();
                if (lore == null) {
                    lore = new ArrayList<>();
                }
                CompatibilityLib.getInventoryUtils().wrapText(invalidMessage, lore);
                meta.setLore(lore);
            } else if (isUnavailable && waypoint.unavailableMessage != null && waypoint.unavailableMessage.length() > 0) {
                List<String> lore = meta.getLore();
                if (lore == null) {
                    lore = new ArrayList<>();
                }
                CompatibilityLib.getInventoryUtils().wrapText(waypoint.unavailableMessage, lore);
                meta.setLore(lore);
            }
            waypointItem.setItemMeta(meta);
            waypointItem = CompatibilityLib.getItemUtils().makeReal(waypointItem);
            CompatibilityLib.getItemUtils().hideFlags(waypointItem, CompatibilityConstants.ALL_HIDE_FLAGS);
            CompatibilityLib.getNBTUtils().setString(waypointItem, "waypoint", "true");
            CompatibilityLib.getItemUtils().makeUnbreakable(waypointItem);
            if (isPlaceholder) {
                CompatibilityLib.getNBTUtils().setBoolean(waypointItem, "placeholder", true);
            }
            if (isUnavailable) {
                CompatibilityLib.getNBTUtils().setBoolean(waypointItem, "unavailable", true);
            }
        }
        displayInventory.setItem(index, waypointItem);
        index++;
        // The inventory will limit its own size
        if (index >= displayInventory.getSize())
            break;
    }
    context.playEffects("menu");
    mage.activateGUI(this, displayInventory);
    isActive = true;
    return SpellResult.PENDING;
}
Also used : Entity(org.bukkit.entity.Entity) ArrayList(java.util.ArrayList) MageController(com.elmakers.mine.bukkit.api.magic.MageController) LostWand(com.elmakers.mine.bukkit.api.wand.LostWand) PlayerWarp(com.elmakers.mine.bukkit.api.protection.PlayerWarp) MagicController(com.elmakers.mine.bukkit.magic.MagicController) ArrayList(java.util.ArrayList) List(java.util.List) ItemMeta(org.bukkit.inventory.meta.ItemMeta) HashSet(java.util.HashSet) Player(org.bukkit.entity.Player) DeathLocation(com.elmakers.mine.bukkit.api.magic.DeathLocation) MagicWarp(com.elmakers.mine.bukkit.warp.MagicWarp) Mage(com.elmakers.mine.bukkit.api.magic.Mage) MaterialAndData(com.elmakers.mine.bukkit.block.MaterialAndData) Block(org.bukkit.block.Block) ItemStack(org.bukkit.inventory.ItemStack) Inventory(org.bukkit.inventory.Inventory) ConfigurationSection(org.bukkit.configuration.ConfigurationSection) Location(org.bukkit.Location) DeathLocation(com.elmakers.mine.bukkit.api.magic.DeathLocation) ItemData(com.elmakers.mine.bukkit.api.item.ItemData)

Example 2 with DeathLocation

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

the class DeadSoulsManager method getSoulLocations.

public void getSoulLocations(Player player, List<DeathLocation> locations) {
    List<DeadSoulsAPI.Soul> souls = new ArrayList<>();
    api.getSoulsByPlayer(souls, player.getUniqueId());
    for (DeadSoulsAPI.Soul soul : souls) {
        Location location = soul.getLocation();
        if (location != null) {
            locations.add(new DeathLocation(location, soul.getItems(), soul.getExperiencePoints()));
        }
    }
}
Also used : DeadSoulsAPI(com.darkyen.minecraft.DeadSoulsAPI) DeathLocation(com.elmakers.mine.bukkit.api.magic.DeathLocation) ArrayList(java.util.ArrayList) Location(org.bukkit.Location) DeathLocation(com.elmakers.mine.bukkit.api.magic.DeathLocation)

Aggregations

DeathLocation (com.elmakers.mine.bukkit.api.magic.DeathLocation)2 ArrayList (java.util.ArrayList)2 Location (org.bukkit.Location)2 DeadSoulsAPI (com.darkyen.minecraft.DeadSoulsAPI)1 ItemData (com.elmakers.mine.bukkit.api.item.ItemData)1 Mage (com.elmakers.mine.bukkit.api.magic.Mage)1 MageController (com.elmakers.mine.bukkit.api.magic.MageController)1 PlayerWarp (com.elmakers.mine.bukkit.api.protection.PlayerWarp)1 LostWand (com.elmakers.mine.bukkit.api.wand.LostWand)1 MaterialAndData (com.elmakers.mine.bukkit.block.MaterialAndData)1 MagicController (com.elmakers.mine.bukkit.magic.MagicController)1 MagicWarp (com.elmakers.mine.bukkit.warp.MagicWarp)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Block (org.bukkit.block.Block)1 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)1 Entity (org.bukkit.entity.Entity)1 Player (org.bukkit.entity.Player)1 Inventory (org.bukkit.inventory.Inventory)1 ItemStack (org.bukkit.inventory.ItemStack)1