Refactor block internal API

This commit is contained in:
Tatsuyuki Ishi 2016-04-25 23:01:00 +09:00
commit a0e663fa12
4 changed files with 44 additions and 45 deletions

View file

@ -749,15 +749,15 @@ class Block extends Position implements Metadatable{
return 0;
}
public function isBlockTopFacingSurfaceSolid(Block $block){
if($block->isSolid()){
public function isTopFacingSurfaceSolid(){
if($this->isSolid()){
return true;
}else{
if($block instanceof Stair and ($block->getDamage() &4) == 4){
if($this instanceof Stair and ($this->getDamage() &4) == 4){
return true;
}elseif($block instanceof Slab and ($block->getDamage() & 8) == 8){
}elseif($this instanceof Slab and ($this->getDamage() & 8) == 8){
return true;
}elseif($block instanceof SnowLayer and ($block->getDamage() & 7) == 7){
}elseif($this instanceof SnowLayer and ($this->getDamage() & 7) == 7){
return true;
}
}
@ -773,40 +773,6 @@ class Block extends Position implements Metadatable{
return false;
}
public function tryToCatchBlockOnFire(Block $block, int $bound, int $damage){
$burnAbility = $block->getBurnAbility();
if(mt_rand(0, $bound) < $burnAbility){
if(mt_rand(0, $damage + 10) < 5){
$meta = min(15, $damage + mt_rand(0, 5) / 4);
$this->getLevel()->setBlock($this, $fire = new Fire($meta), true);
$this->getLevel()->scheduleUpdate($this, $fire->getTickRate());
}else{
$this->getLevel()->getServer()->getPluginManager()->callEvent($ev = new BlockBurnEvent($block));
if(!$ev->isCancelled()){
$this->getLevel()->setBlock($this, new Air(), true);
}
}
if($block instanceof TNT){
$block->prime();
}
}
}
public function getChanceOfNeighborsEncouragingFire(Block $block){
if($block->getId() !== self::AIR){
return 0;
}else{
$chance = 0;
for($i = 0; $i < 5; $i++){
$chance = max($chance, $block->getSide($i)->getBurnChance());
}
return $chance;
}
}
/**
* @return int
*/

View file

@ -82,7 +82,7 @@ class Fire extends Flowable{
public function onUpdate($type){
if($type == Level::BLOCK_UPDATE_NORMAL or $type = Level::BLOCK_UPDATE_RANDOM){
if(!$this->isBlockTopFacingSurfaceSolid($this->getSide(Vector3::SIDE_DOWN)) and !$this->canNeighborBurn()){
if(!$this->getSide(Vector3::SIDE_DOWN)->isTopFacingSurfaceSolid() and !$this->canNeighborBurn()){
$this->getLevel()->setBlock($this, new Air(), true);
return Level::BLOCK_UPDATE_NORMAL;
}elseif($type == Level::BLOCK_UPDATE_SCHEDULED and $this->getLevel()->getServer()->fireSpread){
@ -90,7 +90,7 @@ class Fire extends Flowable{
//TODO: END
if(!$this->isBlockTopFacingSurfaceSolid($this->getSide(Vector3::SIDE_DOWN)) and !$this->canNeighborBurn()){
if(!$this->getSide(Vector3::SIDE_DOWN)->isBlockTopFacingSurfaceSolid() and !$this->canNeighborBurn()){
$this->getLevel()->setBlock($this, new Air(), true);
}
@ -114,7 +114,7 @@ class Fire extends Flowable{
$this->getLevel()->scheduleUpdate($this, $this->getTickRate() + mt_rand(0, 10));
if(!$forever and !$this->canNeighborBurn()){
if(!$this->isBlockTopFacingSurfaceSolid($this->getSide(Vector3::SIDE_DOWN)) or $meta > 3){
if(!$$this->getSide(Vector3::SIDE_DOWN)->isTopFacingSurfaceSolid() or $meta > 3){
$this->getLevel()->setBlock($this, new Air(), true);
}
}else if(!$forever && !($this->getSide(Vector3::SIDE_DOWN)->getBurnAbility() > 0) && $meta == 15 && mt_rand(0, 4) == 0){
@ -191,4 +191,37 @@ class Fire extends Flowable{
return false;
}*/
}
private function tryToCatchBlockOnFire(Block $block, int $bound, int $damage){
$burnAbility = $block->getBurnAbility();
if(mt_rand(0, $bound) < $burnAbility){
if(mt_rand(0, $damage + 10) < 5){
$meta = max(15, $damage + mt_rand(0, 4) / 4);
$this->getLevel()->getServer()->getPluginManager()->callEvent($ev = new BlockBurnEvent($block));
if(!$ev->isCancelled()){
$this->getLevel()->setBlock($block, $fire = new Fire($meta), true);
$this->getLevel()->scheduleUpdate($block, $fire->getTickRate());
}
}else{
$this->getLevel()->setBlock($this, new Air(), true);
}
if($block instanceof TNT){
$block->prime();
}
}
}
private function getChanceOfNeighborsEncouragingFire(Block $block){
if($block->getId() !== self::AIR){
return 0;
}else{
$chance = 0;
for($i = 0; $i < 5; $i++){
$chance = max($chance, $block->getSide($i)->getBurnChance());
}
return $chance;
}
}
}

View file

@ -137,7 +137,7 @@ class FlintSteel extends Tool{
/** @var Fire $block */
$block = $level->getBlock($block);
if($block->isBlockTopFacingSurfaceSolid($block->getSide(Vector3::SIDE_DOWN)) or $block->canNeighborBurn()){
if($block->getSide(Vector3::SIDE_DOWN)->isTopFacingSurfaceSolid() or $block->canNeighborBurn()){
$level->scheduleUpdate($block, $block->getTickRate() + mt_rand(0, 10));
// return true;
}

View file

@ -155,7 +155,7 @@ class MobSpawner extends Spawnable{
$pos = $this->add(mt_rand() / mt_getrandmax() * $this->getSpawnRange(), mt_rand(-1, 1), mt_rand() / mt_getrandmax() * $this->getSpawnRange());
$target = $this->getLevel()->getBlock($pos);
$ground = $target->getSide(Vector3::SIDE_DOWN);
if($target->getId() == Item::AIR && $ground->isSolid()){
if($target->getId() == Item::AIR && $ground->isTopFacingSurfaceSolid()){
$success++;
$this->getLevel()->getServer()->getPluginManager()->callEvent($ev = new EntityGenerateEvent($pos, $this->getEntityId(), EntityGenerateEvent::CAUSE_MOB_SPAWNER));
if(!$ev->isCancelled()){