Search in sources :

Example 1 with BlockStateChange

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

the class BlockAction method placeBlock.

/**
     * Place a block unless something other than air occupies the spot, or if we
     * detect a falling block now sits there. This resolves the issue of falling
     * blocks taking up the space, preventing this rollback. However, it also
     * means that a rollback *could* interfere with a player-placed block.
     */
protected ChangeResult placeBlock(Player player, QueryParameters parameters, boolean is_preview, Block block, boolean is_deferred) {
    final Material m = Material.getMaterial(getBlockId());
    BlockStateChange stateChange;
    // (essentially liquid/air).
    if (!getType().requiresHandler("BlockChangeAction") && !getType().requiresHandler("PrismRollbackAction")) {
        if (!me.botsko.elixr.BlockUtils.isAcceptableForBlockPlace(block.getType()) && !parameters.hasFlag(Flag.OVERWRITE)) {
            // System.out.print("Block skipped due to being unaccaptable for block place.");
            return new ChangeResult(ChangeResultType.SKIPPED, null);
        }
    }
    // On the blacklist (except an undo)
    if (Prism.getIllegalBlocks().contains(getBlockId()) && !parameters.getProcessType().equals(PrismProcessType.UNDO)) {
        // System.out.print("Block skipped because it's not allowed to be placed.");
        return new ChangeResult(ChangeResultType.SKIPPED, null);
    }
    // If we're not in a preview, actually apply this block
    if (!is_preview) {
        // Capture the block before we change it
        final BlockState originalBlock = block.getState();
        // it's set to stationary water so the lilypad will sit
        if (getBlockId() == 111) {
            final Block below = block.getRelative(BlockFace.DOWN);
            if (below.getType().equals(Material.WATER) || below.getType().equals(Material.AIR) || below.getType().equals(Material.STATIONARY_WATER)) {
                below.setType(Material.STATIONARY_WATER);
            } else {
                // Prism.debug("Lilypad skipped because no water exists below.");
                return new ChangeResult(ChangeResultType.SKIPPED, null);
            }
        }
        // If portal, we need to light the portal. seems to be the only way.
        if (getBlockId() == 90) {
            final Block obsidian = me.botsko.elixr.BlockUtils.getFirstBlockOfMaterialBelow(Material.OBSIDIAN, block.getLocation());
            if (obsidian != null) {
                final Block above = obsidian.getRelative(BlockFace.UP);
                if (!(above.getType() == Material.PORTAL)) {
                    above.setType(Material.FIRE);
                    return new ChangeResult(ChangeResultType.APPLIED, null);
                }
            }
        }
        // it becomes unplayable
        if (getBlockId() == 84) {
            block_subid = 0;
        }
        // Set the material
        block.setTypeId(getBlockId());
        block.setData((byte) getBlockSubId());
        /**
             * Skulls
             */
        if ((getBlockId() == 144 || getBlockId() == 397) && getActionData() instanceof SkullActionData) {
            final SkullActionData s = (SkullActionData) getActionData();
            // Set skull data
            final Skull skull = (Skull) block.getState();
            skull.setRotation(s.getRotation());
            skull.setSkullType(s.getSkullType());
            if (!s.owner.isEmpty()) {
                skull.setOwner(s.owner);
            }
            skull.update();
        }
        /**
             * Spawner
             */
        if (getBlockId() == 52) {
            final SpawnerActionData s = (SpawnerActionData) getActionData();
            // Set spawner data
            final CreatureSpawner spawner = (CreatureSpawner) block.getState();
            spawner.setDelay(s.getDelay());
            spawner.setSpawnedType(s.getEntityType());
            spawner.update();
        }
        /**
             * Restoring command block
             */
        if (getBlockId() == 137) {
            final CommandBlock cmdblock = (CommandBlock) block.getState();
            cmdblock.setCommand(data);
            cmdblock.update();
        }
        /**
             * Signs
             */
        if (parameters.getProcessType().equals(PrismProcessType.ROLLBACK) && (getBlockId() == 63 || getBlockId() == 68) && getActionData() instanceof SignActionData) {
            final SignActionData s = (SignActionData) getActionData();
            // https://snowy-evening.com/botsko/prism/455/
            if (block.getState() instanceof Sign) {
                // Set sign data
                final Sign sign = (Sign) block.getState();
                int i = 0;
                if (s.lines != null && s.lines.length > 0) {
                    for (final String line : s.lines) {
                        sign.setLine(i, line);
                        i++;
                    }
                }
                sign.update();
            }
        }
        // logic to use materials.
        if (me.botsko.elixr.BlockUtils.materialRequiresSoil(block.getType())) {
            final Block below = block.getRelative(BlockFace.DOWN);
            if (below.getType().equals(Material.DIRT) || below.getType().equals(Material.AIR) || below.getType().equals(Material.GRASS)) {
                below.setType(Material.SOIL);
            } else {
                // System.out.print("Block skipped because there's no soil below.");
                return new ChangeResult(ChangeResultType.SKIPPED, null);
            }
        }
        // Capture the new state
        final BlockState newBlock = block.getState();
        // Store the state change
        stateChange = new BlockStateChange(originalBlock, newBlock);
        // If we're rolling back a door, we need to set it properly
        if (BlockUtils.isDoor(m)) {
            BlockUtils.properlySetDoor(block, getBlockId(), (byte) getBlockSubId());
        } else // Or a bed
        if (m.equals(Material.BED_BLOCK)) {
            BlockUtils.properlySetBed(block, getBlockId(), (byte) getBlockSubId());
        } else // Or double plants
        if (m.equals(Material.DOUBLE_PLANT)) {
            BlockUtils.properlySetDoublePlant(block, getBlockId(), (byte) getBlockSubId());
        }
    } else {
        // Otherwise, save the state so we can cancel if needed
        final BlockState originalBlock = block.getState();
        // Note: we save the original state as both old/new so we can re-use
        // blockStateChanges
        stateChange = new BlockStateChange(originalBlock, originalBlock);
        // Preview it
        player.sendBlockChange(block.getLocation(), getBlockId(), (byte) getBlockSubId());
        // Send preview to shared players
        for (final CommandSender sharedPlayer : parameters.getSharedPlayers()) {
            if (sharedPlayer instanceof Player) {
                ((Player) sharedPlayer).sendBlockChange(block.getLocation(), getBlockId(), (byte) getBlockSubId());
            }
        }
    }
    return new ChangeResult(ChangeResultType.APPLIED, stateChange);
}
Also used : Player(org.bukkit.entity.Player) BlockStateChange(me.botsko.prism.events.BlockStateChange) Material(org.bukkit.Material) CommandBlock(org.bukkit.block.CommandBlock) ChangeResult(me.botsko.prism.appliers.ChangeResult) CreatureSpawner(org.bukkit.block.CreatureSpawner) BlockState(org.bukkit.block.BlockState) CommandBlock(org.bukkit.block.CommandBlock) Block(org.bukkit.block.Block) Skull(org.bukkit.block.Skull) Sign(org.bukkit.block.Sign) CommandSender(org.bukkit.command.CommandSender)

