Search in sources :

Example 6 with BlockStateChangeImpl

use of me.botsko.prism.events.BlockStateChangeImpl in project Prism-Bukkit by prism.

the class BlockAction method removeBlock.

private ChangeResult removeBlock(Player player, PrismParameters parameters, boolean isPreview, Block block) {
    BlockStateChangeImpl stateChange;
    if (!block.getType().equals(AIR)) {
        // Ensure it's acceptable to remove the current block
        if (!Utilities.isAcceptableForBlockPlace(block.getType()) && !Utilities.areBlockIdsSameCoreItem(block.getType(), getMaterial()) && !parameters.hasFlag(Flag.OVERWRITE)) {
            return new ChangeResultImpl(ChangeResultType.SKIPPED, null);
        }
        // Capture the block before we change it
        final BlockState originalBlock = block.getState();
        if (!isPreview) {
            // Set
            block.setType(AIR);
            // Capture the new state
            final BlockState newBlock = block.getState();
            // Store the state change
            stateChange = new BlockStateChangeImpl(originalBlock, newBlock);
        } else {
            // Otherwise, save the state so we can cancel if needed
            // Note: we save the original state as both old/new so we can
            // re-use blockStateChanges
            stateChange = new BlockStateChangeImpl(originalBlock, originalBlock);
            // Preview it
            EntityUtils.sendBlockChange(player, block.getLocation(), Bukkit.createBlockData(AIR));
            // Send preview to shared players
            for (final CommandSender sharedPlayer : parameters.getSharedPlayers()) {
                if (sharedPlayer instanceof Player) {
                    EntityUtils.sendBlockChange((Player) sharedPlayer, block.getLocation(), Bukkit.createBlockData(AIR));
                }
            }
        }
        return new ChangeResultImpl(ChangeResultType.APPLIED, stateChange);
    }
    return new ChangeResultImpl(ChangeResultType.SKIPPED, null);
}
Also used : Player(org.bukkit.entity.Player) BlockState(org.bukkit.block.BlockState) BlockStateChangeImpl(me.botsko.prism.events.BlockStateChangeImpl) CommandSender(org.bukkit.command.CommandSender) ChangeResultImpl(me.botsko.prism.appliers.ChangeResultImpl)

Example 7 with BlockStateChangeImpl

use of me.botsko.prism.events.BlockStateChangeImpl in project Prism-Bukkit by prism.

the class Utilities method removeMaterialsFromRadius.

/**
 * Remove materials in an radius.
 *
 * @param materials Material array
 * @param loc       Location
 * @param radius    integer
 */
@SuppressWarnings("WeakerAccess")
public static ArrayList<BlockStateChange> removeMaterialsFromRadius(Material[] materials, final Location loc, int radius) {
    final ArrayList<BlockStateChange> blockStateChanges = new ArrayList<>();
    if (loc != null && radius > 0 && materials != null && materials.length > 0) {
        final int x1 = loc.getBlockX();
        final int y1 = loc.getBlockY();
        final int z1 = loc.getBlockZ();
        final World world = loc.getWorld();
        for (int x = x1 - radius; x <= x1 + radius; x++) {
            for (int y = y1 - radius; y <= y1 + radius; y++) {
                for (int z = z1 - radius; z <= z1 + radius; z++) {
                    Location testLocation = new Location(world, x, y, z);
                    final Block b = testLocation.getBlock();
                    if (b.getType().equals(Material.AIR)) {
                        continue;
                    }
                    if (Arrays.asList(materials).contains(testLocation.getBlock().getType())) {
                        final BlockState originalBlock = testLocation.getBlock().getState();
                        testLocation.getBlock().setType(Material.AIR);
                        final BlockState newBlock = testLocation.getBlock().getState();
                        blockStateChanges.add(new BlockStateChangeImpl(originalBlock, newBlock));
                    }
                }
            }
        }
    }
    return blockStateChanges;
}
Also used : BlockStateChange(me.botsko.prism.api.BlockStateChange) BlockState(org.bukkit.block.BlockState) BlockStateChangeImpl(me.botsko.prism.events.BlockStateChangeImpl) ArrayList(java.util.ArrayList) Block(org.bukkit.block.Block) World(org.bukkit.World) Location(org.bukkit.Location)

Aggregations

BlockStateChangeImpl (me.botsko.prism.events.BlockStateChangeImpl)7 BlockState (org.bukkit.block.BlockState)7 ChangeResultImpl (me.botsko.prism.appliers.ChangeResultImpl)5 ArrayList (java.util.ArrayList)3 Block (org.bukkit.block.Block)3 Location (org.bukkit.Location)2 World (org.bukkit.World)2 BlockData (org.bukkit.block.data.BlockData)2 CommandSender (org.bukkit.command.CommandSender)2 Player (org.bukkit.entity.Player)2 NotNull (org.jetbrains.annotations.NotNull)2 BlockStateChange (me.botsko.prism.api.BlockStateChange)1 DyeColor (org.bukkit.DyeColor)1 Nameable (org.bukkit.Nameable)1 Banner (org.bukkit.block.Banner)1 CommandBlock (org.bukkit.block.CommandBlock)1 CreatureSpawner (org.bukkit.block.CreatureSpawner)1 Sign (org.bukkit.block.Sign)1 Skull (org.bukkit.block.Skull)1 Pattern (org.bukkit.block.banner.Pattern)1