Search in sources :

Example 16 with Wand

use of com.elmakers.mine.bukkit.api.wand.Wand in project MagicPlugin by elBukkit.

the class DisarmAction method perform.

@Override
public SpellResult perform(CastContext context) {
    Entity target = context.getTargetEntity();
    if (target == null || !(target instanceof LivingEntity)) {
        return SpellResult.NO_TARGET;
    }
    LivingEntity entity = (LivingEntity) target;
    EntityEquipment equipment = entity.getEquipment();
    ItemStack stack = null;
    Integer originalSlot = null;
    boolean isMainHand = false;
    if (displayName == null) {
        stack = equipment.getItemInMainHand();
        isMainHand = true;
    } else {
        // This is not compatible
        keepInInventory = false;
        // Must be a player in this case
        if (!(entity instanceof Player)) {
            return SpellResult.PLAYER_REQUIRED;
        }
        PlayerInventory playerInventory = ((Player) entity).getInventory();
        for (originalSlot = 0; originalSlot < playerInventory.getSize(); originalSlot++) {
            ItemStack item = playerInventory.getItem(originalSlot);
            if (InventoryUtils.isEmpty(item))
                continue;
            ItemMeta meta = item.getItemMeta();
            if (meta == null || !meta.hasDisplayName())
                continue;
            if (meta.getDisplayName().equals(displayName)) {
                stack = item;
                isMainHand = originalSlot == playerInventory.getHeldItemSlot();
                break;
            }
        }
    }
    if (InventoryUtils.isEmpty(stack)) {
        return SpellResult.NO_TARGET;
    }
    // Special case for wands
    MageController controller = context.getController();
    Mage targetMage = controller.getMage(entity);
    if (com.elmakers.mine.bukkit.wand.Wand.isWand(stack) && controller.isMage(entity)) {
        Mage mage = context.getMage();
        // This gets overridden by superpower...
        if (!mage.isSuperPowered() && targetMage.isSuperProtected()) {
            return SpellResult.NO_TARGET;
        }
        Wand activeWand = targetMage.getActiveWand();
        if (activeWand != null && isMainHand) {
            targetMage.getActiveWand().deactivate();
            stack = equipment.getItemInMainHand();
        }
    }
    Integer targetSlot = null;
    PlayerInventory targetInventory = null;
    ItemStack swapItem = null;
    Random random = context.getRandom();
    if (entity instanceof Player && keepInInventory) {
        Player targetPlayer = (Player) entity;
        targetInventory = targetPlayer.getInventory();
        originalSlot = targetInventory.getHeldItemSlot();
        List<Integer> validSlots = new ArrayList<>();
        ItemStack[] contents = targetInventory.getContents();
        for (int i = minSlot; i <= maxSlot; i++) {
            if (contents[i] == null || contents[i].getType() == Material.AIR) {
                validSlots.add(i);
            }
        }
        // Randomly choose a slot if no empty one was found
        if (validSlots.size() == 0) {
            int swapSlot = random.nextInt(maxSlot - minSlot) + minSlot;
            swapItem = targetInventory.getItem(swapSlot);
            validSlots.add(swapSlot);
        }
        int chosen = random.nextInt(validSlots.size());
        targetSlot = validSlots.get(chosen);
    }
    if (displayName != null) {
        ((Player) entity).getInventory().setItem(originalSlot, null);
    } else {
        equipment.setItemInMainHand(swapItem);
    }
    if (targetSlot != null && targetInventory != null) {
        targetInventory.setItem(targetSlot, stack);
        if (originalSlot != null) {
            DisarmUndoAction disarmUndo = new DisarmUndoAction(targetMage, originalSlot, targetSlot);
            context.registerForUndo(disarmUndo);
        }
    } else {
        Location location = entity.getLocation();
        location.setY(location.getY() + 1);
        Item item = entity.getWorld().dropItemNaturally(location, stack);
        Vector velocity = item.getVelocity();
        velocity.setY(velocity.getY() * 5);
        SafetyUtils.setVelocity(item, velocity);
    }
    return SpellResult.CAST;
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) ArrayList(java.util.ArrayList) Wand(com.elmakers.mine.bukkit.api.wand.Wand) PlayerInventory(org.bukkit.inventory.PlayerInventory) LivingEntity(org.bukkit.entity.LivingEntity) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Item(org.bukkit.entity.Item) Random(java.util.Random) EntityEquipment(org.bukkit.inventory.EntityEquipment) Mage(com.elmakers.mine.bukkit.api.magic.Mage) ItemStack(org.bukkit.inventory.ItemStack) Vector(org.bukkit.util.Vector) ItemMeta(org.bukkit.inventory.meta.ItemMeta) Location(org.bukkit.Location)

