Search in sources :

Example 96 with BlockState

use of org.bukkit.block.BlockState 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 97 with BlockState

use of org.bukkit.block.BlockState in project Prism-Bukkit by prism.

the class PrismWorldEvents method onStructureGrow.

/**
 * StructureGrowEvent.
 * @param event StructureGrowEvent
 */
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onStructureGrow(final StructureGrowEvent event) {
    String type = "tree-grow";
    final TreeType species = event.getSpecies();
    if (species.name().toLowerCase().contains("mushroom")) {
        type = "mushroom-grow";
    }
    if (!Prism.getIgnore().event(type, event.getWorld())) {
        return;
    }
    for (final BlockState block : event.getBlocks()) {
        if (Utilities.isGrowableStructure(block.getType())) {
            if (event.getPlayer() != null) {
                RecordingQueue.addToQueue(ActionFactory.createGrow(type, block, event.getPlayer()));
            } else {
                RecordingQueue.addToQueue(ActionFactory.createGrow(type, block, "Environment"));
            }
        }
    }
}
Also used : TreeType(org.bukkit.TreeType) BlockState(org.bukkit.block.BlockState) EventHandler(org.bukkit.event.EventHandler)

Example 98 with BlockState

use of org.bukkit.block.BlockState in project Prism-Bukkit by prism.

the class PrismWorldEvents method onPortalCreate.

/**
 * Track portal creation events.
 * @param event PortalCreateEvent.
 */
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPortalCreate(final PortalCreateEvent event) {
    final String type = "portal-create";
    if (!Prism.getIgnore().event(type, event.getWorld())) {
        return;
    }
    if (event.getReason() == PortalCreateEvent.CreateReason.FIRE) {
        for (final BlockState newBlock : event.getBlocks()) {
            // Include only the nether portal blocks that were created.
            if (newBlock.getType() == Material.NETHER_PORTAL) {
                final Entity e = event.getEntity();
                final BlockState oldBlock = event.getWorld().getBlockAt(newBlock.getLocation()).getState();
                if (e != null) {
                    // Run the second after the fire was placed (20 ticks), so that it is recorded after the fire.
                    // We have to do this because the database only records changes per second, not tick or instant,
                    // which can result in the fire being recorded after the nether portal blocks.
                    Bukkit.getScheduler().runTaskLater(Prism.getInstance(), () -> {
                        recordCreatePortal(event, type, newBlock, e, oldBlock);
                    }, 20);
                }
            }
        }
    } else if (event.getReason() == PortalCreateEvent.CreateReason.NETHER_PAIR) {
        // Record both the obsidian and portal blocks that were created.
        for (final BlockState newBlock : event.getBlocks()) {
            final Entity e = event.getEntity();
            final BlockState oldBlock = event.getWorld().getBlockAt(newBlock.getLocation()).getState();
            if (e != null) {
                if (newBlock.getType() == Material.NETHER_PORTAL) {
                    // Run the second after the fire was placed (20 ticks), so that it is recorded after the fire.
                    // We have to do this because the database only records changes per second, not tick or instant,
                    // which can result in the fire being recorded after the nether portal blocks.
                    Bukkit.getScheduler().runTaskLater(Prism.getInstance(), () -> {
                        recordCreatePortal(event, type, newBlock, e, oldBlock);
                    }, 20);
                } else {
                    recordCreatePortal(event, type, newBlock, e, oldBlock);
                }
            }
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) BlockState(org.bukkit.block.BlockState) EventHandler(org.bukkit.event.EventHandler)

Example 99 with BlockState

use of org.bukkit.block.BlockState 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)

Example 100 with BlockState

use of org.bukkit.block.BlockState in project Prism-Bukkit by prism.

the class PrismBlockEvents method onBlockSpread.

/**
 * BlockSpreadEvent.
 * @param event BlockSpreadEvent
 */
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockSpread(final BlockSpreadEvent event) {
    // If fire, do we track fire spread? If not, do we track block-spread
    String type = "block-spread";
    if (event.getNewState().getType().equals(Material.FIRE)) {
        if (!Prism.getIgnore().event("fire-spread")) {
            return;
        }
        type = "fire-spread";
    } else {
        if (!Prism.getIgnore().event("block-spread", event.getBlock())) {
            return;
        }
    }
    final Block b = event.getBlock();
    final BlockState s = event.getNewState();
    RecordingQueue.addToQueue(ActionFactory.createBlockChange(type, b.getLocation(), b.getType(), b.getBlockData(), s.getType(), s.getBlockData(), "Environment"));
}
Also used : BlockState(org.bukkit.block.BlockState) Block(org.bukkit.block.Block) EventHandler(org.bukkit.event.EventHandler)

Aggregations

BlockState (org.bukkit.block.BlockState)126 Block (org.bukkit.block.Block)53 EventHandler (org.bukkit.event.EventHandler)34 Location (org.bukkit.Location)16 Sign (org.bukkit.block.Sign)15 World (org.bukkit.World)14 InventoryHolder (org.bukkit.inventory.InventoryHolder)14 Material (org.bukkit.Material)13 MaterialData (org.bukkit.material.MaterialData)13 ItemStack (org.bukkit.inventory.ItemStack)11 ArrayList (java.util.ArrayList)10 CreatureSpawner (org.bukkit.block.CreatureSpawner)10 BlockFace (org.bukkit.block.BlockFace)9 Player (org.bukkit.entity.Player)9 BlockStateChangeImpl (me.botsko.prism.events.BlockStateChangeImpl)7 Vector (org.bukkit.util.Vector)7 ChangeResultImpl (me.botsko.prism.appliers.ChangeResultImpl)5 Chunk (org.bukkit.Chunk)5 DyeColor (org.bukkit.DyeColor)5 CommandBlock (org.bukkit.block.CommandBlock)5