$class){ if($class !== null){ /** @var Block $block */ $block = new $class(); for($data = 0; $data < 16; ++$data){ self::$fullList[($id << 4) | $data] = new $class($data); } }else{ $block = new UnknownBlock($id); for($data = 0; $data < 16; ++$data){ self::$fullList[($id << 4) | $data] = new UnknownBlock($id, $data); } } self::$solid[$id] = $block->isSolid(); self::$transparent[$id] = $block->isTransparent(); self::$hardness[$id] = $block->getHardness(); self::$light[$id] = $block->getLightLevel(); self::$lightFilter[$id] = $block->getLightFilter() + 1; self::$diffusesSkyLight[$id] = $block->diffusesSkyLight(); } } } /** * @param int $id * @param int $meta * @param Position $pos * * @return Block */ public static function get($id, $meta = 0, Position $pos = null){ try{ $block = self::$list[$id]; if($block !== null){ $block = new $block($meta); }else{ $block = new UnknownBlock($id, $meta); } }catch(\RuntimeException $e){ $block = new UnknownBlock($id, $meta); } if($pos !== null){ $block->x = $pos->x; $block->y = $pos->y; $block->z = $pos->z; $block->level = $pos->level; } return $block; } /** * @param int $id * @param int $meta */ public function __construct($id, $meta = 0){ $this->id = (int) $id; $this->meta = (int) $meta; } /** * Places the Block, using block space and block target, and side. Returns if the block has been placed. * * @param Item $item * @param Block $block * @param Block $target * @param int $face * @param float $fx * @param float $fy * @param float $fz * @param Player $player = null * * @return bool */ public function place(Item $item, Block $block, Block $target, $face, $fx, $fy, $fz, Player $player = null){ return $this->getLevel()->setBlock($this, $this, true, true); } /** * Returns if the item can be broken with an specific Item * * @param Item $item * * @return bool */ public function isBreakable(Item $item){ return true; } /** * Do the actions needed so the block is broken with the Item * * @param Item $item * * @return mixed */ public function onBreak(Item $item){ return $this->getLevel()->setBlock($this, new Air(), true, true); } /** * Fires a block update on the Block * * @param int $type * * @return int|bool */ public function onUpdate($type){ return false; } /** * Do actions when activated by Item. Returns if it has done anything * * @param Item $item * @param Player $player * * @return bool */ public function onActivate(Item $item, Player $player = null){ return false; } /** * @return float */ public function getHardness(){ return 10; } /** * @return float */ public function getResistance(){ return $this->getHardness() * 5; } /** * @return int */ public function getToolType(){ return Tool::TYPE_NONE; } /** * @return float */ public function getFrictionFactor(){ return 0.6; } /** * @return int 0-15 */ public function getLightLevel(){ return 0; } /** * Returns the amount of light this block will filter out when light passes through this block. * This value is used in light spread calculation. * * @return int 0-15 */ public function getLightFilter() : int{ return 15; } /** * Returns whether this block will diffuse sky light passing through it vertically. * Diffusion means that full-strength sky light passing through this block will not be reduced, but will start being filtered below the block. * Examples of this behaviour include leaves and cobwebs. * * Light-diffusing blocks are included by the heightmap. * * @return bool */ public function diffusesSkyLight() : bool{ return false; } /** * AKA: Block->isPlaceable * * @return bool */ public function canBePlaced(){ return true; } /** * @return bool */ public function canBeReplaced(){ return false; } /** * @return bool */ public function isTransparent(){ return false; } public function isSolid(){ return true; } /** * AKA: Block->isFlowable * * @return bool */ public function canBeFlowedInto(){ return false; } /** * AKA: Block->isActivable * * @return bool */ public function canBeActivated(){ return false; } public function hasEntityCollision(){ return false; } public function canPassThrough(){ return false; } /** * Returns whether entities can climb up this block. * @return bool */ public function canClimb() : bool{ return false; } /** * @return string */ public function getName(){ return "Unknown"; } /** * @return int */ final public function getId(){ return $this->id; } public function addVelocityToEntity(Entity $entity, Vector3 $vector){ } /** * @return int */ final public function getDamage(){ return $this->meta; } /** * @param int $meta */ final public function setDamage($meta){ $this->meta = $meta & 0x0f; } /** * Sets the block position to a new Position object * * @param Position $v */ final public function position(Position $v){ $this->x = (int) $v->x; $this->y = (int) $v->y; $this->z = (int) $v->z; $this->level = $v->level; $this->boundingBox = null; } /** * Returns an array of Item objects to be dropped * * @param Item $item * * @return array */ public function getDrops(Item $item){ if(!isset(self::$list[$this->getId()])){ //Unknown blocks return []; }else{ return [ [$this->getId(), $this->getDamage(), 1], ]; } } /** * Returns the seconds that this block takes to be broken using an specific Item * * @param Item $item * * @return float */ public function getBreakTime(Item $item){ $base = $this->getHardness() * 1.5; if($this->canBeBrokenWith($item)){ if($this->getToolType() === Tool::TYPE_SHEARS and $item->isShears()){ $base /= 15; }elseif( ($this->getToolType() === Tool::TYPE_PICKAXE and ($tier = $item->isPickaxe()) !== false) or ($this->getToolType() === Tool::TYPE_AXE and ($tier = $item->isAxe()) !== false) or ($this->getToolType() === Tool::TYPE_SHOVEL and ($tier = $item->isShovel()) !== false) ){ switch($tier){ case Tool::TIER_WOODEN: $base /= 2; break; case Tool::TIER_STONE: $base /= 4; break; case Tool::TIER_IRON: $base /= 6; break; case Tool::TIER_DIAMOND: $base /= 8; break; case Tool::TIER_GOLD: $base /= 12; break; } } }else{ $base *= 3.33; } if($item->isSword()){ $base *= 0.5; } return $base; } public function canBeBrokenWith(Item $item){ return $this->getHardness() !== -1; } /** * Returns the Block on the side $side, works like Vector3::side() * * @param int $side * @param int $step * * @return Block */ public function getSide($side, $step = 1){ if($this->isValid()){ return $this->getLevel()->getBlock(Vector3::getSide($side, $step)); } return Block::get(Item::AIR, 0, Position::fromObject(Vector3::getSide($side, $step))); } /** * @return string */ public function __toString(){ return "Block[" . $this->getName() . "] (" . $this->getId() . ":" . $this->getDamage() . ")"; } /** * Checks for collision against an AxisAlignedBB * * @param AxisAlignedBB $bb * * @return bool */ public function collidesWithBB(AxisAlignedBB $bb){ $bb2 = $this->getBoundingBox(); return $bb2 !== null and $bb->intersectsWith($bb2); } /** * @param Entity $entity */ public function onEntityCollide(Entity $entity){ } /** * @return AxisAlignedBB|null */ public function getBoundingBox(){ if($this->boundingBox === null){ $this->boundingBox = $this->recalculateBoundingBox(); } return $this->boundingBox; } /** * @return AxisAlignedBB|null */ protected function recalculateBoundingBox(){ return new AxisAlignedBB( $this->x, $this->y, $this->z, $this->x + 1, $this->y + 1, $this->z + 1 ); } /** * @param Vector3 $pos1 * @param Vector3 $pos2 * * @return MovingObjectPosition|null */ public function calculateIntercept(Vector3 $pos1, Vector3 $pos2){ $bb = $this->getBoundingBox(); if($bb === null){ return null; } $v1 = $pos1->getIntermediateWithXValue($pos2, $bb->minX); $v2 = $pos1->getIntermediateWithXValue($pos2, $bb->maxX); $v3 = $pos1->getIntermediateWithYValue($pos2, $bb->minY); $v4 = $pos1->getIntermediateWithYValue($pos2, $bb->maxY); $v5 = $pos1->getIntermediateWithZValue($pos2, $bb->minZ); $v6 = $pos1->getIntermediateWithZValue($pos2, $bb->maxZ); if($v1 !== null and !$bb->isVectorInYZ($v1)){ $v1 = null; } if($v2 !== null and !$bb->isVectorInYZ($v2)){ $v2 = null; } if($v3 !== null and !$bb->isVectorInXZ($v3)){ $v3 = null; } if($v4 !== null and !$bb->isVectorInXZ($v4)){ $v4 = null; } if($v5 !== null and !$bb->isVectorInXY($v5)){ $v5 = null; } if($v6 !== null and !$bb->isVectorInXY($v6)){ $v6 = null; } $vector = $v1; if($v2 !== null and ($vector === null or $pos1->distanceSquared($v2) < $pos1->distanceSquared($vector))){ $vector = $v2; } if($v3 !== null and ($vector === null or $pos1->distanceSquared($v3) < $pos1->distanceSquared($vector))){ $vector = $v3; } if($v4 !== null and ($vector === null or $pos1->distanceSquared($v4) < $pos1->distanceSquared($vector))){ $vector = $v4; } if($v5 !== null and ($vector === null or $pos1->distanceSquared($v5) < $pos1->distanceSquared($vector))){ $vector = $v5; } if($v6 !== null and ($vector === null or $pos1->distanceSquared($v6) < $pos1->distanceSquared($vector))){ $vector = $v6; } if($vector === null){ return null; } $f = -1; if($vector === $v1){ $f = 4; }elseif($vector === $v2){ $f = 5; }elseif($vector === $v3){ $f = 0; }elseif($vector === $v4){ $f = 1; }elseif($vector === $v5){ $f = 2; }elseif($vector === $v6){ $f = 3; } return MovingObjectPosition::fromBlock($this->x, $this->y, $this->z, $f, $vector->add($this->x, $this->y, $this->z)); } public function setMetadata($metadataKey, MetadataValue $metadataValue){ if($this->getLevel() instanceof Level){ $this->getLevel()->getBlockMetadata()->setMetadata($this, $metadataKey, $metadataValue); } } public function getMetadata($metadataKey){ if($this->getLevel() instanceof Level){ return $this->getLevel()->getBlockMetadata()->getMetadata($this, $metadataKey); } return null; } public function hasMetadata($metadataKey){ if($this->getLevel() instanceof Level){ $this->getLevel()->getBlockMetadata()->hasMetadata($this, $metadataKey); } } public function removeMetadata($metadataKey, Plugin $plugin){ if($this->getLevel() instanceof Level){ $this->getLevel()->getBlockMetadata()->removeMetadata($this, $metadataKey, $plugin); } } }