* 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()) {