Example 2 with BlockStateChange

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

the class BlockAction method removeBlock.

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

Example 3 with BlockStateChange

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

the class BlockUtils method removeMaterialsFromRadius.

/**
     * 
     * @param materials
     * @param loc
     * @param radius
     */
public static ArrayList<BlockStateChange> removeMaterialsFromRadius(Material[] materials, Location loc, int radius) {
    final ArrayList<BlockStateChange> blockStateChanges = new ArrayList<BlockStateChange>();
    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++) {
                    loc = new Location(world, x, y, z);
                    final Block b = loc.getBlock();
                    if (b.getType().equals(Material.AIR))
                        continue;
                    if (Arrays.asList(materials).contains(loc.getBlock().getType())) {
                        final BlockState originalBlock = loc.getBlock().getState();
                        loc.getBlock().setType(Material.AIR);
                        final BlockState newBlock = loc.getBlock().getState();
                        blockStateChanges.add(new BlockStateChange(originalBlock, newBlock));
                    }
                }
            }
        }
    }
    return blockStateChanges;
}
Also used : BlockStateChange(me.botsko.prism.events.BlockStateChange) BlockState(org.bukkit.block.BlockState) ArrayList(java.util.ArrayList) Block(org.bukkit.block.Block) World(org.bukkit.World) Location(org.bukkit.Location)

