/**
* Uses a item on a position and face, placing it or activating the block
*
* @param Vector3 $vector
* @param Item $item
* @param int $face
* @param float $fx default 0.0
* @param float $fy default 0.0
* @param float $fz default 0.0
* @param Player $player default null
*
* @return boolean
*/
public function useItemOn(Vector3 $vector, Item &$item, $face, $fx = 0.0, $fy = 0.0, $fz = 0.0, Player $player = null)
{
$target = $this->getBlock($vector);
$block = $target->getSide($face);
if ($block->y > 127 or $block->y < 0) {
return false;
}
if ($target->getId() === Item::AIR) {
return false;
}
if ($player instanceof Player) {
$ev = new PlayerInteractEvent($player, $item, $target, $face, $target->getId() === 0 ? PlayerInteractEvent::RIGHT_CLICK_AIR : PlayerInteractEvent::RIGHT_CLICK_BLOCK);
if (!$player->isOp() and ($distance = $this->server->getSpawnRadius()) > -1) {
$t = new Vector2($target->x, $target->z);
$s = new Vector2($this->getSpawnLocation()->x, $this->getSpawnLocation()->z);
if (count($this->server->getOps()->getAll()) > 0 and $t->distance($s) <= $distance) {
//set it to cancelled so plugins can bypass this
$ev->setCancelled();
}
}
$this->server->getPluginManager()->callEvent($ev);
if (!$ev->isCancelled()) {
$target->onUpdate(self::BLOCK_UPDATE_TOUCH);
if ($target->canBeActivated() === true and $target->onActivate($item, $player) === true) {
return true;
}
if ($item->canBeActivated() and $item->onActivate($this, $player, $block, $target, $face, $fx, $fy, $fz)) {
if ($item->getCount() <= 0) {
$item = Item::get(Item::AIR, 0, 0);
return true;
}
}
} else {
return false;
}
} elseif ($target->canBeActivated() === true and $target->onActivate($item, $player) === true) {
return true;
}
if ($item->isPlaceable()) {
$hand = $item->getBlock();
$hand->position($block);
} elseif ($block->getId() === Item::FIRE) {
$this->setBlock($block, new Air(), true);
return false;
} else {
return false;
}
if (!($block->canBeReplaced() === true or $hand->getId() === Item::SLAB and $block->getId() === Item::SLAB)) {
return false;
}
if ($target->canBeReplaced() === true) {
$block = $target;
$hand->position($block);
//$face = -1;
}
if ($hand->isSolid() === true and $hand->getBoundingBox() !== null) {
$entities = $this->getCollidingEntities($hand->getBoundingBox());
$realCount = 0;
foreach ($entities as $e) {
if ($e instanceof Arrow or $e instanceof DroppedItem) {
continue;
}
++$realCount;
}
if ($realCount > 0) {
return false;
//Entity in block
}
}
if ($player instanceof Player) {
$ev = new BlockPlaceEvent($player, $hand, $block, $target, $item);
if (!$player->isOp() and ($distance = $this->server->getSpawnRadius()) > -1) {
$t = new Vector2($target->x, $target->z);
$s = new Vector2($this->getSpawnLocation()->x, $this->getSpawnLocation()->z);
if (count($this->server->getOps()->getAll()) > 0 and $t->distance($s) <= $distance) {
//set it to cancelled so plugins can bypass this
$ev->setCancelled();
}
}
$this->server->getPluginManager()->callEvent($ev);
if ($ev->isCancelled()) {
return false;
}
}
if ($hand->place($item, $block, $target, $face, $fx, $fy, $fz, $player) === false) {
return false;
}
if ($hand->getId() === Item::SIGN_POST or $hand->getId() === Item::WALL_SIGN) {
$tile = Tile::createTile("Sign", $this->getChunk($block->x >> 4, $block->z >> 4), new Compound("", ["id" => new String("id", Tile::SIGN), "x" => new Int("x", $block->x), "y" => new Int("y", $block->y), "z" => new Int("z", $block->z), "Text1" => new String("Text1", ""), "Text2" => new String("Text2", ""), "Text3" => new String("Text3", ""), "Text4" => new String("Text4", "")]));
if ($player instanceof Player) {
$tile->namedtag->Creator = new String("Creator", $player->getName());
}
}
$item->setCount($item->getCount() - 1);
if ($item->getCount() <= 0) {
$item = Item::get(Item::AIR, 0, 0);
}
return true;
}