Search in sources :

Example 11 with MaterialAndData

use of com.elmakers.mine.bukkit.block.MaterialAndData in project MagicPlugin by elBukkit.

the class MoveBlockAction method perform.

@Override
public SpellResult perform(CastContext context) {
    Block targetBlock = context.getTargetBlock();
    if (!context.isDestructible(targetBlock)) {
        return SpellResult.NO_TARGET;
    }
    if (!context.hasBreakPermission(targetBlock)) {
        return SpellResult.INSUFFICIENT_PERMISSION;
    }
    double distanceSquared = offset.lengthSquared();
    Block moveToBlock = targetBlock;
    int distance = 0;
    Vector moveBlock = offset.clone().normalize();
    while (distance * distance < distanceSquared) {
        moveToBlock = moveToBlock.getLocation().add(moveBlock).getBlock();
        if (moveToBlock.getType() != Material.AIR) {
            return SpellResult.NO_TARGET;
        }
        distance++;
    }
    if (!context.hasBuildPermission(moveToBlock)) {
        return SpellResult.INSUFFICIENT_PERMISSION;
    }
    MaterialAndData blockState = new MaterialAndData(targetBlock);
    context.registerForUndo(targetBlock);
    context.registerForUndo(moveToBlock);
    if (setTarget) {
        Location sourceLocation = context.getTargetLocation();
        Location targetLocation = moveToBlock.getLocation();
        targetLocation.setDirection(sourceLocation.toVector().subtract(targetLocation.toVector()));
        context.setTargetLocation(targetLocation);
    } else {
        // Kind of hack for the way some current spells use FX
        // They play effects from the source to the target
        Location targetLocation = context.getTargetLocation();
        targetLocation.setDirection(moveToBlock.getLocation().toVector().subtract(targetLocation.toVector()));
        context.setTargetLocation(targetLocation);
    }
    targetBlock.setType(Material.AIR);
    blockState.modify(moveToBlock);
    context.registerBreakable(moveToBlock, 1);
    return SpellResult.CAST;
}
Also used : MaterialAndData(com.elmakers.mine.bukkit.block.MaterialAndData) Block(org.bukkit.block.Block) Vector(org.bukkit.util.Vector) Location(org.bukkit.Location)

Example 12 with MaterialAndData

use of com.elmakers.mine.bukkit.block.MaterialAndData in project MagicPlugin by elBukkit.

the class FlowerAction method initialize.

@Override
public void initialize(Spell spell, ConfigurationSection parameters) {
    super.initialize(spell, parameters);
    flowers.clear();
    tallFlowers.clear();
    Collection<String> flowerKeys = parameters.getStringList("flowers");
    for (String flowerKey : flowerKeys) {
        flowers.add(new MaterialAndData(flowerKey));
    }
    Collection<String> tallFlowerKeys = parameters.getStringList("tall_flowers");
    for (String flowerKey : tallFlowerKeys) {
        tallFlowers.add(new MaterialAndData(flowerKey));
    }
}
Also used : MaterialAndData(com.elmakers.mine.bukkit.block.MaterialAndData)

Example 13 with MaterialAndData

use of com.elmakers.mine.bukkit.block.MaterialAndData in project MagicPlugin by elBukkit.

the class FreezeAction method perform.

@SuppressWarnings("deprecation")
@Override
public SpellResult perform(CastContext context) {
    Block block = context.getTargetBlock();
    Material material = Material.SNOW;
    if (block.getType() == Material.WATER || block.getType() == Material.STATIONARY_WATER) {
        if (!freezeWater) {
            return SpellResult.NO_TARGET;
        }
        material = iceMaterial;
    } else if (block.getType() == Material.LAVA) {
        if (!freezeLava) {
            return SpellResult.NO_TARGET;
        }
        material = Material.COBBLESTONE;
    } else if (block.getType() == Material.STATIONARY_LAVA) {
        if (!freezeLava) {
            return SpellResult.NO_TARGET;
        }
        material = Material.OBSIDIAN;
    } else if (block.getType() == Material.FIRE) {
        if (!freezeFire) {
            return SpellResult.NO_TARGET;
        }
        material = Material.AIR;
    } else if (block.getType() == Material.SNOW) {
        material = Material.SNOW;
    } else if (context.isTransparent(block.getType())) {
        return SpellResult.NO_TARGET;
    } else {
        block = block.getRelative(BlockFace.UP);
        // This is kind of ugly, maybe clean it up somehow?
        if (block.getType() == Material.WATER || block.getType() == Material.STATIONARY_WATER) {
            if (!freezeWater) {
                return SpellResult.NO_TARGET;
            }
            material = iceMaterial;
        } else if (block.getType() == Material.LAVA) {
            if (!freezeLava) {
                return SpellResult.NO_TARGET;
            }
            material = Material.COBBLESTONE;
        } else if (block.getType() == Material.STATIONARY_LAVA) {
            if (!freezeLava) {
                return SpellResult.NO_TARGET;
            }
            material = Material.OBSIDIAN;
        } else if (block.getType() == Material.FIRE) {
            if (!freezeFire) {
                return SpellResult.NO_TARGET;
            }
            material = Material.AIR;
        }
    }
    if (!context.isDestructible(block)) {
        return SpellResult.NO_TARGET;
    }
    context.registerForUndo(block);
    MaterialAndData applyMaterial = new MaterialAndData(material);
    if (block.getType() == Material.SNOW && material == Material.SNOW) {
        short data = block.getData();
        if (data < 7) {
            data++;
        }
        applyMaterial.setData(data);
    }
    applyMaterial.modify(block);
    return SpellResult.CAST;
}
Also used : MaterialAndData(com.elmakers.mine.bukkit.block.MaterialAndData) Block(org.bukkit.block.Block) Material(org.bukkit.Material)