Example 17 with Wand

use of com.elmakers.mine.bukkit.api.wand.Wand in project MagicPlugin by elBukkit.

the class FillWandAction method perform.

@Override
public SpellResult perform(CastContext context) {
    Mage mage = context.getMage();
    Wand wand = context.getWand();
    Player player = mage.getPlayer();
    if (player == null) {
        return SpellResult.PLAYER_REQUIRED;
    }
    if (wand == null) {
        context.showMessage("no_wand", "You must be holding a wand!");
        return SpellResult.FAIL;
    }
    wand.fill(player, maxLevel);
    return SpellResult.CAST;
}
Also used : Player(org.bukkit.entity.Player) Mage(com.elmakers.mine.bukkit.api.magic.Mage) Wand(com.elmakers.mine.bukkit.api.wand.Wand)

Example 18 with Wand

use of com.elmakers.mine.bukkit.api.wand.Wand in project MagicPlugin by elBukkit.

the class InventoryAction method perform.

@Override
public SpellResult perform(CastContext context) {
    MageController controller = context.getController();
    Entity targetEntity = context.getTargetEntity();
    Mage showMage = context.getMage();
    Player showPlayer = showMage.getPlayer();
    if (showPlayer == null)
        return SpellResult.PLAYER_REQUIRED;
    // Make sure to close the player's wand
    Wand activeWand = showMage.getActiveWand();
    if (activeWand != null) {
        activeWand.closeInventory();
    }
    switch(inventoryType) {
        case CRAFTING:
            if (targetEntity == null || !(targetEntity instanceof Player)) {
                return SpellResult.NO_TARGET;
            }
            Mage mage = controller.getMage(targetEntity);
            Inventory craftingInventory = mage.getInventory();
            showPlayer.openInventory(craftingInventory);
            break;
        case ENDER_CHEST:
            if (targetEntity == null || !(targetEntity instanceof HumanEntity)) {
                return SpellResult.NO_TARGET;
            }
            HumanEntity humanTarget = (HumanEntity) targetEntity;
            Inventory enderInventory = humanTarget.getEnderChest();
            showPlayer.openInventory(enderInventory);
            break;
        case WORKBENCH:
            showPlayer.openWorkbench(null, true);
            break;
        case CHEST:
            if (disposal) {
                showGenericInventory(showPlayer);
                break;
            }
            Block targetBlock = context.getTargetBlock();
            if (targetBlock == null)
                return SpellResult.NO_TARGET;
            BlockState state = targetBlock.getState();
            if (!(state instanceof InventoryHolder))
                return SpellResult.NO_TARGET;
            InventoryHolder holder = (InventoryHolder) state;
            showPlayer.openInventory(holder.getInventory());
            break;
        default:
            // Probably wont' work very well, but sure why not.
            showGenericInventory(showPlayer);
            break;
    }
    return SpellResult.CAST;
}
Also used : MageController(com.elmakers.mine.bukkit.api.magic.MageController) HumanEntity(org.bukkit.entity.HumanEntity) Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) BlockState(org.bukkit.block.BlockState) Mage(com.elmakers.mine.bukkit.api.magic.Mage) HumanEntity(org.bukkit.entity.HumanEntity) Wand(com.elmakers.mine.bukkit.api.wand.Wand) Block(org.bukkit.block.Block) InventoryHolder(org.bukkit.inventory.InventoryHolder) Inventory(org.bukkit.inventory.Inventory)

