Search in sources :

Example 56 with Material

use of org.bukkit.Material in project MagicPlugin by elBukkit.

the class WearAction method perform.

@Override
public SpellResult perform(CastContext context) {
    Entity entity = context.getTargetEntity();
    if (entity == null) {
        if (!context.getTargetsCaster())
            return SpellResult.NO_TARGET;
        entity = context.getEntity();
    }
    if (entity == null || !(entity instanceof Player)) {
        return SpellResult.NO_TARGET;
    }
    Player player = (Player) entity;
    MaterialAndData material = this.material;
    MageController controller = context.getController();
    Mage mage = controller.getMage(player);
    if (useItem) {
        Wand activeWand = mage.getActiveWand();
        // Not handling this for now.
        if (activeWand != context.getWand()) {
            return SpellResult.NO_TARGET;
        }
        if (activeWand != null) {
            activeWand.deactivate();
        }
        ItemStack itemInHand = player.getInventory().getItemInMainHand();
        if (itemInHand == null || itemInHand.getType() == Material.AIR) {
            return SpellResult.FAIL;
        }
        ItemStack[] armor = player.getInventory().getArmorContents();
        ItemStack currentItem = armor[slotNumber];
        armor[slotNumber] = itemInHand;
        player.getInventory().setArmorContents(armor);
        if (!InventoryUtils.isTemporary(currentItem)) {
            player.getInventory().setItemInMainHand(currentItem);
        } else {
            player.getInventory().setItemInMainHand(new ItemStack(Material.AIR));
        }
        if (mage instanceof com.elmakers.mine.bukkit.magic.Mage) {
            ((com.elmakers.mine.bukkit.magic.Mage) mage).armorUpdated();
        }
        return SpellResult.CAST;
    }
    ItemStack wearItem = null;
    String materialName = null;
    if (item == null) {
        if (material == null && (context.getSpell().usesBrush() || context.getSpell().hasBrushOverride())) {
            material = context.getBrush();
        }
        if (material == null) {
            Block targetBlock = context.getTargetBlock();
            if (targetBlock != null) {
                material = new com.elmakers.mine.bukkit.block.MaterialAndData(targetBlock);
                // Check for Banners with 1.7 support
                // TODO: this is outdated?
                Material baseMaterial = material.getMaterial();
                if (DeprecatedUtils.getId(baseMaterial) == 176 || DeprecatedUtils.getId(baseMaterial) == 177) {
                    ((com.elmakers.mine.bukkit.block.MaterialAndData) material).setMaterialId(425);
                }
            }
        }
        if (material == null || material.getMaterial() == Material.AIR) {
            return SpellResult.NO_TARGET;
        }
        wearItem = material.getItemStack(1);
        materialName = material.getName();
    } else {
        wearItem = InventoryUtils.getCopy(item);
        materialName = context.getController().describeItem(wearItem);
    }
    ItemMeta meta = wearItem.getItemMeta();
    // Legacy support
    String displayName = context.getMessage("hat_name", "");
    displayName = context.getMessage("wear_name", displayName);
    if (materialName == null || materialName.isEmpty()) {
        materialName = "?";
    }
    if (displayName != null && !displayName.isEmpty()) {
        meta.setDisplayName(displayName.replace("$hat", materialName).replace("$item", materialName));
    }
    List<String> lore = new ArrayList<>();
    String loreLine = context.getMessage("hat_lore");
    loreLine = context.getMessage("wear_lore", loreLine);
    lore.add(loreLine);
    meta.setLore(lore);
    wearItem.setItemMeta(meta);
    wearItem = InventoryUtils.makeReal(wearItem);
    NMSUtils.makeTemporary(wearItem, context.getMessage("removed").replace("$hat", materialName).replace("$item", materialName));
    if (enchantments != null) {
        wearItem.addUnsafeEnchantments(enchantments);
    }
    if (unbreakable) {
        CompatibilityUtils.makeUnbreakable(wearItem);
    }
    ItemStack[] armor = player.getInventory().getArmorContents();
    ItemStack itemStack = armor[slotNumber];
    if (itemStack != null && itemStack.getType() != Material.AIR) {
        if (NMSUtils.isTemporary(itemStack)) {
            ItemStack replacement = NMSUtils.getReplacement(itemStack);
            if (replacement != null) {
                itemStack = replacement;
            }
        }
        NMSUtils.setReplacement(wearItem, itemStack);
    }
    armor[slotNumber] = wearItem;
    player.getInventory().setArmorContents(armor);
    // Sanity check to make sure the block was allowed to be created
    armor = player.getInventory().getArmorContents();
    ItemStack helmetItem = armor[slotNumber];
    if (!NMSUtils.isTemporary(helmetItem)) {
        armor[slotNumber] = itemStack;
        player.getInventory().setArmorContents(armor);
        return SpellResult.NO_TARGET;
    }
    targetMage = mage;
    context.registerForUndo(new WearUndoAction());
    if (mage instanceof com.elmakers.mine.bukkit.magic.Mage) {
        ((com.elmakers.mine.bukkit.magic.Mage) mage).armorUpdated();
    }
    return SpellResult.CAST;
}
Also used : Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) ArrayList(java.util.ArrayList) Wand(com.elmakers.mine.bukkit.api.wand.Wand) Material(org.bukkit.Material) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Mage(com.elmakers.mine.bukkit.api.magic.Mage) MaterialAndData(com.elmakers.mine.bukkit.api.block.MaterialAndData) Block(org.bukkit.block.Block) ItemStack(org.bukkit.inventory.ItemStack) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 57 with Material

