Search in sources :

Example 1 with PistonExtensionMaterial

use of org.bukkit.material.PistonExtensionMaterial in project Towny by ElgarL.

the class ProtectionRegenTask method replaceProtections.

public void replaceProtections() {
    try {
        Block block = state.getBlock();
        if (state.getData() instanceof Door) {
            Door door = (Door) state.getData();
            Block topHalf;
            Block bottomHalf;
            if (door.isTopHalf()) {
                topHalf = block;
                bottomHalf = block.getRelative(BlockFace.DOWN);
            } else {
                bottomHalf = block;
                topHalf = block.getRelative(BlockFace.UP);
            }
            door.setTopHalf(true);
            topHalf.setTypeIdAndData(state.getTypeId(), state.getData().getData(), false);
            door.setTopHalf(false);
            bottomHalf.setTypeIdAndData(state.getTypeId(), state.getData().getData(), false);
        } else if (state instanceof Sign) {
            block.setTypeIdAndData(state.getTypeId(), state.getData().getData(), false);
            Sign sign = (Sign) block.getState();
            int i = 0;
            for (String line : ((Sign) state).getLines()) sign.setLine(i++, line);
            sign.update(true);
        } else if (state instanceof CreatureSpawner) {
            block.setTypeIdAndData(state.getTypeId(), state.getData().getData(), false);
            ((CreatureSpawner) block.getState()).setSpawnedType(((CreatureSpawner) state).getSpawnedType());
        } else if (state instanceof InventoryHolder) {
            block.setTypeId(state.getTypeId(), false);
            // Container to receive the inventory
            Inventory container = ((InventoryHolder) block.getState()).getInventory();
            container.setContents(contents.toArray(new ItemStack[0]));
            block.setData(state.getData().getData(), false);
        } else if (state.getData() instanceof PistonExtensionMaterial) {
            PistonExtensionMaterial extension = (PistonExtensionMaterial) state.getData();
            Block piston = block.getRelative(extension.getAttachedFace());
            block.setTypeIdAndData(state.getTypeId(), state.getData().getData(), false);
            if (altState != null) {
                piston.setTypeIdAndData(altState.getTypeId(), altState.getData().getData(), false);
            }
        } else if (state.getData() instanceof Attachable) {
            Block attachedBlock = block.getRelative(((Attachable) state.getData()).getAttachedFace());
            if (attachedBlock.getTypeId() == 0) {
                attachedBlock.setTypeId(placeholder.getId(), false);
                TownyRegenAPI.addPlaceholder(attachedBlock);
            }
            block.setTypeIdAndData(state.getTypeId(), state.getData().getData(), false);
        } else {
            if (NeedsPlaceholder.contains(state.getType())) {
                Block blockBelow = block.getRelative(BlockFace.DOWN);
                if (blockBelow.getTypeId() == 0) {
                    if (state.getType().equals(Material.CROPS)) {
                        blockBelow.setTypeId(Material.SOIL.getId(), true);
                    } else {
                        blockBelow.setTypeId(placeholder.getId(), true);
                    }
                    TownyRegenAPI.addPlaceholder(blockBelow);
                }
            }
            block.setTypeIdAndData(state.getTypeId(), state.getData().getData(), !NeedsPlaceholder.contains(state.getType()));
        }
        TownyRegenAPI.removePlaceholder(block);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : PistonExtensionMaterial(org.bukkit.material.PistonExtensionMaterial) Block(org.bukkit.block.Block) Sign(org.bukkit.block.Sign) Attachable(org.bukkit.material.Attachable) InventoryHolder(org.bukkit.inventory.InventoryHolder) Door(org.bukkit.material.Door) Inventory(org.bukkit.inventory.Inventory) CreatureSpawner(org.bukkit.block.CreatureSpawner)

Example 2 with PistonExtensionMaterial

use of org.bukkit.material.PistonExtensionMaterial in project modules-extra by CubeEngine.

the class ListenerBlock method onPistonExtend.

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPistonExtend(final BlockPistonExtendEvent event) {
    if (this.isActive(BlockShift.class, event.getBlock().getWorld())) {
        boolean first = true;
        if (event.getBlocks().isEmpty()) {
            PistonExtensionMaterial pistonHead = PISTON_HEAD.clone();
            pistonHead.setSticky(event.isSticky());
            pistonHead.setFacingDirection(event.getDirection());
            Block firstBlock = event.getBlock().getRelative(event.getDirection());
            BlockState newState = firstBlock.getState();
            newState.setType(pistonHead.getItemType());
            newState.setData(pistonHead);
            BlockShift action = this.set(BlockShift.class, firstBlock.getState(), newState);
            action.setPush();
            this.logAction(action);
            return;
        }
        for (Block block : event.getBlocks()) {
            BlockState state = block.getState();
            if (first) {
                first = false;
                PistonExtensionMaterial pistonHead = PISTON_HEAD.clone();
                pistonHead.setSticky(event.isSticky());
                pistonHead.setFacingDirection(event.getDirection());
                BlockState newState = block.getState();
                newState.setType(pistonHead.getItemType());
                newState.setData(pistonHead);
                BlockShift action = this.set(BlockShift.class, state, newState);
                action.setPush();
                this.logAction(action);
            }
            if (block.getType() == AIR) {
                continue;
            }
            BlockState oldState = block.getRelative(event.getDirection()).getState();
            BlockState newState = oldState.getBlock().getState();
            newState.setType(state.getType());
            newState.setData(state.getData());
            BlockShift action = this.set(BlockShift.class, oldState, newState);
            action.setPush();
            this.logAction(action);
        }
    }
}
Also used : PistonExtensionMaterial(org.bukkit.material.PistonExtensionMaterial) BlockState(org.bukkit.block.BlockState) Block(org.bukkit.block.Block) ActionPlayerBlock(org.cubeengine.module.log.action.block.player.ActionPlayerBlock) EventHandler(org.bukkit.event.EventHandler)

Aggregations

Block (org.bukkit.block.Block)2 PistonExtensionMaterial (org.bukkit.material.PistonExtensionMaterial)2 BlockState (org.bukkit.block.BlockState)1 CreatureSpawner (org.bukkit.block.CreatureSpawner)1 Sign (org.bukkit.block.Sign)1 EventHandler (org.bukkit.event.EventHandler)1 Inventory (org.bukkit.inventory.Inventory)1 InventoryHolder (org.bukkit.inventory.InventoryHolder)1 Attachable (org.bukkit.material.Attachable)1 Door (org.bukkit.material.Door)1 ActionPlayerBlock (org.cubeengine.module.log.action.block.player.ActionPlayerBlock)1