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);
}
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);
}
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;
}
Aggregations