use of org.bukkit.Material in project MagicPlugin by elBukkit.

the class ConstructBatch method constructBlock.

@SuppressWarnings("deprecation")
public boolean constructBlock(int dx, int dy, int dz) {
    // Special-case hackiness..
    if (limitYAxis && minOrientDimension != null && dy < -minOrientDimension)
        return true;
    if (limitYAxis && maxOrientDimension != null && dy > maxOrientDimension)
        return true;
    if (bounds != null) {
        if (dx > bounds.getX() || dy > bounds.getY() || dz > bounds.getZ())
            return true;
        if (dx < -bounds.getX() || dy < -bounds.getY() || dz < -bounds.getZ())
            return true;
    }
    // Initial range checks, we skip everything if this is not sane.
    int x = center.getBlockX() + dx;
    int y = center.getBlockY() + dy;
    int z = center.getBlockZ() + dz;
    if (y < 0 || y > center.getWorld().getMaxHeight())
        return true;
    // Make sure the block is loaded.
    Block block = center.getWorld().getBlockAt(x, y, z);
    if (!block.getChunk().isLoaded()) {
        block.getChunk().load();
        return false;
    }
    // Destructibility and permission checks
    if (!spell.isDestructible(block)) {
        return true;
    }
    if (replace != null && replace.size() > 0) {
        com.elmakers.mine.bukkit.block.MaterialAndData check = new com.elmakers.mine.bukkit.block.MaterialAndData(block);
        if (!replace.contains(check.getKey()) && !replace.contains(check.getWildDataKey())) {
            return true;
        }
    }
    // Prepare material brush, it may update
    // given the current target (clone, replicate)
    MaterialBrush brush = spell.getBrush();
    brush.update(mage, block.getLocation());
    if (brush.isErase()) {
        if (!spell.hasBreakPermission(block)) {
            return true;
        }
    } else {
        if (!spell.hasBuildPermission(block)) {
            return true;
        }
    }
    // Check for power mode.
    if (power) {
        Material material = block.getType();
        BlockState blockState = block.getState();
        MaterialData data = blockState.getData();
        boolean powerBlock = false;
        if (data instanceof Button) {
            Button powerData = (Button) data;
            registerForUndo(block);
            powerData.setPowered(!powerData.isPowered());
            powerBlock = true;
        } else if (data instanceof Lever) {
            Lever powerData = (Lever) data;
            registerForUndo(block);
            powerData.setPowered(!powerData.isPowered());
            powerBlock = true;
        } else if (data instanceof PistonBaseMaterial) {
            PistonBaseMaterial powerData = (PistonBaseMaterial) data;
            registerForUndo(block);
            powerData.setPowered(!powerData.isPowered());
            powerBlock = true;
        } else if (data instanceof PoweredRail) {
            PoweredRail powerData = (PoweredRail) data;
            registerForUndo(block);
            powerData.setPowered(!powerData.isPowered());
            powerBlock = true;
        } else if (data instanceof RedstoneWire) {
            RedstoneWire wireData = (RedstoneWire) data;
            registerForUndo(block);
            wireData.setData((byte) (15 - wireData.getData()));
            powerBlock = true;
        } else if (material == Material.REDSTONE_BLOCK) {
            registerForUndo(block);
            block.getWorld().playEffect(block.getLocation(), Effect.STEP_SOUND, material.getId());
            controller.getRedstoneReplacement().modify(block, applyPhysics);
        } else if (material == Material.REDSTONE_TORCH_OFF) {
            registerForUndo(block);
            block.setType(Material.REDSTONE_TORCH_ON);
        } else if (material == Material.REDSTONE_TORCH_ON) {
            registerForUndo(block);
            block.setType(Material.REDSTONE_TORCH_OFF);
        } else if (material == Material.TNT) {
            registerForUndo(block);
            block.setType(Material.AIR);
            // Kaboomy time!
            registerForUndo(block.getLocation().getWorld().spawnEntity(block.getLocation(), EntityType.PRIMED_TNT));
        }
        if (powerBlock) {
            blockState.update();
        }
        return true;
    }
    // Make sure the brush is ready, it may need to load chunks.
    if (!brush.isReady()) {
        brush.prepare();
        return false;
    }
    // Postpone attachable blocks to a second batch
    if (attachables.testMaterialAndData(brush) || attachablesWall.testMaterialAndData(brush) || attachablesDouble.testMaterialAndData(brush)) {
        BlockData attachBlock = new BlockData(block);
        attachBlock.updateFrom(brush);
        attachedBlockMap.put(attachBlock.getId(), attachBlock);
        attachedBlockList.add(attachBlock);
        return true;
    }
    if (delayed.testMaterialAndData(brush)) {
        BlockData delayBlock = new BlockData(block);
        delayBlock.updateFrom(brush);
        delayedBlocks.add(delayBlock);
        return true;
    }
    modifyWith(block, brush);
    return true;
}
Also used : RedstoneWire(org.bukkit.material.RedstoneWire) PistonBaseMaterial(org.bukkit.material.PistonBaseMaterial) Material(org.bukkit.Material) PoweredRail(org.bukkit.material.PoweredRail) BlockState(org.bukkit.block.BlockState) MaterialBrush(com.elmakers.mine.bukkit.api.block.MaterialBrush) Lever(org.bukkit.material.Lever) Button(org.bukkit.material.Button) MaterialAndData(com.elmakers.mine.bukkit.api.block.MaterialAndData) Block(org.bukkit.block.Block) FallingBlock(org.bukkit.entity.FallingBlock) MaterialData(org.bukkit.material.MaterialData) BlockData(com.elmakers.mine.bukkit.block.BlockData) PistonBaseMaterial(org.bukkit.material.PistonBaseMaterial)