Example 4 with BlockStateChange

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

the class PrismMiscEvents method onPrismBlocksExtinguishEvent.

/**
     * 
     * @param event
     */
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPrismBlocksExtinguishEvent(final PrismBlocksExtinguishEvent event) {
    // Get all block changes for this event
    final ArrayList<BlockStateChange> blockStateChanges = event.getBlockStateChanges();
    if (!blockStateChanges.isEmpty()) {
        // Create an entry for the rollback as a whole
        final Handler primaryAction = ActionFactory.createPrismProcess("prism-process", PrismProcessType.EXTINGUISH, event.onBehalfOf(), "" + event.getRadius());
        final int id = RecordingTask.insertActionIntoDatabase(primaryAction);
        if (id == 0) {
            return;
        }
        for (final BlockStateChange stateChange : blockStateChanges) {
            final BlockState orig = stateChange.getOriginalBlock();
            final BlockState newBlock = stateChange.getNewBlock();
            // Build the action
            RecordingQueue.addToQueue(ActionFactory.createPrismRollback("prism-extinguish", orig, newBlock, event.onBehalfOf().getName(), id));
        }
    // ActionQueue.save();
    }
}
Also used : BlockStateChange(me.botsko.prism.events.BlockStateChange) BlockState(org.bukkit.block.BlockState) Handler(me.botsko.prism.actions.Handler) EventHandler(org.bukkit.event.EventHandler) EventHandler(org.bukkit.event.EventHandler)

Example 5 with BlockStateChange

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

the class PrismMiscEvents method onPrismBlocksDrainEvent.

/**
     * 
     * @param event
     */
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPrismBlocksDrainEvent(final PrismBlocksDrainEvent event) {
    // Get all block changes for this event
    final ArrayList<BlockStateChange> blockStateChanges = event.getBlockStateChanges();
    if (!blockStateChanges.isEmpty()) {
        // Create an entry for the rollback as a whole
        final Handler primaryAction = ActionFactory.createPrismProcess("prism-process", PrismProcessType.DRAIN, event.onBehalfOf(), "" + event.getRadius());
        final int id = RecordingTask.insertActionIntoDatabase(primaryAction);
        if (id == 0) {
            return;
        }
        for (final BlockStateChange stateChange : blockStateChanges) {
            final BlockState orig = stateChange.getOriginalBlock();
            final BlockState newBlock = stateChange.getNewBlock();
            // Build the action
            RecordingQueue.addToQueue(ActionFactory.createPrismRollback("prism-drain", orig, newBlock, event.onBehalfOf().getName(), id));
        }
    // ActionQueue.save();
    }
}
Also used : BlockStateChange(me.botsko.prism.events.BlockStateChange) BlockState(org.bukkit.block.BlockState) Handler(me.botsko.prism.actions.Handler) EventHandler(org.bukkit.event.EventHandler) EventHandler(org.bukkit.event.EventHandler)

Aggregations

BlockStateChange (me.botsko.prism.events.BlockStateChange)5 BlockState (org.bukkit.block.BlockState)5 Handler (me.botsko.prism.actions.Handler)2 ChangeResult (me.botsko.prism.appliers.ChangeResult)2 Block (org.bukkit.block.Block)2 CommandSender (org.bukkit.command.CommandSender)2 Player (org.bukkit.entity.Player)2 EventHandler (org.bukkit.event.EventHandler)2 ArrayList (java.util.ArrayList)1 Location (org.bukkit.Location)1 Material (org.bukkit.Material)1 World (org.bukkit.World)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