Example 19 with Wand

use of com.elmakers.mine.bukkit.api.wand.Wand in project MagicPlugin by elBukkit.

the class ItemShopAction method parseItemKey.

@Nullable
protected ItemStack parseItemKey(MageController controller, String itemKey) {
    if (itemKey == null || itemKey.isEmpty() || itemKey.equalsIgnoreCase("none")) {
        return null;
    }
    String[] pieces = StringUtils.split(itemKey, '@');
    int amount = 1;
    if (pieces.length > 1) {
        itemKey = pieces[0];
        try {
            amount = Integer.parseInt(pieces[1]);
        } catch (Exception ex) {
            context.getLogger().warning("Invalid item amount in shop: " + pieces[1] + " for item " + itemKey + " shop " + context.getSpell().getKey());
        }
    }
    ItemStack item = controller.createItem(itemKey, castsSpells);
    if (item == null)
        return null;
    if (showActiveIcons && controller.getAPI().isWand(item)) {
        Wand wand = controller.getWand(item);
        wand.getIcon().applyToItem(item);
    }
    item.setAmount(amount);
    return item;
}
Also used : Wand(com.elmakers.mine.bukkit.api.wand.Wand) ItemStack(org.bukkit.inventory.ItemStack) Nullable(javax.annotation.Nullable)

Example 20 with Wand

use of com.elmakers.mine.bukkit.api.wand.Wand in project MagicPlugin by elBukkit.

the class BrushSelectAction method perform.