Example 58 with Material

use of org.bukkit.Material in project MagicPlugin by elBukkit.

the class PortalSpell method onCast.

@Override
public SpellResult onCast(ConfigurationSection parameters) {
    Block target = getTargetBlock();
    if (target == null) {
        return SpellResult.NO_TARGET;
    }
    if (!hasBuildPermission(target)) {
        return SpellResult.INSUFFICIENT_PERMISSION;
    }
    Material blockType = target.getType();
    Block portalBase = target.getRelative(BlockFace.UP);
    if (portalBase == null) {
        return SpellResult.NO_TARGET;
    }
    blockType = portalBase.getType();
    if (blockType != Material.AIR) {
        portalBase = getPreviousBlock();
    }
    if (portalBase == null) {
        return SpellResult.NO_TARGET;
    }
    blockType = portalBase.getType();
    if (blockType != Material.AIR && blockType != Material.SNOW) {
        return SpellResult.NO_TARGET;
    }
    controller.disablePhysics(1000);
    buildPortalBlocks(portalBase.getLocation(), BlockFace.NORTH);
    registerForUndo();
    return SpellResult.CAST;
}
Also used : Block(org.bukkit.block.Block) Material(org.bukkit.Material)

Example 59 with Material

use of org.bukkit.Material in project MagicPlugin by elBukkit.

