Search in sources :

Example 1 with NoteBlock

use of org.bukkit.block.NoteBlock in project modules-extra by CubeEngine.

the class ActionBlock method setBlock.

protected boolean setBlock(BlockSection blockData, Location loc, LogAttachment attachment, boolean force, boolean preview, boolean rollback) {
    Block block = loc.getBlock();
    BlockState state = loc.getBlock().getState();
    state.setType(blockData.material);
    if (blockData.material == IRON_DOOR_BLOCK || blockData.material == WOODEN_DOOR) {
        // TODO correct?
        byte data = (byte) (blockData.data & ~8);
        state.setRawData(data);
    } else {
        state.setRawData(blockData.data);
    }
    if (!force && (// TODO correct?
    state.getData() instanceof Attachable || BlockUtil.isDetachableFromBelow(blockData.material))) {
        return false;
    }
    if (state.getData() instanceof Bed) {
        Bed bed = (Bed) state.getData();
        Block headBed = block.getRelative(bed.getFacing());
        BlockState headState = headBed.getState();
        headState.setType(AIR);
        if (preview) {
            attachment.addToPreview(headState);
        } else {
            headState.update(true, false);
        }
    } else if (state.getType() == WOOD_DOOR || state.getType() == IRON_DOOR_BLOCK) {
        Block topDoor = block.getRelative(UP);
        if (topDoor.getType() == state.getType()) {
            BlockState topState = topDoor.getState();
            topState.setType(AIR);
            if (preview) {
                attachment.addToPreview(topState);
            } else {
                topState.update(true, false);
            }
        }
    }
    if (preview) {
        attachment.addToPreview(state);
    } else {
        state.update(true, false);
    }
    if (rollback) {
        if (this instanceof SignBreak || this instanceof PlayerSignBreak) {
            String[] lines = this instanceof SignBreak ? ((SignBreak) this).oldLines : ((PlayerSignBreak) this).oldLines;
            if (preview) {
                attachment.addToPreview(state.getLocation(), lines);
            } else {
                Sign sign = (Sign) state.getBlock().getState();
                int i = 0;
                for (String line : lines) {
                    sign.setLine(i++, line);
                }
                sign.update();
            }
        } else if (blockData.is(BED_BLOCK)) {
            Bed bed = (Bed) state.getData();
            BlockState headBed = block.getRelative(bed.getFacing()).getState();
            headBed.setType(BED_BLOCK);
            Bed bedhead = (Bed) headBed.getData();
            bedhead.setHeadOfBed(true);
            bedhead.setFacingDirection(bed.getFacing());
            if (preview) {
                attachment.addToPreview(headBed);
            } else {
                headBed.update(true);
            }
        } else if (blockData.is(WOOD_DOOR, IRON_DOOR_BLOCK)) {
            byte data = (byte) (((blockData.data & 8) == 8) ? 9 : 8);
            BlockState topDoor = block.getRelative(UP).getState();
            topDoor.setType(state.getType());
            topDoor.setRawData(data);
            if (preview) {
                attachment.addToPreview(topDoor);
            } else {
                topDoor.update(true);
            }
        } else if (!preview) {
            if (this instanceof PlayerNoteBlockBreak) {
                NoteBlock noteblock = (NoteBlock) state.getBlock().getState();
                noteblock.setNote(((PlayerNoteBlockBreak) this).note);
                noteblock.update();
            } else if (this instanceof PlayerJukeboxBreak) {
                Jukebox jukebox = (Jukebox) state.getBlock().getState();
                jukebox.setPlaying(((PlayerJukeboxBreak) this).disc);
                jukebox.update();
            } else if (this instanceof PlayerContainerBreak) {
                InventoryHolder inventoryHolder = (InventoryHolder) state.getBlock().getState();
                inventoryHolder.getInventory().setContents(((PlayerContainerBreak) this).contents);
                ((BlockState) inventoryHolder).update();
            }
        }
    }
    return true;
}
Also used : Bed(org.bukkit.material.Bed) Jukebox(org.bukkit.block.Jukebox) PlayerSignBreak(org.cubeengine.module.log.action.block.player.destroy.PlayerSignBreak) NoteBlock(org.bukkit.block.NoteBlock) PlayerContainerBreak(org.cubeengine.module.log.action.block.player.destroy.PlayerContainerBreak) PlayerNoteBlockBreak(org.cubeengine.module.log.action.block.player.destroy.PlayerNoteBlockBreak) BlockState(org.bukkit.block.BlockState) PlayerJukeboxBreak(org.cubeengine.module.log.action.block.player.destroy.PlayerJukeboxBreak) Block(org.bukkit.block.Block) NoteBlock(org.bukkit.block.NoteBlock) Sign(org.bukkit.block.Sign) PlayerSignBreak(org.cubeengine.module.log.action.block.player.destroy.PlayerSignBreak) Attachable(org.bukkit.material.Attachable) InventoryHolder(org.bukkit.inventory.InventoryHolder)