Example 14 with MaterialAndData

use of com.elmakers.mine.bukkit.block.MaterialAndData 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)

Example 15 with MaterialAndData

use of com.elmakers.mine.bukkit.block.MaterialAndData in project MagicPlugin by elBukkit.

the class TorchAction method perform.

@Override
public SpellResult perform(CastContext context) {
    Block face = context.getPreviousBlock();
    if (face == null) {
        return SpellResult.NO_TARGET;
    }
    Block target = context.getTargetBlock();
    if (!context.hasBuildPermission(target)) {
        return SpellResult.INSUFFICIENT_PERMISSION;
    }
    if (slippery != null && slippery.testBlock(target)) {
        return SpellResult.NO_TARGET;
    }
    boolean isAir = face.getType() == Material.AIR;
    boolean replaceAttachment = target.getType() == Material.SNOW || target.getType() == Material.SNOW_BLOCK;
    boolean isWater = face.getType() == Material.STATIONARY_WATER || face.getType() == Material.WATER;
    boolean isNether = target.getType() == Material.NETHERRACK || target.getType() == Material.SOUL_SAND;
    MaterialAndData targetMaterial = new MaterialAndData(torchType);
    // Don't replace blocks unless allow_glowstone is explicitly set
    if (isNether && allowLightstone) {
        targetMaterial.setMaterial(Material.GLOWSTONE);
        replaceAttachment = true;
    }
    // Otherwise use glowstone as the torch
    boolean allowLightstone = this.allowLightstone;
    if (useLightstone) {
        targetMaterial.setMaterial(Material.GLOWSTONE);
        allowLightstone = true;
    }
    if (isWater) {
        targetMaterial.setMaterial(Material.GLOWSTONE);
    }
    if (!isAir && !isWater) {
        return SpellResult.NO_TARGET;
    }
    if (targetMaterial.getMaterial() == torchType) {
        BlockFace direction = face.getFace(target);
        if (direction == null) {
            direction = BlockFace.SELF;
        }
        switch(direction) {
            case WEST:
                targetMaterial.setData((short) 1);
                break;
            case EAST:
                targetMaterial.setData((short) 2);
                break;
            case NORTH:
                targetMaterial.setData((short) 3);
                break;
            case SOUTH:
                targetMaterial.setData((short) 4);
                break;
            case DOWN:
                targetMaterial.setData((short) 5);
                break;
            default:
                targetMaterial.setMaterial(Material.GLOWSTONE);
        }
    }
    if (!allowLightstone && targetMaterial.getMaterial() == Material.GLOWSTONE) {
        return SpellResult.NO_TARGET;
    }
    if (!replaceAttachment) {
        target = face;
    }
    context.registerForUndo(target);
    context.getController().disableItemSpawn();
    try {
        targetMaterial.modify(target);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    context.getController().enableItemSpawn();
    if (targetMaterial.getMaterial() != target.getType()) {
        return SpellResult.NO_TARGET;
    }
    return SpellResult.CAST;
}
Also used : BlockFace(org.bukkit.block.BlockFace) MaterialAndData(com.elmakers.mine.bukkit.block.MaterialAndData) Block(org.bukkit.block.Block)

Aggregations

MaterialAndData (com.elmakers.mine.bukkit.block.MaterialAndData)47 Block (org.bukkit.block.Block)20 ItemStack (org.bukkit.inventory.ItemStack)11 ArrayList (java.util.ArrayList)10 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)10 Material (org.bukkit.Material)8 Nullable (javax.annotation.Nullable)7 ItemMeta (org.bukkit.inventory.meta.ItemMeta)7 Mage (com.elmakers.mine.bukkit.api.magic.Mage)5 Target (com.elmakers.mine.bukkit.utility.Target)5 Player (org.bukkit.entity.Player)5 MageController (com.elmakers.mine.bukkit.api.magic.MageController)4 Wand (com.elmakers.mine.bukkit.api.wand.Wand)4 List (java.util.List)4 ItemData (com.elmakers.mine.bukkit.api.item.ItemData)3 MaterialSet (com.elmakers.mine.bukkit.api.magic.MaterialSet)3 IOException (java.io.IOException)3 Location (org.bukkit.Location)3 CurrencyItem (com.elmakers.mine.bukkit.api.block.CurrencyItem)2 ParseException (java.text.ParseException)2