the class TorchSpell method onCast.

@Override
public SpellResult onCast(ConfigurationSection parameters) {
    World world = getWorld();
    if (world == null) {
        return SpellResult.NO_TARGET;
    }
    if (parameters.contains("weather")) {
        String weatherString = parameters.getString("weather");
        if (weatherString.equals("storm")) {
            world.setStorm(true);
            world.setThundering(true);
        } else {
            world.setStorm(false);
            world.setThundering(false);
        }
    }
    if (parameters.contains("time")) {
        long targetTime = 0;
        timeType = parameters.getString("time", "day");
        if (timeType.equalsIgnoreCase("toggle")) {
            long currentTime = world.getTime();
            if (currentTime > 13000) {
                timeType = "day";
            } else {
                timeType = "night";
            }
        }
        if (timeType.equalsIgnoreCase("night")) {
            targetTime = 13000;
        } else {
            try {
                targetTime = Long.parseLong(timeType);
                timeType = "raw(" + targetTime + ")";
            } catch (NumberFormatException ex) {
                targetTime = 0;
            }
        }
        if (parameters.getBoolean("cycle_moon_phase", false)) {
            long currentTime = world.getFullTime();
            currentTime = ((currentTime % 24000) + 1) * 24000 + targetTime;
            world.setFullTime(currentTime);
            return SpellResult.ALTERNATE;
        }
        world.setTime(targetTime);
        return SpellResult.ALTERNATE;
    }
    boolean allowNight = parameters.getBoolean("allow_night", false);
    boolean allowDay = parameters.getBoolean("allow_day", false);
    if (isLookingUp() && allowDay) {
        timeType = "day";
        world.setTime(0);
        return SpellResult.ALTERNATE;
    }
    if (isLookingDown() && allowNight) {
        timeType = "night";
        world.setTime(13000);
        return SpellResult.ALTERNATE;
    }
    Block target = getTargetBlock();
    Block face = getPreviousBlock();
    if (target == null || face == null) {
        return SpellResult.NO_TARGET;
    }
    if (!hasBuildPermission(target)) {
        return SpellResult.INSUFFICIENT_PERMISSION;
    }
    boolean isAir = face.getType() == Material.AIR;
    boolean isAttachmentSlippery = target.getType() == Material.GLASS || target.getType() == Material.ICE;
    boolean replaceAttachment = target.getType() == Material.SNOW || target.getType() == Material.NETHERRACK || target.getType() == Material.SOUL_SAND;
    boolean isWater = face.getType() == Material.STATIONARY_WATER || face.getType() == Material.WATER;
    boolean isNether = target.getType() == Material.NETHERRACK || target.getType() == Material.SOUL_SAND;
    Material targetMaterial = Material.TORCH;
    if (isWater || isAttachmentSlippery || isNether) {
        targetMaterial = Material.GLOWSTONE;
        replaceAttachment = true;
    }
    boolean allowLightstone = parameters.getBoolean("allow_glowstone", false);
    if ((!isAir && !isWater) || (targetMaterial == Material.GLOWSTONE && !allowLightstone)) {
        return SpellResult.NO_TARGET;
    }
    BlockFace direction = face.getFace(target);
    if (!replaceAttachment) {
        target = face;
    }
    byte data = 0;
    if (targetMaterial == Material.TORCH) {
        switch(direction) {
            case WEST:
                data = 1;
                break;
            case EAST:
                data = 2;
                break;
            case NORTH:
                data = 3;
                break;
            case SOUTH:
                data = 4;
                break;
            case DOWN:
                data = 5;
                break;
            default:
                targetMaterial = Material.GLOWSTONE;
        }
    }
    if (!allowLightstone && targetMaterial == Material.GLOWSTONE) {
        return SpellResult.NO_TARGET;
    }
    registerForUndo(target);
    target.setTypeIdAndData(targetMaterial.getId(), data, false);
    registerForUndo();
    controller.updateBlock(target);
    return SpellResult.CAST;
}
Also used : BlockFace(org.bukkit.block.BlockFace) Block(org.bukkit.block.Block) Material(org.bukkit.Material) World(org.bukkit.World)