Example 2 with NoteBlock

use of org.bukkit.block.NoteBlock in project modules-extra by CubeEngine.

the class ListenerPlayerBlock method onBlockBreak.

// Doors / Beds only logged bottom / feet
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onBlockBreak(BlockBreakEvent event) {
    if (event.getBlock().getType() == AIR) {
        // breaking air !? -> no logging
        return;
    }
    if (!this.isActive(PlayerBlockBreak.class, event.getBlock().getWorld())) {
        return;
    }
    BlockState blockState = event.getBlock().getState();
    PlayerBlockBreak action;
    if (blockState instanceof NoteBlock) {
        action = this.newAction(PlayerNoteBlockBreak.class);
        ((PlayerNoteBlockBreak) action).setNote(((NoteBlock) blockState).getNote());
    } else if (blockState instanceof Sign) {
        action = this.newAction(PlayerSignBreak.class);
        ((PlayerSignBreak) action).setLines(((Sign) blockState).getLines());
    } else if (blockState instanceof Jukebox && ((Jukebox) blockState).getPlaying() != null) {
        action = this.newAction(PlayerJukeboxBreak.class);
        ((PlayerJukeboxBreak) action).setDisc(((Jukebox) blockState).getPlaying());
    } else if (blockState instanceof InventoryHolder) {
        action = this.newAction(PlayerContainerBreak.class, event.getBlock().getWorld());
        if (action == null) {
            action = this.newAction(PlayerBlockBreak.class);
        } else {
            ((PlayerContainerBreak) action).setContents(((InventoryHolder) blockState).getInventory().getContents());
        }
    // TODO item drops
    // itemDrop.logDropsFromChest((InventoryHolder)blockState,location,event.getPlayer());
    } else {
        action = this.newAction(PlayerBlockBreak.class);
        // WOOD_DOOR IRON_DOOR OR BED_BLOCK
        blockState = adjustBlockForDoubleBlocks(blockState);
    }
    action.setPlayer(event.getPlayer());
    action.setLocation(event.getBlock().getLocation());
    action.setOldBlock(blockState);
    action.setNewBlock(AIR);
    this.logAction(action);
    if (// portal?
    blockState.getType() == OBSIDIAN) {
        // TODO better & complete
        Block block = blockState.getBlock();
        for (BlockFace face : BLOCK_FACES) {
            if (block.getRelative(face).getType() == PORTAL) {
                Block portal = block.getRelative(face);
                PlayerBlockBreak pAction = this.newAction(PlayerBlockBreak.class);
                pAction.setPlayer(event.getPlayer());
                pAction.setLocation(portal.getLocation());
                pAction.setOldBlock(portal.getState());
                pAction.setNewBlock(AIR);
                pAction.reference = this.reference(action);
                this.logAction(pAction);
                break;
            }
        }
    }
    // TODO attached & falling
    ListenerBlock.logAttachedBlocks(this, module.getCore().getEventManager(), event.getBlock(), action);
    ListenerBlock.logFallingBlocks(this, module.getCore().getEventManager(), event.getBlock(), action);
}
Also used : PlayerBlockBreak(org.cubeengine.module.log.action.block.player.destroy.PlayerBlockBreak) Jukebox(org.bukkit.block.Jukebox) BlockState(org.bukkit.block.BlockState) PlayerJukeboxBreak(org.cubeengine.module.log.action.block.player.destroy.PlayerJukeboxBreak) NoteBlock(org.bukkit.block.NoteBlock) BlockFace(org.bukkit.block.BlockFace) ListenerBlock(org.cubeengine.module.log.action.block.ListenerBlock) Block(org.bukkit.block.Block) NoteBlock(org.bukkit.block.NoteBlock) Sign(org.bukkit.block.Sign) InventoryHolder(org.bukkit.inventory.InventoryHolder) PlayerNoteBlockBreak(org.cubeengine.module.log.action.block.player.destroy.PlayerNoteBlockBreak) EventHandler(org.bukkit.event.EventHandler)

Example 3 with NoteBlock

use of org.bukkit.block.NoteBlock in project modules-extra by CubeEngine.