@Override
public SpellResult perform(CastContext context) {
    Mage mage = context.getMage();
    MageController controller = context.getController();
    Wand wand = context.getWand();
    schematics.clear();
    variants.clear();
    this.context = context;
    Player player = mage.getPlayer();
    if (player == null) {
        return SpellResult.PLAYER_REQUIRED;
    }
    if (wand == null) {
        return SpellResult.FAIL;
    }
    List<String> brushKeys = new ArrayList<>(wand.getBrushes());
    Collections.sort(brushKeys);
    List<ItemStack> brushes = new ArrayList<>();
    List<ItemStack> specials = new ArrayList<>();
    MaterialAndData previous = null;
    for (String brushKey : brushKeys) {
        ItemStack brushItem = com.elmakers.mine.bukkit.wand.Wand.createBrushItem(brushKey, controller, null, false);
        if (MaterialBrush.isSchematic(brushKey)) {
            schematics.add(brushItem);
            continue;
        }
        if (MaterialBrush.isSpecialMaterialKey(brushKey)) {
            specials.add(brushItem);
            continue;
        }
        if (brushItem != null) {
            MaterialAndData material = new MaterialAndData(brushKey);
            if (previous != null && material.getMaterial() == previous.getMaterial()) {
                List<ItemStack> variantList = variants.get(material.getMaterial());
                ItemStack lastAdded = brushes.get(brushes.size() - 1);
                if (variantList == null) {
                    String baseName = material.getBaseName();
                    variantList = new ArrayList<>();
                    variantList.add(lastAdded);
                    brushes.remove(brushes.size() - 1);
                    ItemStack category = InventoryUtils.getCopy(lastAdded);
                    ItemMeta meta = category.getItemMeta();
                    String name = context.getMessage("variant_name", "" + ChatColor.AQUA + "$variant");
                    meta.setDisplayName(name.replace("$variant", baseName));
                    List<String> lore = new ArrayList<>();
                    String description = context.getMessage("variant_description", "Choose a type of $variant");
                    lore.add(description.replace("$variant", baseName));
                    meta.setLore(lore);
                    category.setItemMeta(meta);
                    InventoryUtils.setMeta(category, "brush_set", "variants");
                    variants.put(material.getMaterial(), variantList);
                    brushes.add(category);
                }
                variantList.add(brushItem);
            } else {
                brushes.add(brushItem);
            }
            previous = material;
        }
    }
    ItemStack schematicItem = null;
    if (schematics.size() == 1) {
        schematicItem = schematics.get(0);
    } else if (schematics.size() > 0) {
        schematicItem = InventoryUtils.getCopy(schematics.get(0));
        ItemMeta meta = schematicItem.getItemMeta();
        meta.setDisplayName(context.getMessage("schematics_name", "" + ChatColor.AQUA + "Schematics"));
        List<String> lore = new ArrayList<>();
        lore.add(context.getMessage("schematics_description", "Choose a schematic"));
        meta.setLore(lore);
        schematicItem.setItemMeta(meta);
        InventoryUtils.setMeta(schematicItem, "brush_set", "schematics");
    }
    if (schematicItem != null) {
        brushes.add(schematicItem);
    }
    brushes.addAll(specials);
    if (brushes.size() == 0) {
        return SpellResult.NO_TARGET;
    }
    int inventorySize = 9 * INVENTORY_ROWS;
    int numPages = (int) Math.ceil((float) brushes.size() / inventorySize);
    if (page < 1)
        page = numPages;
    else if (page > numPages)
        page = 1;
    int pageIndex = page - 1;
    int startIndex = pageIndex * inventorySize;
    int maxIndex = (pageIndex + 1) * inventorySize - 1;
    List<ItemStack> showBrushes = new ArrayList<>();
    for (int i = startIndex; i <= maxIndex && i < brushes.size(); i++) {
        showBrushes.add(brushes.get(i));
    }
    String inventoryTitle = context.getMessage("title", "Brushes");
    if (numPages > 1) {
        inventoryTitle += " (" + page + "/" + numPages + ")";
    }
    int invSize = (int) Math.ceil(showBrushes.size() / 9.0f) * 9;
    Inventory displayInventory = CompatibilityUtils.createInventory(null, invSize, inventoryTitle);
    for (ItemStack brush : showBrushes) {
        displayInventory.addItem(brush);
    }
    mage.activateGUI(this, displayInventory);
    return SpellResult.CAST;
}
Also used : Player(org.bukkit.entity.Player) ArrayList(java.util.ArrayList) Wand(com.elmakers.mine.bukkit.api.wand.Wand) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Mage(com.elmakers.mine.bukkit.api.magic.Mage) MaterialAndData(com.elmakers.mine.bukkit.block.MaterialAndData) ArrayList(java.util.ArrayList) List(java.util.List) ItemStack(org.bukkit.inventory.ItemStack) ItemMeta(org.bukkit.inventory.meta.ItemMeta) Inventory(org.bukkit.inventory.Inventory)

Aggregations

Wand (com.elmakers.mine.bukkit.api.wand.Wand)60 Mage (com.elmakers.mine.bukkit.api.magic.Mage)47 ItemStack (org.bukkit.inventory.ItemStack)20 Player (org.bukkit.entity.Player)18 MageController (com.elmakers.mine.bukkit.api.magic.MageController)14 Entity (org.bukkit.entity.Entity)10 SpellTemplate (com.elmakers.mine.bukkit.api.spell.SpellTemplate)7 WandUpgradePath (com.elmakers.mine.bukkit.api.wand.WandUpgradePath)7 ArrayList (java.util.ArrayList)7 CasterProperties (com.elmakers.mine.bukkit.api.magic.CasterProperties)6 MageClass (com.elmakers.mine.bukkit.api.magic.MageClass)5 Inventory (org.bukkit.inventory.Inventory)5 ItemMeta (org.bukkit.inventory.meta.ItemMeta)5 ProgressionPath (com.elmakers.mine.bukkit.api.magic.ProgressionPath)4 Spell (com.elmakers.mine.bukkit.api.spell.Spell)4 MaterialAndData (com.elmakers.mine.bukkit.block.MaterialAndData)4 Nullable (javax.annotation.Nullable)4 Checker (ch.njol.util.Checker)3 IOException (java.io.IOException)3 Block (org.bukkit.block.Block)3