Search in sources :

Example 1 with BlockPlacementMessage

use of net.glowstone.net.message.play.player.BlockPlacementMessage in project Glowstone by GlowstoneMC.

the class BlockPlacementHandler method handle.

@Override
public void handle(GlowSession session, BlockPlacementMessage message) {
    //TODO: Hand handling instead of .getHeldItem()
    GlowPlayer player = session.getPlayer();
    if (player == null)
        return;
    //GlowServer.logger.info(session + ": " + message);
    /*
         * The client sends this packet for the following cases:
         * Right click air:
         * - Send direction=-1 packet for any non-null item
         * Right click block:
         * - Send packet with all values filled
         * - If client DOES NOT expect a block placement to result:
         *   - Send direction=-1 packet (unless item is null)
         *
         * Client will expect a block placement to result from blocks and from
         * certain items (e.g. sugarcane, sign). We *could* opt to trust the
         * client on this, but the server's view of events (particularly under
         * the Bukkit API, or custom ItemTypes) may differ from the client's.
         *
         * In order to avoid firing two events for one interact, the two
         * packet case must be handled here. Care must also be taken that a
         * 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();
        //   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();
    boolean rightClickedAir = false;
    // check that a block-click wasn't against air
    if (clicked == null || clicked.getType() == Material.AIR) {
        action = Action.RIGHT_CLICK_AIR;
        // inform the player their perception of reality is wrong
        if (holding.getType().isBlock()) {
            player.sendBlockChange(clicked.getLocation(), Material.AIR, (byte) 0);
            return;
        } else {
            rightClickedAir = true;
        }
    }
    // call interact event
    PlayerInteractEvent event = EventFactory.onPlayerInteract(player, action, rightClickedAir ? null : 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() != Result.DENY;
    if (useInteractedBlock && !rightClickedAir && (!player.isSneaking() || InventoryUtil.isEmpty(holding))) {
        BlockType blockType = ItemTable.instance().getBlock(clicked.getType());
        if (blockType != null) {
            useInteractedBlock = blockType.blockInteract(player, clicked, face, clickedLoc);
        } else {
            GlowServer.logger.info("Unknown clicked block, " + clicked.getType());
        }
    } else {
        useInteractedBlock = false;
    }
    // follows ALLOW/DENY: default to if no block was interacted with
    if (selectResult(event.useItemInHand(), !useInteractedBlock) && holding != null) {
        ItemType type = ItemTable.instance().getItem(holding.getType());
        if (!rightClickedAir && holding.getType() != Material.AIR && !type.canOnlyUseSelf()) {
            type.rightClickBlock(player, clicked, face, holding, clickedLoc);
        }
    }
    // in case something is unimplemented or otherwise screwy on our side
    if (!rightClickedAir) {
        revert(player, clicked);
        revert(player, clicked.getRelative(face));
    }
    // if there's been a change in the held item, make it valid again
    if (!InventoryUtil.isEmpty(holding)) {
        if (holding.getType().getMaxDurability() > 0 && holding.getDurability() > holding.getType().getMaxDurability()) {
            holding.setAmount(holding.getAmount() - 1);
            holding.setDurability((short) 0);
        }
        if (holding.getAmount() <= 0) {
            holding = null;
        }
    }
    player.setItemInHand(holding);
}
Also used : BlockPlacementMessage(net.glowstone.net.message.play.player.BlockPlacementMessage) Action(org.bukkit.event.block.Action) GlowBlock(net.glowstone.block.GlowBlock) BlockType(net.glowstone.block.blocktype.BlockType) GlowPlayer(net.glowstone.entity.GlowPlayer) BlockFace(org.bukkit.block.BlockFace) PlayerInteractEvent(org.bukkit.event.player.PlayerInteractEvent) ItemType(net.glowstone.block.itemtype.ItemType) ItemStack(org.bukkit.inventory.ItemStack) Vector(org.bukkit.util.Vector)

Example 2 with BlockPlacementMessage

use of net.glowstone.net.message.play.player.BlockPlacementMessage in project Glowstone by GlowstoneMC.

the class BlockPlacementCodec method decode.

@Override
public BlockPlacementMessage decode(ByteBuf buf) throws IOException {
    BlockVector pos = GlowBufUtils.readBlockPosition(buf);
    int direction = buf.readByte();
    int hand = ByteBufUtils.readVarInt(buf);
    float cursorX = buf.readFloat();
    float cursorY = buf.readFloat();
    float cursorZ = buf.readFloat();
    return new BlockPlacementMessage(pos.getBlockX(), pos.getBlockY(), pos.getBlockZ(), direction, hand, cursorX, cursorY, cursorZ);
}
Also used : BlockPlacementMessage(net.glowstone.net.message.play.player.BlockPlacementMessage) BlockVector(org.bukkit.util.BlockVector)

Example 3 with BlockPlacementMessage

use of net.glowstone.net.message.play.player.BlockPlacementMessage in project Dragonet-Legacy by DragonetMC.

the class UseItemPacketTranslator method handleSpecific.

@Override
public Message[] handleSpecific(UseItemPacket packet) {
    UseItemPacket pkUseItem = (UseItemPacket) packet;
    System.out.println("FACE=" + (pkUseItem.face & 0xFF) + ", ITEM=" + packet.item);
    if (pkUseItem.face == 0xFF) {
        // Air touch
        // Left click air
        PlayerSwingArmMessage msg = new PlayerSwingArmMessage();
        return new Message[] { msg };
    }
    if (!(pkUseItem.face >= 0 && pkUseItem.face < 6)) {
        return null;
    }
    // Check the slot
    ItemStack test_holding = this.getSession().getPlayer().getInventory().getItemInHand();
    if (packet.item.id != this.getTranslator().getItemTranslator().translateToPE(test_holding.getTypeId()) || packet.item.meta != test_holding.getDurability()) {
        // Not same, resend slot
        PlayerEquipmentPacket pkRet = new PlayerEquipmentPacket();
        pkRet.eid = this.getSession().getPlayer().getEntityId();
        pkRet.item = new PEInventorySlot((short) 0, (byte) 0, (short) 0);
        pkRet.item.id = (short) (this.getTranslator().getItemTranslator().translateToPE(test_holding.getTypeId()) & 0xFFFF);
        pkRet.item.count = (byte) (test_holding.getAmount() & 0xFF);
        pkRet.item.meta = test_holding.getDurability();
        pkRet.selectedSlot = this.getSession().getPlayer().getInventory().getHeldItemSlot();
        // Resend block
        UpdateBlockPacket pkUpdateBlock = new UpdateBlockPacket();
        UpdateBlockPacket.UpdateBlockRecord blockRec = new UpdateBlockPacket.UpdateBlockRecord();
        blockRec.x = packet.x;
        blockRec.z = packet.z;
        blockRec.y = (byte) (packet.y & 0xFF);
        blockRec.block = (byte) (this.getSession().getPlayer().getWorld().getBlockAt(pkUseItem.x, pkUseItem.y, pkUseItem.z).getTypeId() & 0xFF);
        blockRec.meta = (byte) (this.getSession().getPlayer().getWorld().getBlockAt(pkUseItem.x, pkUseItem.y, pkUseItem.z).getData() & 0xFF);
        pkUpdateBlock.records = new UpdateBlockPacket.UpdateBlockRecord[] { blockRec };
        getSession().send(pkRet);
        getSession().send(pkUpdateBlock);
        return null;
    }
    // Copied from Glowstone class BlockPlacementHandler
    new BlockPlacementHandler().handle(getSession(), new BlockPlacementMessage(packet.x, packet.y, packet.z, packet.face, test_holding, 0, 0, 0));
    return null;
}
Also used : BlockPlacementMessage(net.glowstone.net.message.play.player.BlockPlacementMessage) UseItemPacket(org.dragonet.net.packet.minecraft.UseItemPacket) Message(com.flowpowered.networking.Message) PlayerSwingArmMessage(net.glowstone.net.message.play.player.PlayerSwingArmMessage) BlockPlacementMessage(net.glowstone.net.message.play.player.BlockPlacementMessage) ItemStack(org.bukkit.inventory.ItemStack) PlayerSwingArmMessage(net.glowstone.net.message.play.player.PlayerSwingArmMessage) PlayerEquipmentPacket(org.dragonet.net.packet.minecraft.PlayerEquipmentPacket) BlockPlacementHandler(net.glowstone.net.handler.play.player.BlockPlacementHandler) PEInventorySlot(org.dragonet.inventory.PEInventorySlot) UpdateBlockPacket(org.dragonet.net.packet.minecraft.UpdateBlockPacket)

Aggregations

BlockPlacementMessage (net.glowstone.net.message.play.player.BlockPlacementMessage)3 ItemStack (org.bukkit.inventory.ItemStack)2 Message (com.flowpowered.networking.Message)1 GlowBlock (net.glowstone.block.GlowBlock)1 BlockType (net.glowstone.block.blocktype.BlockType)1 ItemType (net.glowstone.block.itemtype.ItemType)1 GlowPlayer (net.glowstone.entity.GlowPlayer)1 BlockPlacementHandler (net.glowstone.net.handler.play.player.BlockPlacementHandler)1 PlayerSwingArmMessage (net.glowstone.net.message.play.player.PlayerSwingArmMessage)1 BlockFace (org.bukkit.block.BlockFace)1 Action (org.bukkit.event.block.Action)1 PlayerInteractEvent (org.bukkit.event.player.PlayerInteractEvent)1 BlockVector (org.bukkit.util.BlockVector)1 Vector (org.bukkit.util.Vector)1 PEInventorySlot (org.dragonet.inventory.PEInventorySlot)1 PlayerEquipmentPacket (org.dragonet.net.packet.minecraft.PlayerEquipmentPacket)1 UpdateBlockPacket (org.dragonet.net.packet.minecraft.UpdateBlockPacket)1 UseItemPacket (org.dragonet.net.packet.minecraft.UseItemPacket)1