Example 60 with Material

use of org.bukkit.Material in project MagicPlugin by elBukkit.

the class TossSpell method onCast.

@Override
public SpellResult onCast(ConfigurationSection parameters) {
    Location location = getLocation();
    if (!hasBuildPermission(location.getBlock())) {
        return SpellResult.INSUFFICIENT_PERMISSION;
    }
    location.setY(location.getY() - 1);
    MaterialBrush buildWith = getBrush();
    buildWith.setTarget(location);
    Material material = buildWith.getMaterial();
    byte data = buildWith.getBlockData();
    int tossCount = 1;
    tossCount = parameters.getInt("count", tossCount);
    tossCount = (int) (mage.getRadiusMultiplier() * tossCount);
    float speed = 0.6f;
    speed = (float) parameters.getDouble("speed", speed);
    Vector direction = getDirection();
    direction.normalize().multiply(speed);
    Vector up = new Vector(0, 1, 0);
    Vector perp = new Vector();
    perp.copy(direction);
    perp.crossProduct(up);
    for (int i = 0; i < tossCount; i++) {
        FallingBlock block = null;
        location = getEyeLocation();
        location.setX(location.getX() + perp.getX() * (Math.random() * tossCount / 4 - tossCount / 8));
        location.setY(location.getY());
        location.setZ(location.getZ() + perp.getZ() * (Math.random() * tossCount / 4 - tossCount / 8));
        block = getWorld().spawnFallingBlock(location, material, data);
        if (block == null) {
            registerForUndo();
            return SpellResult.FAIL;
        }
        registerForUndo(block);
        block.setDropItem(false);
        SafetyUtils.setVelocity(block, direction);
    }
    registerForUndo();
    return SpellResult.CAST;
}
Also used : FallingBlock(org.bukkit.entity.FallingBlock) MaterialBrush(com.elmakers.mine.bukkit.api.block.MaterialBrush) Material(org.bukkit.Material) Vector(org.bukkit.util.Vector) Location(org.bukkit.Location)

Aggregations

Material (org.bukkit.Material)427 ItemStack (org.bukkit.inventory.ItemStack)99 Block (org.bukkit.block.Block)87 EventHandler (org.bukkit.event.EventHandler)51 ArrayList (java.util.ArrayList)46 Player (org.bukkit.entity.Player)44 Location (org.bukkit.Location)42 Vector (org.bukkit.util.Vector)27 EntityType (org.bukkit.entity.EntityType)26 GlowBlock (net.glowstone.block.GlowBlock)24 Test (org.junit.Test)24 BlockFace (org.bukkit.block.BlockFace)23 Entity (org.bukkit.entity.Entity)22 MaterialData (org.bukkit.material.MaterialData)22 World (org.bukkit.World)20 HashMap (java.util.HashMap)19 IOException (java.io.IOException)17 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)16 File (java.io.File)15 HashSet (java.util.HashSet)13