the class ListenerPlayerBlockInteract method onPlayerInteract.

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerInteract(PlayerInteractEvent event) {
    // TODO put item into itemframe
    if (event.getAction() == RIGHT_CLICK_BLOCK) {
        ItemStack itemInHand = event.getPlayer().getItemInHand();
        Location location = event.getClickedBlock().getLocation();
        BlockState state = event.getClickedBlock().getState();
        ActionPlayerBlock action;
        BlockState newState = state.getBlock().getState();
        if (state instanceof InventoryHolder) {
            action = this.newAction(UseContainer.class, state.getWorld());
        } else if (state.getData() instanceof Openable) {
            action = this.newAction(UseDoor.class, state.getWorld());
            if (action != null) {
                state = adjustBlockForDoubleBlocks(state);
                Openable openable = (Openable) state.getBlock().getState().getData();
                openable.setOpen(!openable.isOpen());
                newState.setData((MaterialData) openable);
            }
        } else if (state.getData() instanceof Lever) {
            action = this.newAction(UseLever.class, state.getWorld());
            if (action != null) {
                Lever leverData = (Lever) state.getBlock().getState().getData();
                leverData.setPowered(!leverData.isPowered());
                newState.setData(leverData);
            }
        } else if (state.getType() == REDSTONE_COMPARATOR_ON || state.getType() == REDSTONE_COMPARATOR_OFF) {
            action = this.newAction(UseComparator.class, state.getWorld());
            if (action != null) {
                newState.setType(state.getType() == REDSTONE_COMPARATOR_ON ? REDSTONE_COMPARATOR_OFF : REDSTONE_COMPARATOR_ON);
            }
        } else if (state.getData() instanceof Button) {
            action = this.newAction(UseButton.class, state.getWorld());
            if (action != null) {
                Button button = (Button) state.getBlock().getState().getData();
                button.setPowered(true);
                newState.setData(button);
            }
        } else if (state.getData() instanceof Rails) {
            if (itemInHand.getType() == MINECART || itemInHand.getType() == STORAGE_MINECART || itemInHand.getType() == POWERED_MINECART || itemInHand.getType() == HOPPER_MINECART || // BOAT is done down below
            itemInHand.getType() == EXPLOSIVE_MINECART) {
                VehiclePrePlaceEvent vEvent = new VehiclePrePlaceEvent(event.getClickedBlock().getRelative(UP).getLocation(), event.getPlayer());
                this.module.getCore().getEventManager().fireEvent(vEvent);
            }
            action = null;
        } else if (itemInHand.getType().equals(BOAT)) {
            VehiclePrePlaceEvent vEvent = new VehiclePrePlaceEvent(event.getClickedBlock().getRelative(UP).getLocation(), event.getPlayer());
            this.module.getCore().getEventManager().fireEvent(vEvent);
            action = null;
        } else if (state.getData() instanceof Cake) {
            action = null;
            if (event.getPlayer().getFoodLevel() < 20 && !(event.getPlayer().getGameMode() == CREATIVE)) {
                action = this.newAction(UseCake.class, state.getWorld());
                if (action != null) {
                    Cake cake = (Cake) state.getBlock().getState().getData();
                    cake.setSlicesEaten(cake.getSlicesEaten() + 1);
                    if (cake.getSlicesRemaining() == 0) {
                        action.setNewBlock(AIR);
                        newState = null;
                    } else {
                        newState.setData(cake);
                    }
                }
            }
        } else if (state instanceof NoteBlock) {
            action = this.newAction(UseNoteblock.class, state.getWorld());
            if (action != null) {
                try {
                    ((NoteBlock) newState).setNote(((NoteBlock) newState).getNote().sharped());
                } catch (IllegalArgumentException e) {
                    ((NoteBlock) newState).setNote(new Note(1, Tone.F, true));
                }
            }
        } else if (state.getData() instanceof Diode) {
            action = this.newAction(UseRepeater.class, state.getWorld());
            if (action != null) {
                Diode diode = (Diode) state.getBlock().getState().getData();
                Integer delay = diode.getDelay() + 1;
                if (delay == 5) {
                    delay = 1;
                }
                diode.setDelay(delay);
                newState.setData(diode);
            }
        } else if (state.getType() == TNT) {
            action = null;
            if (itemInHand.getType() == FLINT_AND_STEEL) {
                action = this.newAction(UseTnt.class, state.getWorld());
                if (action != null) {
                    action.setNewBlock(AIR);
                    newState = null;
                }
            }
        } else if (state.getData() instanceof Crops || state.getType() == GRASS) {
            if (itemInHand.getData() instanceof Dye && ((Dye) itemInHand.getData()).getColor() == WHITE) {
                // TODO THIS IS BULLSHIT I need an event for bonemealUse...
                action = this.newAction(UseBonemeal.class, state.getWorld());
                if (action != null) {
                    // TODO adjust growth stage
                    // TODO do not log if fully grown
                    action.setNewBlock(state);
                    action.setLocation(state.getLocation());
                    action.setOldBlock(state);
                    action.setPlayer(event.getPlayer());
                    this.logAction(action);
                }
            }
            return;
        } else if (state instanceof Jukebox) {
            // TODO
            action = null;
        } else {
            action = null;
        }
        if (action != null) {
            action.setLocation(state.getLocation());
            action.setOldBlock(state);
            if (newState != null) {
                action.setNewBlock(newState);
            }
            action.setPlayer(event.getPlayer());
            this.logAction(action);
        }
    } else if (event.getAction() == PHYSICAL) {
        Block block = event.getClickedBlock();
        if (block.getType() == SOIL) {
            BlockTrample action = this.newAction(BlockTrample.class, block.getWorld());
            if (action != null) {
                BlockState stateUp = block.getRelative(UP).getState();
                if (stateUp.getData() instanceof Crops) {
                    BlockTrample actionUp = this.newAction(BlockTrample.class, block.getWorld());
                    actionUp.setLocation(stateUp.getLocation());
                    actionUp.setOldBlock(stateUp);
                    actionUp.setNewBlock(AIR);
                    actionUp.setPlayer(event.getPlayer());
                    this.logAction(actionUp);
                }
                action.setLocation(block.getLocation());
                action.setOldBlock(block.getState());
                action.setNewBlock(DIRT);
                action.setPlayer(event.getPlayer());
                this.logAction(action);
            }
        } else if (block.getState().getData() instanceof PressurePlate) {
            UsePlate action = this.newAction(UsePlate.class, block.getWorld());
            if (action != null) {
                // BlockState newState = block.getState();
                // PressurePlate plate = (PressurePlate)newState.getData();
                action.setLocation(block.getLocation());
                action.setOldBlock(block.getState());
                action.setNewBlock(block.getState());
                action.setPlayer(event.getPlayer());
                this.logAction(action);
            }
        }
    }
}
Also used : Diode(org.bukkit.material.Diode) Cake(org.bukkit.material.Cake) Openable(org.bukkit.material.Openable) Dye(org.bukkit.material.Dye) Button(org.bukkit.material.Button) PressurePlate(org.bukkit.material.PressurePlate) InventoryHolder(org.bukkit.inventory.InventoryHolder) Jukebox(org.bukkit.block.Jukebox) ActionPlayerBlock(org.cubeengine.module.log.action.block.player.ActionPlayerBlock) NoteBlock(org.bukkit.block.NoteBlock) Crops(org.bukkit.material.Crops) Rails(org.bukkit.material.Rails) BlockState(org.bukkit.block.BlockState) Lever(org.bukkit.material.Lever) Note(org.bukkit.Note) VehiclePrePlaceEvent(org.cubeengine.module.log.action.vehicle.VehiclePrePlaceEvent) Block(org.bukkit.block.Block) NoteBlock(org.bukkit.block.NoteBlock) ActionPlayerBlock(org.cubeengine.module.log.action.block.player.ActionPlayerBlock) MaterialData(org.bukkit.material.MaterialData) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Location(org.spongepowered.api.world.Location) EventHandler(org.bukkit.event.EventHandler)

Aggregations

Block (org.bukkit.block.Block)3 BlockState (org.bukkit.block.BlockState)3 Jukebox (org.bukkit.block.Jukebox)3 NoteBlock (org.bukkit.block.NoteBlock)3 InventoryHolder (org.bukkit.inventory.InventoryHolder)3 Sign (org.bukkit.block.Sign)2 EventHandler (org.bukkit.event.EventHandler)2 PlayerJukeboxBreak (org.cubeengine.module.log.action.block.player.destroy.PlayerJukeboxBreak)2 PlayerNoteBlockBreak (org.cubeengine.module.log.action.block.player.destroy.PlayerNoteBlockBreak)2 Note (org.bukkit.Note)1 BlockFace (org.bukkit.block.BlockFace)1 Attachable (org.bukkit.material.Attachable)1 Bed (org.bukkit.material.Bed)1 Button (org.bukkit.material.Button)1 Cake (org.bukkit.material.Cake)1 Crops (org.bukkit.material.Crops)1 Diode (org.bukkit.material.Diode)1 Dye (org.bukkit.material.Dye)1 Lever (org.bukkit.material.Lever)1 MaterialData (org.bukkit.material.MaterialData)1