Package net.glowstone.block

Examples of net.glowstone.block.GlowBlock


    ////////////////////////////////////////////////////////////////////////////
    // get block, chunk, id, highest methods with coords

    @Override
    public GlowBlock getBlockAt(int x, int y, int z) {
        return new GlowBlock(getChunkAt(x >> 4, z >> 4), x, y & 0xff, z);
    }
View Full Code Here


        return z;
    }

    @Override
    public GlowBlock getBlock(int x, int y, int z) {
        return new GlowBlock(this, (this.x << 4) | (x & 0xf), y & 0xff, (this.z << 4) | (z & 0xf));
    }
View Full Code Here

        setMaxStackSize(1);
    }

    @Override
    public void rightClickBlock(GlowPlayer player, GlowBlock against, BlockFace face, ItemStack holding, Vector clickedLoc) {
        GlowBlock target = against.getRelative(face);
        BlockType againstBlockType = ItemTable.instance().getBlock(against.getType());

        // only allow placement inside replaceable blocks
        if (againstBlockType.canAbsorb(target, face, holding)) {
            target = against;
        } else if (!target.isEmpty()) {
            BlockType targetType = ItemTable.instance().getBlock(target.getTypeId());
            if (!targetType.canOverride(target, face, holding)) {
                return;
            }
        }

        GlowBlockState newState = target.getState();

        PlayerBucketEmptyEvent event = EventFactory.callEvent(new PlayerBucketEmptyEvent(player, target, face, holding.getType(), holding));
        if (event.isCancelled()) {
            return;
        }
View Full Code Here

     * Checks for a completed portal at all relevant positions.
     */
    private void searchForCompletedPortal(GlowPlayer player, GlowBlock changed) {
        for (int i = 0; i < 4; i++) {
            for (int j = -1; j <= 1; j++) {
                GlowBlock center = changed.getRelative(DIRECTION[i], 2).getRelative(DIRECTION[(i + 1) % 4], j);
                if (isCompletedPortal(center)) {
                    createPortal(player, center);
                    return;
                }
            }
View Full Code Here

     * Check whether there is a completed portal with the specified center.
     */
    private boolean isCompletedPortal(GlowBlock center) {
        for (int i = 0; i < 4; i++) {
            for (int j = -1; j <= 1; j++) {
                GlowBlock block = center.getRelative(DIRECTION[i], 2).getRelative(DIRECTION[(i + 1) % 4], j);
                if (block.getType() != Material.ENDER_PORTAL_FRAME || (block.getData() & 0x4) == 0) {
                    return false;
                }
            }
        }
        return true;
View Full Code Here

    @Override
    public void afterPlace(GlowPlayer player, GlowBlock block, ItemStack holding) {
        if (block.getType() == Material.BED_BLOCK) {
            BlockFace direction = ((Bed) block.getState().getData()).getFacing();
            GlowBlock headBlock = block.getRelative(direction);
            headBlock.setType(Material.BED_BLOCK);
            GlowBlockState headBlockState = headBlock.getState();
            MaterialData data = headBlockState.getData();
            ((Bed) data).setHeadOfBed(true);
            ((Bed) data).setFacingDirection(direction);
            headBlockState.setData(data);
            headBlockState.update(true);
View Full Code Here

        return block.isLiquid();
    }

    @Override
    public final void rightClickBlock(GlowPlayer player, GlowBlock against, BlockFace face, ItemStack holding, Vector clickedLoc) {
        GlowBlock target = against.getRelative(face);

        // check whether the block clicked against should absorb the placement
        BlockType againstType = ItemTable.instance().getBlock(against.getTypeId());
        if (againstType.canAbsorb(against, face, holding)) {
            target = against;
        } else if (!target.isEmpty()) {
            // air can always be overridden
            BlockType targetType = ItemTable.instance().getBlock(target.getTypeId());
            if (!targetType.canOverride(target, face, holding)) {
                return;
            }
        }

        // call canBuild event
        boolean canBuild = canPlaceAt(target, face);
        BlockCanBuildEvent canBuildEvent = new BlockCanBuildEvent(target, getId(), canBuild);
        if (!EventFactory.callEvent(canBuildEvent).isBuildable()) {
            //revert(player, target);
            return;
        }

        // grab states and update block
        GlowBlockState oldState = target.getState(), newState = target.getState();
        placeBlock(player, newState, face, holding, clickedLoc);
        newState.update(true);

        // call blockPlace event
        BlockPlaceEvent event = new BlockPlaceEvent(target, oldState, against, holding, player, canBuild);
        EventFactory.callEvent(event);
        if (event.isCancelled() || !event.canBuild()) {
            oldState.update(true);
            return;
        }

        // play a sound effect
        // todo: vary sound effect based on block type
        target.getWorld().playSound(target.getLocation(), Sound.DIG_WOOD, 1, 1);

        // do any after-place actions
        afterPlace(player, target, holding);

        // deduct from stack if not in creative mode
View Full Code Here

public final class DiggingHandler implements MessageHandler<GlowSession, DiggingMessage> {
    @Override
    public void handle(GlowSession session, DiggingMessage message) {
        final GlowPlayer player = session.getPlayer();
        GlowWorld world = player.getWorld();
        GlowBlock block = world.getBlockAt(message.getX(), message.getY(), message.getZ());
        BlockFace face = BlockPlacementHandler.convertFace(message.getFace());
        ItemStack holding = player.getItemInHand();

        boolean blockBroken = false;
        boolean revert = false;
        if (message.getState() == DiggingMessage.START_DIGGING) {
            // call interact event
            Action action = Action.LEFT_CLICK_BLOCK;
            Block eventBlock = block;
            if (player.getLocation().distanceSquared(block.getLocation()) > 36 || block.getTypeId() == 0) {
                action = Action.LEFT_CLICK_AIR;
                eventBlock = null;
            }
            PlayerInteractEvent interactEvent = EventFactory.onPlayerInteract(player, action, eventBlock, face);

            // blocks don't get interacted with on left click, so ignore that
            // attempt to use item in hand, that is, dig up the block
            if (!BlockPlacementHandler.selectResult(interactEvent.useItemInHand(), true)) {
                // the event was cancelled, get out of here
                revert = true;
            } else {
                // emit damage event - cancel by default if holding a sword
                boolean instaBreak = player.getGameMode() == GameMode.CREATIVE;
                BlockDamageEvent damageEvent = new BlockDamageEvent(player, block, player.getItemInHand(), instaBreak);
                if (player.getGameMode() == GameMode.CREATIVE && holding != null && EnchantmentTarget.WEAPON.includes(holding.getType())) {
                    damageEvent.setCancelled(true);
                }
                EventFactory.callEvent(damageEvent);

                // follow orders
                if (damageEvent.isCancelled()) {
                    revert = true;
                } else {
                    // in creative, break even if denied in the event, or the block
                    // can never be broken (client does not send DONE_DIGGING).
                    blockBroken = damageEvent.getInstaBreak() || instaBreak;
                }
            }
        } else if (message.getState() == DiggingMessage.FINISH_DIGGING) {
            // shouldn't happen in creative mode

            // todo: verification against malicious clients
            // also, if the block dig was denied, this break might still happen
            // because a player's digging status isn't yet tracked. this is bad.
            blockBroken = true;
        } else {
            return;
        }

        if (blockBroken) {
            // fire the block break event
            BlockBreakEvent breakEvent = EventFactory.callEvent(new BlockBreakEvent(block, player));
            if (breakEvent.isCancelled()) {
                BlockPlacementHandler.revert(player, block);
                return;
            }

            BlockType blockType = ItemTable.instance().getBlock(block.getType());
            if (blockType != null) {
                blockType.blockDestroy(player, block, face);
            }

            // destroy the block
            if (!block.isEmpty() && !block.isLiquid() && player.getGameMode() != GameMode.CREATIVE) {
                for (ItemStack drop : block.getDrops(holding)) {
                    player.getInventory().addItem(drop);
                }
            }
            // STEP_SOUND actually is the block break particles
            world.playEffectExceptTo(block.getLocation(), Effect.STEP_SOUND, block.getTypeId(), 64, player);
            block.setType(Material.AIR);
        } else if (revert) {
            // replace the block that wasn't really dug
            BlockPlacementHandler.revert(player, block);
        }
    }
View Full Code Here

         * right-click air of an expected-place item immediately after is
         * not considered part of the same action.
         */

        Action action = Action.RIGHT_CLICK_BLOCK;
        GlowBlock clicked = player.getWorld().getBlockAt(message.getX(), message.getY(), message.getZ());

        /**
         * Check if the message is a -1. If we *just* got a message with the
         * values filled, discard it, otherwise perform right-click-air.
         */
        if (message.getDirection() == -1) {
            BlockPlacementMessage previous = session.getPreviousPlacement();
            if (previous == null || !previous.getHeldItem().equals(message.getHeldItem())) {
                // perform normal right-click-air actions
                action = Action.RIGHT_CLICK_AIR;
                clicked = null;
            } else {
                // terminate processing of this event
                session.setPreviousPlacement(null);
                return;
            }
        }

        // Set previous placement message
        session.setPreviousPlacement(message);

        // Get values from the message
        Vector clickedLoc = new Vector(message.getCursorX(), message.getCursorY(), message.getCursorZ());
        BlockFace face = convertFace(message.getDirection());
        ItemStack holding = player.getItemInHand();

        // check that held item matches
        if (!Objects.equals(holding, message.getHeldItem())) {
            // above handles cases where holding and/or message's item are null
            // todo: inform player their item is wrong
            return;
        }

        // check that a block-click wasn't against air
        if (clicked != null && clicked.getType() == Material.AIR) {
            // inform the player their perception of reality is wrong
            player.sendBlockChange(clicked.getLocation(), Material.AIR, (byte) 0);
            return;
        }

        // call interact event
        PlayerInteractEvent event = EventFactory.onPlayerInteract(player, action, clicked, face);
        //GlowServer.logger.info("Interact: " + action + " " + clicked + " " + face);

        // attempt to use interacted block
        // DEFAULT is treated as ALLOW, and sneaking is always considered
        boolean useInteractedBlock = event.useInteractedBlock() != Event.Result.DENY;
        if (useInteractedBlock && clicked != null && (!player.isSneaking() || holding == null)) {
            BlockType blockType = ItemTable.instance().getBlock(clicked.getType());
            useInteractedBlock = blockType.blockInteract(player, clicked, face, clickedLoc);
        } else {
            useInteractedBlock = false;
        }

        // attempt to use item in hand
        // follows ALLOW/DENY: default to if no block was interacted with
        if (selectResult(event.useItemInHand(), !useInteractedBlock) && holding != null) {
            // call out to the item type to determine the appropriate right-click action
            ItemType type = ItemTable.instance().getItem(holding.getType());
            if (clicked == null) {
                type.rightClickAir(player, holding);
            } else {
                type.rightClickBlock(player, clicked, face, holding, clickedLoc);
            }
        }

        // if anything was actually clicked, make sure the player's up to date
        // in case something is unimplemented or otherwise screwy on our side
        if (clicked != null) {
            revert(player, clicked);
            revert(player, clicked.getRelative(face));
        }

        // if there's been a change in the held item, make it valid again
        if (holding != null) {
            if (holding.getType().getMaxDurability() > 0 && holding.getDurability() > holding.getType().getMaxDurability()) {
View Full Code Here

TOP

Related Classes of net.glowstone.block.GlowBlock

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.