Search in sources :

Example 1 with MessagePlayInPlayerDigging

use of org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerDigging in project LanternServer by LanternPowered.

the class CodecPlayInPlayerDigging method decode.

@Override
public Message decode(CodecContext context, ByteBuffer buf) throws CodecException {
    int action = buf.readByte();
    Vector3i position = buf.read(Types.VECTOR_3_I);
    int face = buf.readByte();
    switch(action) {
        case 0:
        case 1:
        case 2:
            return new MessagePlayInPlayerDigging(MessagePlayInPlayerDigging.Action.values()[action], position, fromFace(face));
        case 3:
        case 4:
            return new MessagePlayInDropHeldItem(action == 3);
        case 5:
            return new MessagePlayInOutFinishUsingItem();
        case 6:
            return new MessagePlayInSwapHandItems();
        default:
            throw new DecoderException("Unknown player digging message action: " + action);
    }
}
Also used : DecoderException(io.netty.handler.codec.DecoderException) MessagePlayInOutFinishUsingItem(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutFinishUsingItem) Vector3i(com.flowpowered.math.vector.Vector3i) MessagePlayInPlayerDigging(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerDigging) MessagePlayInSwapHandItems(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInSwapHandItems) MessagePlayInDropHeldItem(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInDropHeldItem)

Example 2 with MessagePlayInPlayerDigging

use of org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerDigging in project LanternServer by LanternPowered.

the class PlayerInteractionHandler method handleDigging.

/**
 * Handles the {@link MessagePlayInPlayerDigging}.
 *
 * @param message The message
 */
public void handleDigging(MessagePlayInPlayerDigging message) {
    final MessagePlayInPlayerDigging.Action action = message.getAction();
    final Vector3i blockPos = message.getPosition();
    if (action == MessagePlayInPlayerDigging.Action.START) {
        // Check if the block is within the players reach
        if (this.player.getPosition().distanceSquared(blockPos.toDouble().add(0.5, 2.0, 0.5)) > 6.0 * 6.0) {
            return;
        }
        if (this.diggingBlock != null) {
            Lantern.getLogger().warn("{} started breaking a block without finishing the last one.", this.player.getName());
        }
        final BlockType blockType = this.player.getWorld().getBlockType(blockPos);
        if (blockType == BlockTypes.AIR) {
            return;
        }
        this.diggingBlock = blockPos;
        this.diggingBlockType = blockType;
        this.diggingDuration = getDiggingDuration(blockPos);
        // The client won't send a finish message
        if (this.diggingDuration == 0) {
            handleBrokenBlock();
        } else {
            this.diggingEndTime = this.diggingDuration == -1 ? -1 : System.nanoTime() + this.diggingDuration;
        }
    } else if (action == MessagePlayInPlayerDigging.Action.CANCEL) {
        if (this.diggingBlock == null || !this.diggingBlock.equals(blockPos)) {
            return;
        }
        if (this.lastBreakState != -1) {
            sendBreakUpdate(-1);
        }
        this.diggingBlock = null;
        this.diggingBlockType = null;
    } else {
        if (this.diggingBlock == null) {
            return;
        }
        final BlockType blockType = this.player.getWorld().getBlockType(blockPos);
        if (blockType != this.diggingBlockType) {
            return;
        }
        if (this.diggingEndTime == -1) {
            Lantern.getLogger().warn("{} attempted to break a unbreakable block.", this.player.getName());
        } else {
            final long deltaTime = System.nanoTime() - this.diggingEndTime;
            if (deltaTime < 0) {
                Lantern.getLogger().warn("{} finished breaking a block too early, {}ms too fast.", this.player.getName(), -(deltaTime / 1000));
            }
            handleBrokenBlock();
        }
    }
}
Also used : LanternBlockType(org.lanternpowered.server.block.LanternBlockType) BlockType(org.spongepowered.api.block.BlockType) Vector3i(com.flowpowered.math.vector.Vector3i) MessagePlayInPlayerDigging(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerDigging)

Aggregations

Vector3i (com.flowpowered.math.vector.Vector3i)2 MessagePlayInPlayerDigging (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerDigging)2 DecoderException (io.netty.handler.codec.DecoderException)1 LanternBlockType (org.lanternpowered.server.block.LanternBlockType)1 MessagePlayInDropHeldItem (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInDropHeldItem)1 MessagePlayInOutFinishUsingItem (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInOutFinishUsingItem)1 MessagePlayInSwapHandItems (org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInSwapHandItems)1 BlockType (org.spongepowered.api.block.BlockType)1