diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index 30adaf3d..d8fb334c 100755 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -457,7 +457,8 @@ class BlockAPI{ return $this->cancelAction($block, $player); } } - $this->blockUpdate($target, BLOCK_UPDATE_TOUCH); + + StaticBlock::getBlock($target->getID())::interact($target->level, $target->x, $target->y, $target->z, $player); if($target->isActivable === true){ if($this->server->api->dhandle("player.block.activate", ["player" => $player, "block" => $block, "target" => $target, "item" => $item]) !== false and $target->onActivate($item, $player) === true){ @@ -527,24 +528,29 @@ class BlockAPI{ } public function blockUpdate(Position $pos, $type = BLOCK_UPDATE_NORMAL){ - if(!($pos instanceof Block)){ - $block = $pos->level->getBlock($pos); - }else{ - $pos = new Position($pos->x, $pos->y, $pos->z, $pos->level); - $block = $pos->level->getBlock($pos); - } + $block = StaticBlock::getBlock($pos->level->level->getBlockID($pos->x, $pos->y, $pos->z)); if($block === false){ return false; } - - $level = $block->onUpdate($type); - if($level === BLOCK_UPDATE_NORMAL){ - $this->blockUpdateAround($block, $level); - } + $level = $block::onUpdate($pos->level, $pos->x, $pos->y, $pos->z, $type); return $level; } + + + public function blockUpdateAround(Position $pos, $type = BLOCK_UPDATE_NORMAL, $delay = false){ + + if($type == BLOCK_UPDATE_NORMAL){ + try{ + throw new Exception("Deprecated: tried updating $pos using BLOCK_UPDATE_NORMAL."); + }catch(Exception $e){ + ConsoleAPI::error($e->getMessage()); + ConsoleAPI::error($e->getTraceAsString()); + } + return; + } + if($delay !== false){ $this->scheduleBlockUpdate($pos->getSide(0), $delay, $type); $this->scheduleBlockUpdate($pos->getSide(1), $delay, $type); @@ -561,6 +567,21 @@ class BlockAPI{ $this->blockUpdate($pos->getSide(5), $type); } } + public function scheduleBlockUpdateXYZ(Level $level, $x, $y, $z, $type = BLOCK_UPDATE_SCHEDULED, $delay = false){ + $type = (int) $type; + if($delay < 0){ + return false; + } + + $index = $x . "." . $y . "." . $z . "." . $level->getName() . "." . $type; + $delay = microtime(true) + $delay * 0.05; + if(!isset($this->scheduledUpdates[$index])){ + $this->scheduledUpdates[$index] = new Position($x, $y, $z, $level); //TODO dont create a position + $this->server->query("INSERT INTO blockUpdates (x, y, z, level, type, delay) VALUES ($x, $y, $z, '{$level->getName()}', $type, $delay);"); + return true; + } + return false; + } public function scheduleBlockUpdate(Position $pos, $delay, $type = BLOCK_UPDATE_SCHEDULED){ $type = (int) $type; diff --git a/src/API/TileAPI.php b/src/API/TileAPI.php index 000b70a5..6770c656 100644 --- a/src/API/TileAPI.php +++ b/src/API/TileAPI.php @@ -10,7 +10,13 @@ class TileAPI{ $this->tiles = []; $this->server = ServerAPI::request(); } - + public function getXYZ(Level $level, $x, $y, $z){ + $tile = $this->server->query("SELECT * FROM tiles WHERE level = '{$level->getName()}' AND x = $z AND y = $y AND z = $z;", true); + if($tile !== false and $tile !== true and ($tile = $this->getByID($tile["ID"])) !== false){ + return $tile; + } + return false; + } public function get(Position $pos){ $tile = $this->server->query("SELECT * FROM tiles WHERE level = '" . $pos->level->getName() . "' AND x = {$pos->x} AND y = {$pos->y} AND z = {$pos->z};", true); if($tile !== false and $tile !== true and ($tile = $this->getByID($tile["ID"])) !== false){ diff --git a/src/material/Block.php b/src/material/Block.php index ed847ec0..8b49559c 100755 --- a/src/material/Block.php +++ b/src/material/Block.php @@ -154,6 +154,9 @@ abstract class Block extends Position{ public $y = 0; public $z = 0; public $slipperiness; + public static function interact(Level $level, $x, $y, $z, Player $player){} + + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){} public static function getAABB(Level $level, $x, $y, $z){ return StaticBlock::getAABB($level, $x, $y, $z); @@ -216,9 +219,10 @@ abstract class Block extends Position{ return MovingObjectPosition::fromBlock($x, $y, $z, $v14, $v13->add($x, $y, $z)); } - + public static function onPlace(Level $level, $x, $y, $z){} public static function addVelocityToEntity(Level $level, $x, $y, $z, Entity $entity, Vector3 $velocityVector){} public static function onRandomTick(Level $level, $x, $y, $z){} + public static function onUpdate(Level $level, $x, $y, $z, $type){} public static function fallOn(Level $level, $x, $y, $z, Entity $entity, $fallDistance){} public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ return [static::getAABB($level, $x, $y, $z)]; @@ -298,8 +302,6 @@ abstract class Block extends Position{ abstract function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz); abstract function onActivate(Item $item, Player $player); - - abstract function onUpdate($type); } /***REM_START***/ diff --git a/src/material/block/DoorBlock.php b/src/material/block/DoorBlock.php index d2ba29b0..abfe20a6 100644 --- a/src/material/block/DoorBlock.php +++ b/src/material/block/DoorBlock.php @@ -104,25 +104,21 @@ class DoorBlock extends TransparentBlock{ return $metaLower & 7 | (($myMeta & 8) != 0 ? 8 : 0) | (($metaUpper & 1 != 0) ? 16 : 0); } + - /** - * @param int $type - * - * @return bool|int - */ - public function onUpdate($type){ - if($type === BLOCK_UPDATE_NORMAL){ - if($this->getSide(0)->getID() === AIR){ //Replace with common break method - $this->level->setBlock($this, new AirBlock(), false); - if($this->getID() == 64) ServerAPI::request()->api->entity->drop(new Position($this->x+0.5, $this->y, $this->z+0.5, $this->level), BlockAPI::getItem(324, 0, 1)); - elseif($this->getID() == 71) ServerAPI::request()->api->entity->drop(new Position($this->x+0.5, $this->y, $this->z+0.5, $this->level), BlockAPI::getItem(330, 0, 1)); - if($this->getSide(1) instanceof DoorBlock){ - $this->level->setBlock($this->getSide(1), new AirBlock(), false); - } - return BLOCK_UPDATE_NORMAL; + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + if($level->level->getBlockID($x, $y - 1, $z) == AIR){ //Replace with common break method + $level->fastSetBlockUpdate($x, $y, $z, 0, 0); + $id = $level->level->getBlockID($x, $y, $z); + + if($id == 64) ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(324, 0, 1)); + elseif($id == 71) ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(330, 0, 1)); + + $top = $level->level->getBlockID($x, $y + 1, $z); + if($top == IRON_DOOR_BLOCK || $top == DOOR_BLOCK){ + $level->fastSetBlockUpdate($x, $y + 1, $z, 0, 0); } } - return false; } diff --git a/src/material/block/FallableBlock.php b/src/material/block/FallableBlock.php index 856050fa..ca8113db 100644 --- a/src/material/block/FallableBlock.php +++ b/src/material/block/FallableBlock.php @@ -25,7 +25,8 @@ class FallableBlock extends SolidBlock{ */ public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ $ret = $this->level->setBlock($this, $this, true, false, true); - ServerAPI::request()->api->block->blockUpdate(clone $this, BLOCK_UPDATE_NORMAL); + $clz = StaticBlock::getBlock($this->getID()); + $clz::neighborChanged($this->level, $this->x, $this->y, $this->z, 0, 0, 0, 0); //TODO move to another method return $ret; } } \ No newline at end of file diff --git a/src/material/block/GenericBlock.php b/src/material/block/GenericBlock.php index f8ffb163..a4867017 100644 --- a/src/material/block/GenericBlock.php +++ b/src/material/block/GenericBlock.php @@ -52,25 +52,23 @@ class GenericBlock extends Block{ * * @return boolean */ - public function onUpdate($type){ - if($this->hasPhysics === true and $type === BLOCK_UPDATE_NORMAL){ - $down = $this->getSide(0); - if($down->getID() === AIR or ($down instanceof LiquidBlock)){ + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + [$id, $meta] = $level->level->getBlock($x, $y, $z); + if(StaticBlock::getHasPhysics($id)){ //TODO move from here to different class + $down = $level->level->getBlockID($x, $y - 1, $z); + if($down == AIR || StaticBlock::getIsLiquid($down)){ $data = array( - "x" => $this->x + 0.5, - "y" => $this->y, - "z" => $this->z + 0.5, - "Tile" => $this->id, + "x" => $x + 0.5, + "y" => $y - 0.5, + "z" => $z + 0.5, + "Tile" => $id, ); $server = ServerAPI::request(); - $this->level->setBlock($this, new AirBlock(), false, false, true); - $e = $server->api->entity->add($this->level, ENTITY_FALLING, FALLING_SAND, $data); + $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); + $e = $server->api->entity->add($level, ENTITY_FALLING, FALLING_SAND, $data); $server->api->entity->spawnToAll($e); - $server->api->block->blockUpdateAround(clone $this, BLOCK_UPDATE_NORMAL, 1); } - return false; } - return false; } /** diff --git a/src/material/block/LiquidBlock.php b/src/material/block/LiquidBlock.php index 2cfa7f72..001a23ed 100644 --- a/src/material/block/LiquidBlock.php +++ b/src/material/block/LiquidBlock.php @@ -15,6 +15,23 @@ class LiquidBlock extends TransparentBlock{ $this->isFullBlock = true; $this->hardness = 500; } + public static $blockID = 0; + public static function getDepth(Level $level, $x, $y, $z){ + [$id, $meta] = $level->level->getBlock($x, $y, $z); + return match(static::$blockID){ + WATER, STILL_WATER => $id == WATER || $id == STILL_WATER ? $meta : -1, + LAVA, STILL_LAVA => $id == LAVA || $id == STILL_LAVA ? $meta : -1, + default=> -1 + }; + } + + public static function onPlace(Level $level, $x, $y, $z){ + static::updateLiquid($level, $x, $y, $z); + } + + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + static::updateLiquid($level, $x, $y, $z); + } public static function getAABB(Level $level, $x, $y, $z){ return null; } @@ -24,6 +41,39 @@ class LiquidBlock extends TransparentBlock{ return $f; } + public static function getTickDelay(){ + throw new RuntimeException("If you see this, something bad happened"); + } + + public static function updateLiquid(Level $level, $x, $y, $z){ + [$id, $meta] = $level->level->getBlock($x, $y, $z); + if($id != LAVA && $id != STILL_LAVA) return; + + $zNeg = $level->level->getBlockID($x, $y, $z - 1); + $zPos = $level->level->getBlockID($x, $y, $z + 1); + $xNeg = $level->level->getBlockID($x - 1, $y, $z); + $xPos = $level->level->getBlockID($x + 1, $y, $z); + $yPos = $level->level->getBlockID($x, $y + 1, $z); + + if( + $zNeg == WATER || $zNeg == STILL_WATER + || $zPos == WATER || $zPos == STILL_WATER + || $xNeg == WATER || $xNeg == STILL_WATER + || $xPos == WATER || $xPos == STILL_WATER + || $yPos == WATER || $yPos == STILL_WATER + ) { + if($meta){ + //if($meta > 4) -> fizz & ret + $replacement = COBBLESTONE; + }else{ + $replacement = OBSIDIAN; + } + + $level->fastSetBlockUpdate($x, $y, $z, $replacement, true); + } + } + + public function getDrops(Item $item, Player $player){ return array(); } diff --git a/src/material/block/attachable/LadderBlock.php b/src/material/block/attachable/LadderBlock.php index e51d6888..97f83b37 100644 --- a/src/material/block/attachable/LadderBlock.php +++ b/src/material/block/attachable/LadderBlock.php @@ -1,13 +1,6 @@ 2, - 2 => 3, - 5 => 4, - 4 => 5, - ]; public static function getAABB(Level $level, $x, $y, $z){ [$id, $meta] = $level->level->getBlock($x, $y, $z); @@ -47,17 +40,21 @@ class LadderBlock extends TransparentBlock{ return false; } - public function onUpdate($type){ + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + $side = $level->level->getBlockDamage($x, $y, $z); - if($type === BLOCK_UPDATE_NORMAL){ - $side = $this->getMetadata(); - if($this->getSide(self::$faces[$side]) instanceof AirBlock){ //Replace with common break method - ServerAPI::request()->api->entity->drop($this, BlockAPI::getItem($this->id, 0, 1)); - $this->level->setBlock($this, new AirBlock(), true, false, true); - return BLOCK_UPDATE_NORMAL; - } + $attached = match($side){ + 3 => $level->level->getBlockID($x, $y, $z - 1), + 2 => $level->level->getBlockID($x, $y, $z + 1), + 5 => $level->level->getBlockID($x - 1, $y, $z), + 4 => $level->level->getBlockID($x + 1, $y, $z), + default => 0 //TODO + }; + + if($attached == AIR){ //Replace with common break method + ServerAPI::request()->api->entity->drop(new Position($x, $y, $z, $level), BlockAPI::getItem(LADDER, 0, 1)); + $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); } - return false; } public function getDrops(Item $item, Player $player){ diff --git a/src/material/block/attachable/SignPostBlock.php b/src/material/block/attachable/SignPostBlock.php index e94fae08..514b9fc5 100644 --- a/src/material/block/attachable/SignPostBlock.php +++ b/src/material/block/attachable/SignPostBlock.php @@ -33,15 +33,12 @@ class SignPostBlock extends TransparentBlock{ return false; } - public function onUpdate($type){ - if($type === BLOCK_UPDATE_NORMAL){ - if($this->getSide(0)->getID() === AIR){ //Replace with common break method - ServerAPI::request()->api->entity->drop($this, BlockAPI::getItem(SIGN, 0, 1)); - $this->level->setBlock($this, new AirBlock(), true, true, true); - return BLOCK_UPDATE_NORMAL; - } + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + if($level->level->getBlockID($x, $y - 1, $z) === AIR){ //Replace with common break method + ServerAPI::request()->api->entity->drop(new Position($x, $y, $z, $level), BlockAPI::getItem(SIGN, 0, 1)); + //$this->level->setBlock($this, new AirBlock(), true, true, true); + $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true, true); } - return false; } public function onBreak(Item $item, Player $player){ diff --git a/src/material/block/attachable/TorchBlock.php b/src/material/block/attachable/TorchBlock.php index 70e4ff76..c8b81963 100644 --- a/src/material/block/attachable/TorchBlock.php +++ b/src/material/block/attachable/TorchBlock.php @@ -14,26 +14,20 @@ class TorchBlock extends FlowableBlock implements LightingBlock{ return null; } - public function onUpdate($type){ - if($type === BLOCK_UPDATE_NORMAL){ - $side = $this->getMetadata(); - $faces = array( - 1 => 4, - 2 => 5, - 3 => 2, - 4 => 3, - 5 => 0, - 6 => 0, - 0 => 0, - ); - - if($this->getSide($faces[$side])->isTransparent === true and !($side === 0 and $this->getSide(0)->getID() === FENCE)){ //Replace with common break method - ServerAPI::request()->api->entity->drop($this, BlockAPI::getItem($this->id, 0, 1)); - $this->level->setBlock($this, new AirBlock(), true, false, true); - return BLOCK_UPDATE_NORMAL; - } + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + $side = $level->level->getBlockDamage($x, $y, $z); + $attach = match($side){ + 1 => $level->level->getBlockID($x - 1, $y, $z), + 2 => $level->level->getBlockID($x + 1, $y, $z), + 3 => $level->level->getBlockID($x, $y, $z - 1), + 4 => $level->level->getBlockID($x, $y, $z + 1), + default => $level->level->getBlockID($x, $y - 1, $z) + }; + + if(StaticBlock::getIsTransparent($attach) && !($side === 0 && $attach === FENCE)){ //Replace with common break method + ServerAPI::request()->api->entity->drop(new Position($x, $y, $z, $level), BlockAPI::getItem(TORCH, 0, 1)); + $level->fastSetBlockUpdate($x, $y, $z, 0, 0); } - return false; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ diff --git a/src/material/block/attachable/TrapdoorBlock.php b/src/material/block/attachable/TrapdoorBlock.php index 7d638722..5674318b 100644 --- a/src/material/block/attachable/TrapdoorBlock.php +++ b/src/material/block/attachable/TrapdoorBlock.php @@ -74,26 +74,21 @@ class TrapdoorBlock extends TransparentBlock{ array($this->id, 0, 1), ); } - public function onUpdate($type){ - if($type === BLOCK_UPDATE_NORMAL){ - $faces = array( //meta => side - 0 => 3, - 4 => 3, //activated - 1 => 2, - 5 => 2, //activated - 2 => 5, - 6 => 5, //activated - 3 => 4, - 7 => 4, //activated - ); - $side = $faces[$this->meta]; - if($this->getSide($side) instanceof AirBlock){ //Replace with common break method - ServerAPI::request()->api->entity->drop($this, BlockAPI::getItem($this->id, 0, 1)); - $this->level->setBlock($this, new AirBlock(), true, false, true); - return BLOCK_UPDATE_NORMAL; - } + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + $attach = match($level->level->getBlockDamage($x, $y, $z)){ + 0, 4 => $level->level->getBlockID($x, $y, $z + 1), + 1, 5 => $level->level->getBlockID($x, $y, $z - 1), + 2, 6 => $level->level->getBlockID($x + 1, $y, $z), + 3, 7 => $level->level->getBlockID($x - 1, $y, $z), + default => 0 + }; + + if($attach == AIR){ //Replace with common break method + ServerAPI::request()->api->entity->drop(new Position($x, $y, $z, $level), BlockAPI::getItem(TRAPDOOR, 0, 1)); + $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); } } + public function onActivate(Item $item, Player $player){ $this->meta ^= 0x04; $this->level->setBlock($this, $this, true, false, true); diff --git a/src/material/block/attachable/WallSignBlock.php b/src/material/block/attachable/WallSignBlock.php index 5f7f0f12..5eff39af 100644 --- a/src/material/block/attachable/WallSignBlock.php +++ b/src/material/block/attachable/WallSignBlock.php @@ -5,27 +5,23 @@ require_once("SignPostBlock.php"); /***REM_END***/ class WallSignBlock extends SignPostBlock{ - - public static $meta2side = [ - 2 => 3, - 3 => 2, - 4 => 5, - 5 => 4 - ]; - - public function __construct($meta = 0){ TransparentBlock::__construct(WALL_SIGN, $meta, "Wall Sign"); $this->isSolid = false; } - - public function onUpdate($type){ - $attached = $this->getSide(self::$meta2side[$this->meta] ?? -1); - if(!($attached instanceof Block) || (!$attached->isSolid && $attached->getID() != SIGN_POST && $attached->getID() != WALL_SIGN)){ - $this->level->setBlock($this, new AirBlock(), true, true, true); - (ServerAPI::request())->api->entity->drop(new Position($this->x + 0.5, $this->y + 0.5, $this->z + 0.5, $this->level), BlockAPI::getItem(SIGN, 0, 1)); + + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + $attached = match($level->level->getBlockDamage($x, $y, $z)){ + 2 => $level->level->getBlockID($x, $y, $z + 1), + 3 => $level->level->getBlockID($x, $y, $z - 1), + 4 => $level->level->getBlockID($x + 1, $y, $z), + 5 => $level->level->getBlockID($x - 1, $y, $z), + default => WALL_SIGN + }; + + if(!StaticBlock::getIsSolid($attached) && $attached != SIGN_POST && $attached != WALL_SIGN){ + $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true, true); + (ServerAPI::request())->api->entity->drop(new Position($x + 0.5, $y + 0.5, $z + 0.5, $level), BlockAPI::getItem(SIGN, 0, 1)); } - - return false; } } \ No newline at end of file diff --git a/src/material/block/liquid/LavaBlock.php b/src/material/block/liquid/LavaBlock.php index 5d6bff7a..21249474 100644 --- a/src/material/block/liquid/LavaBlock.php +++ b/src/material/block/liquid/LavaBlock.php @@ -1,6 +1,8 @@ hardness = 500; @@ -8,121 +10,15 @@ class LavaBlock extends LiquidBlock implements LightingBlock{ public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ $ret = $this->level->setBlock($this, $this, true, false, true); - ServerAPI::request()->api->block->scheduleBlockUpdate(clone $this, 40, BLOCK_UPDATE_NORMAL); + //TODO place ServerAPI::request()->api->block->scheduleBlockUpdate(clone $this, 40, BLOCK_UPDATE_NORMAL); return $ret; } + public static function getTickDelay(){ + return 30; + } + public function getMaxLightValue(){ return 15; } - - public function getSourceCount(){ - $count = 0; - for($side = 2; $side <= 5; ++$side){ - if($this->getSide($side) instanceof LavaBlock ){ - $b = $this->getSide($side); - $level = $b->meta & 0x06; - if($level == 0x00){ - $count++; - } - } - } - return $count; - } - - public function checkWater(){ - for($side = 0; $side <= 5; ++$side){ - $b = $this->getSide($side); - if($b instanceof WaterBlock){ - $level = $this->meta & 0x07; - if($level == 0x00){ - $this->level->setBlock($b, new ObsidianBlock(), false, false, true); - }else{ - $this->level->setBlock($b, new CobblestoneBlock(), false, false, true); - } - } - } - } - - public function getFrom(){ - for($side = 0; $side <= 5; ++$side){ - $b = $this->getSide($side); - if($b instanceof LavaBlock){ - $tlevel = $b->meta & 0x06; - $level = $this->meta & 0x06; - if( ($tlevel + 2) == $level || ($side == 0x01 && $level == 0x01 ) || ($tlevel == 6 && $level == 6 )){ - return $b; - } - } - } - return null; - } - - public function onUpdate($type){ - //return false; - $newId = $this->id; - $level = $this->meta & 0x06; - if($type !== BLOCK_UPDATE_NORMAL){ - return false; - } - - /*if($this->checkWater()){ - return; - }*/ - $falling = $this->meta >> 3; - $down = $this->getSide(0); - if($down instanceof WaterBlock) { - $this->level->fastSetBlockUpdate($down->x, $down->y, $down->z, STONE, 0, true); - return; - } - - $from = $this->getFrom(); - if($from !== null || $level == 0x00){ - if($down instanceof AirBlock || $down instanceof LavaBlock){ - $this->level->setBlock($down, new LavaBlock(0x01), false, false, true); - ServerAPI::request()->api->block->scheduleBlockUpdate($down, 40, BLOCK_UPDATE_NORMAL); - }elseif($level !== 0x06){ - for($side = 2; $side <= 5; ++$side){ - $b = $this->getSide($side); - if($b instanceof LavaBlock){ - - }elseif($b->isFlowable === true){ - $this->level->setBlock($b, new LavaBlock( min($level + 2,6) ), false, false, true); - ServerAPI::request()->api->block->scheduleBlockUpdate($b, 40, BLOCK_UPDATE_NORMAL); - } - } - } - }else{ - //Extend Remove for Left Lavas - for($side = 2; $side <= 5; ++$side){ - $sb = $this->getSide($side); - if($sb instanceof LavaBlock){ - $tlevel = $sb->meta & 0x06; - if($tlevel != 0x00){ - for ($s = 0; $s <= 5; $s++) { - $ssb = $sb->getSide($s); - ServerAPI::request()->api->block->scheduleBlockUpdate($ssb, 40, BLOCK_UPDATE_NORMAL); - } - $this->level->setBlock($sb, new AirBlock(), false, false, true); - } - } - $b = $this->getSide(0)->getSide($side); - if($b instanceof LavaBlock){ - $tlevel = $b->meta & 0x06; - if($tlevel != 0x00){ - for ($s = 0; $s <= 5; $s++) { - $ssb = $sb->getSide($s); - ServerAPI::request()->api->block->scheduleBlockUpdate($ssb, 40, BLOCK_UPDATE_NORMAL); - } - $this->level->setBlock($b, new AirBlock(), false, false, true); - } - } - //ServerAPI::request()->api->block->scheduleBlockUpdate(new Position($b, 0, 0, $this->level), 10, BLOCK_UPDATE_NORMAL); - } - - $this->level->setBlock($this, new AirBlock(), false, false, true); - } - return false; - } - } diff --git a/src/material/block/liquid/LiquidBlockDynamic.php b/src/material/block/liquid/LiquidBlockDynamic.php new file mode 100644 index 00000000..1dfcff3c --- /dev/null +++ b/src/material/block/liquid/LiquidBlockDynamic.php @@ -0,0 +1,213 @@ += 8) $depth = 0; + + return $highest >= 0 && $depth >= $highest ? $highest : $depth; + } + + public static function setStatic(Level $level, $x, $y, $z){ + $meta = $level->level->getBlockDamage($x, $y, $z); + + $level->fastSetBlockUpdate($x, $y, $z, static::$blockID + 1, $meta); + } + + public static function isWaterBlocking(Level $level, $x, $y, $z){ + $id = $level->level->getBlockID($x, $y, $z); + + if($id == AIR) return false; + + if($id == DOOR_BLOCK || $id == SIGN || $id == LADDER || $id == SUGARCANE_BLOCK) return true; + + if($id == CARPET || $id == SNOW_LAYER || $id == RAIL || $id == POWERED_RAIL) return false; //TODO Tile::getThickness() > 0 + + //TODO materials + //if(!StaticBlock::getIsFlowable($id)) return 1; + return StaticBlock::getIsSolid($id); + } + + public static function canSpreadTo(Level $level, $x, $y, $z){ + $id = $level->level->getBlockID($x, $y, $z); + if($id == LAVA || $id == STILL_LAVA) return false; + if((static::$blockID == WATER) && ($id == WATER || $id == STILL_WATER)) return false; + + return static::isWaterBlocking($level, $x, $y, $z) ^ 1; + } + + public static function getSpread(Level $level, $x, $y, $z){ + for($i = 0; $i < 4; ++$i){ + static::$spread[$i] = 1000; + $xs = $x; + $ys = $y; + $zs = $z; + + switch($i){ + case 0: + --$xs; + break; + case 1: + ++$xs; + break; + case 2: + --$zs; + break; + case 3: + ++$zs; + break; + } + + if(static::isWaterBlocking($level, $xs, $ys, $zs)) continue; + [$id, $meta] = $level->level->getBlock($xs, $ys, $zs); + if(((static::$blockID == WATER && ($id == WATER || $id == STILL_WATER)) || (static::$blockID == LAVA && ($id == LAVA || $id == STILL_LAVA))) && $meta == 0){ + continue; + } + + if(!static::isWaterBlocking($level, $xs, $ys - 1, $zs)) static::$spread[$i] = 0; + else static::$spread[$i] = static::getSlopeDistance($level, $xs, $ys, $zs, 1, $i); + } + $i1 = static::$spread[0]; + for($k1 = 1; $k1 < 4; ++$k1){ + if(static::$spread[$k1] < $i1) $i1 = static::$spread[$k1]; + } + $ba = []; + for($i = 0; $i < 4; ++$i) $ba[$i] = ($i1 == static::$spread[$i]); + return $ba; + } + + public static function getSlopeDistance(Level $level, $x, $y, $z, $l, $i1){ + $j1 = 1000; + for($k1 = 0; $k1 < 4; ++$k1){ + if($k1 == 0 && $i1 == 1 || $k1 == 1 && $i1 == 0 || $k1 == 2 && $i1 == 3 || $k1 == 3 && $i1 == 2){ + continue; + } + + $xs = $x; + $ys = $y; + $zs = $z; + + switch($k1){ + case 0: + --$xs; + break; + case 1: + ++$xs; + break; + case 2: + --$zs; + break; + case 3: + ++$zs; + break; + } + + if(static::isWaterBlocking($level, $xs, $ys, $zs)) continue; + [$id, $meta] = $level->level->getBlock($xs, $ys, $zs); + + if( + ((static::$blockID == WATER && ($id == WATER || $id == STILL_WATER)) || + (static::$blockID == LAVA && ($id == LAVA || $id == STILL_LAVA))) && + $meta == 0 + ){ + continue; + } + + if(!static::isWaterBlocking($level, $xs, $ys - 1, $zs)) return $l; + + if($l >= 4) continue; + $k2 = static::getSlopeDistance($level, $xs, $ys, $zs, $l + 1, $k1); + if($k2 < $j1) $j1 = $k2; + } + return $j1; + } + + public static function trySpreadTo(Level $level, $x, $y, $z, $meta){ + if(static::canSpreadTo($level, $x, $y, $z)){ + $id = $level->level->getBlockID($x, $y, $z); + + if($id > 0){ + if(($id == LAVA || $id == STILL_LAVA) && (static::$blockID == LAVA || static::$blockID == STILL_LAVA)); //fizz + else{ + //TODO spawnResources + } + } + + $level->fastSetBlockUpdate($x, $y, $z, static::$blockID, $meta, true); + } + } + + public static function onPlace(Level $level, $x, $y, $z){ + static::updateLiquid($level, $x, $y, $z); + $id = $level->level->getBlockID($x, $y, $z); + if($id == static::$blockID) ServerAPI::request()->api->block->scheduleBlockUpdateXYZ($level, $x, $y, $z, BLOCK_UPDATE_SCHEDULED, static::getTickDelay()); + } + + public static function onUpdate(Level $level, $x, $y, $z, $type){ + $id = $level->level->getBlockID($x, $y, $z); + $depth = static::getDepth($level, $x, $y, $z); + $flowAdd = (static::$blockID == LAVA) ? 2 : 1; + $flag = true; + + if($depth > 0){ + static::$sourcesAround = 0; + $highest = static::getHighest($level, $x - 1, $y, $z, -100); + $highest = static::getHighest($level, $x + 1, $y, $z, $highest); + $highest = static::getHighest($level, $x, $y, $z - 1, $highest); + $highest = static::getHighest($level, $x, $y, $z + 1, $highest); + $j1 = $highest + $flowAdd; + if($j1 >= 8 || $highest < 0) $j1 = -1; + $l1 = static::getDepth($level, $x, $y + 1, $z); + if($l1 >= 0) $j1 = ($l1 >= 8 ? $l1 : $l1 + 8); + + if(static::$sourcesAround >= 2 && static::$blockID == WATER){ + $idBot = $level->level->getBlockID($x, $y - 1, $z); + if($idBot > 0){ + if(StaticBlock::getIsSolid($idBot) || (($idBot == WATER || $idBot == STILL_WATER) && $level->level->getBlockDamage($x, $y, $z) == 0)) $j1 = 0; + } + } + + if(static::$blockID == LAVA && $depth < 8 && $j1 < 8 && $j1 > $depth && mt_rand(0, 4) != 0){ + $j1 = $depth; + $flag = false; + } + + if($j1 != $depth){ + $depth = $j1; + if($depth < 0) $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); + else{ + $level->fastSetBlockUpdateMeta($x, $y, $z, $depth, true); + ServerAPI::request()->api->block->scheduleBlockUpdateXYZ($level, $x, $y, $z, BLOCK_UPDATE_SCHEDULED, static::getTickDelay()); + $level->updateNeighborsAt($x, $y, $z, static::$blockID); //TODO check is needed + } + }else if($flag){ + static::setStatic($level, $x, $y, $z); + } + }else{ + static::setStatic($level, $x, $y, $z); + } + + if(static::canSpreadTo($level, $x, $y - 1, $z)){ + $level->fastSetBlockUpdate($x, $y - 1, $z, static::$blockID, ($depth >= 8 ? $depth : $depth + 8) & 0xf, true); + }else if($depth >= 0 && ($depth == 0 || static::isWaterBlocking($level, $x, $y - 1, $z))){ + $flags = static::getSpread($level, $x, $y, $z); + $k1 = $depth >= 8 ? 1 : ($depth + $flowAdd); + if($k1 >= 8) return; + + if($flags[0]) static::trySpreadTo($level, $x - 1, $y, $z, $k1); + if($flags[1]) static::trySpreadTo($level, $x + 1, $y, $z, $k1); + if($flags[2]) static::trySpreadTo($level, $x, $y, $z - 1, $k1); + if($flags[3]) static::trySpreadTo($level, $x, $y, $z + 1, $k1); + } + } +} \ No newline at end of file diff --git a/src/material/block/liquid/LiquidBlockStatic.php b/src/material/block/liquid/LiquidBlockStatic.php new file mode 100644 index 00000000..c37a461e --- /dev/null +++ b/src/material/block/liquid/LiquidBlockStatic.php @@ -0,0 +1,30 @@ +level->getBlock($x, $y, $z); + $dynamicID = $id - 1; //very unsafe + $level->fastSetBlockUpdate($x, $y, $z, $dynamicID, $meta); + $delay = static::getTickDelay(); + ServerAPI::request()->api->block->scheduleBlockUpdateXYZ($level, $x, $y, $z, BLOCK_UPDATE_SCHEDULED, $delay); + + } + + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + $oldID = $level->level->getBlockID($x, $y, $z); + static::updateLiquid($level, $x, $y, $z); + $newID = $level->level->getBlockID($x, $y, $z); + if($oldID == $newID){ + static::setDynamic($level, $x, $y, $z); + } + } + + +} \ No newline at end of file diff --git a/src/material/block/liquid/StillLavaBlock.php b/src/material/block/liquid/StillLavaBlock.php index ea82e135..2a8e89cf 100644 --- a/src/material/block/liquid/StillLavaBlock.php +++ b/src/material/block/liquid/StillLavaBlock.php @@ -1,9 +1,15 @@ hardness = 500; } + public static function getTickDelay(){ + return 30; + } + } \ No newline at end of file diff --git a/src/material/block/liquid/StillWaterBlock.php b/src/material/block/liquid/StillWaterBlock.php index 67ae7ab1..632177a3 100644 --- a/src/material/block/liquid/StillWaterBlock.php +++ b/src/material/block/liquid/StillWaterBlock.php @@ -3,12 +3,18 @@ /***REM_START***/ require_once("WaterBlock.php"); //TODO class loader? +require_once("LiquidBlockStatic.php"); //TODO class loader? + /***REM_END***/ -class StillWaterBlock extends WaterBlock{ +class StillWaterBlock extends LiquidBlockStatic{ + public static $blockID = STILL_WATER; public function __construct($meta = 0){ LiquidBlock::__construct(STILL_WATER, $meta, "Still Water"); $this->hardness = 500; } + public static function getTickDelay(){ + return 5; + } } \ No newline at end of file diff --git a/src/material/block/liquid/WaterBlock.php b/src/material/block/liquid/WaterBlock.php index e6ecc512..f0506a23 100644 --- a/src/material/block/liquid/WaterBlock.php +++ b/src/material/block/liquid/WaterBlock.php @@ -1,6 +1,10 @@ hardness = 500; @@ -8,115 +12,11 @@ class WaterBlock extends LiquidBlock{ public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ $ret = $this->level->setBlock($this, $this, true, false, true); - ServerAPI::request()->api->block->scheduleBlockUpdate(clone $this, 5, BLOCK_UPDATE_NORMAL); + //TODO place ServerAPI::request()->api->block->scheduleBlockUpdate(clone $this, 5, BLOCK_UPDATE_NORMAL); return $ret; } - public function getSourceCount(){ - $count = 0; - for($side = 2; $side <= 5; ++$side){ - if( $this->getSide($side) instanceof WaterBlock ){ - $b = $this->getSide($side); - $level = $b->meta & 0x07; - if($level == 0x00){ - $count++; - } - } - } - return $count; - } - - public function checkLava(){ - $ret = false; - for($side = 0; $side <= 5; ++$side){ - if($side == 1){ - continue; - } - $b = $this->getSide($side); - if($b instanceof LavaBlock){ - $level = $b->meta & 0x07; - if($level == 0x00){ - $this->level->setBlock($b, new ObsidianBlock(), false, false, true); - }else{ - $this->level->setBlock($b, new CobblestoneBlock(), false, false, true); - } - $ret = true; - } - } - return $ret; - } - - public function getFrom(){ - for($side = 0; $side <= 5; ++$side){ - $b = $this->getSide($side); - if($b instanceof WaterBlock){ - $tlevel = $b->meta & 0x07; - $level = $this->meta & 0x07; - if( ($tlevel + 1) == $level || ($side == 0x01 && $level == 0x01 )){ - return $b; - } - } - } - return null; - } - - public function onUpdate($type){ - //return false; - $newId = $this->id; - $level = $this->meta & 0x07; - - if($type !== BLOCK_UPDATE_NORMAL){ - return false; - } - - $this->checkLava(); - - $falling = $this->meta >> 3; - $down = $this->getSide(0); - - $from = $this->getFrom(); - //Has Source or Its Source - if($from !== null || $level == 0x00){ - if($down instanceof AirBlock || $down instanceof WaterBlock){ - $this->level->setBlock($down, new WaterBlock(1), false, false, true); //TODO fix somehow - ServerAPI::request()->api->block->scheduleBlockUpdate($down, 5, BLOCK_UPDATE_NORMAL); - }elseif($level !== 0x07){{ - for($side = 2; $side <= 5; ++$side){ - $b = $this->getSide($side); - if($b instanceof WaterBlock){ - if($this->getSourceCount() >= 2 && $level != 0x00){ //Infinite water source - $this->level->setBlock($this, new WaterBlock(0), false, false, true); - } - }elseif($b->isFlowable === true){ - $this->level->setBlock($b, new WaterBlock($level + 1), false, false, true); - ServerAPI::request()->api->block->scheduleBlockUpdate($b, 5, BLOCK_UPDATE_NORMAL); - } - } - } - } - }else{ - //Extend Remove for Left Waters - for($side = 2; $side <= 5; ++$side){ - $sb = $this->getSide($side); - if($sb instanceof WaterBlock){ - $tlevel = $sb->meta & 0x07; - if($tlevel != 0x00){ - ServerAPI::request()->api->block->blockUpdateAround($sb, BLOCK_UPDATE_NORMAL, 5); - $this->level->setBlock($sb, new AirBlock(), false, false, true); - } - } - $b = $this->getSide(0)->getSide($side); - if($b instanceof WaterBlock){ - $tlevel = $b->meta & 0x07; - if($tlevel != 0x00){ - ServerAPI::request()->api->block->blockUpdateAround($b, BLOCK_UPDATE_NORMAL, 5); - $this->level->setBlock($b, new AirBlock(), false, false, true); - } - } - } - ServerAPI::request()->api->block->blockUpdateAround($this, BLOCK_UPDATE_NORMAL, 5); - $this->level->setBlock($this, new AirBlock(), false, false, true); - } - return false; + public static function getTickDelay(){ + return 5; } } diff --git a/src/material/block/misc/FireBlock.php b/src/material/block/misc/FireBlock.php index c4908adf..8d906438 100644 --- a/src/material/block/misc/FireBlock.php +++ b/src/material/block/misc/FireBlock.php @@ -30,35 +30,37 @@ class FireBlock extends FlowableBlock implements LightingBlock{ } } + public static function onPlace(Level $level, $x, $y, $z){ + ServerAPI::request()->api->block->scheduleBlockUpdateXYZ($level, $x, $y, $z, BLOCK_UPDATE_SCHEDULED, 30); + } + public function getDrops(Item $item, Player $player){ return array(); } public function getMaxLightValue(){ return 15; } - public function onUpdate($type){ - if($type === BLOCK_UPDATE_NORMAL){ - for($s = 0; $s <= 5; ++$s){ - $side = $this->getSide($s); - if($side->getID() !== AIR and !($side instanceof LiquidBlock)){ - return false; - } - } - $this->level->setBlock($this, new AirBlock(), true, false, true); - return BLOCK_UPDATE_NORMAL; - }else if($type === BLOCK_UPDATE_SCHEDULED){ - $idBelow = $this->level->level->getBlockID($this->x, $this->y - 1, $this->z); + + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + $b = $level->level->getBlockID($x, $y - 1, $z); + if(!StaticBlock::getIsSolid($b)) $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); //TODO more vanilla later? + } + + public static function onUpdate(Level $level, $x, $y, $z, $type){ + if($type === BLOCK_UPDATE_SCHEDULED){ + $idBelow = $level->level->getBlockID($x, $y - 1, $z); + [$id, $meta] = $level->level->getBlock($x, $y, $z); $alwaysBurn = $idBelow == NETHERRACK; - if($this->meta < 15){ - $newMeta = $this->meta + 1; //TODO better formula + if($meta < 15){ + $newMeta = $meta + 1; //TODO better formula if($newMeta > 15) $newMeta = 15; - $this->level->fastSetBlockUpdate($this->x, $this->y, $this->z, $this->id, $newMeta); + $level->fastSetBlockUpdate($x, $y, $z, $id, $newMeta); } - if($this->meta == 15){ - if(!$alwaysBurn && !self::canBurn($this->level, $this->x, $this->y - 1, $this->z) && mt_rand(0, 4) == 0){ + if($meta == 15){ + if(!$alwaysBurn && !self::canBurn($level, $x, $y - 1, $z) && mt_rand(0, 4) == 0){ REMOVE_FIRE: - $this->level->fastSetBlockUpdate($this->x, $this->y, $this->z, 0, 0); + $level->fastSetBlockUpdate($x, $y, $z, 0, 0); return false; } } @@ -66,13 +68,12 @@ class FireBlock extends FlowableBlock implements LightingBlock{ if(mt_rand(0, 249) < $chance){ //TODO ignite tnt - $this->level->fastSetBlockUpdate($this->x, $this->y - 1, $this->z, 0, 0, true); + $level->fastSetBlockUpdate($x, $y - 1, $z, 0, 0, true); goto REMOVE_FIRE; } - $this->level->scheduleBlockUpdate($this, 30, BLOCK_UPDATE_SCHEDULED); //TODO looks like it also adds mt_rand(0, 9) to it + ServerAPI::request()->api->block->scheduleBlockUpdateXYZ($level, $x, $y, $z, BLOCK_UPDATE_SCHEDULED, 30); //TODO looks like it also adds mt_rand(0, 9) to it } - return false; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ parent::place($item, $player, $block, $target, $face, $fx, $fy, $fz); diff --git a/src/material/block/nonfull/CakeBlock.php b/src/material/block/nonfull/CakeBlock.php index 6d049dda..50a3baeb 100644 --- a/src/material/block/nonfull/CakeBlock.php +++ b/src/material/block/nonfull/CakeBlock.php @@ -29,14 +29,10 @@ class CakeBlock extends TransparentBlock{ return false; } - public function onUpdate($type){ - if($type === BLOCK_UPDATE_NORMAL){ - if($this->getSide(0)->getID() === AIR){ //Replace with common break method - $this->level->setBlock($this, new AirBlock(), true, false, true); - return BLOCK_UPDATE_NORMAL; - } + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + if($level->level->getBlockID($x, $y - 1, $z) == AIR){ //Replace with common break method + $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); } - return false; } public function getDrops(Item $item, Player $player){ diff --git a/src/material/block/nonfull/CarpetBlock.php b/src/material/block/nonfull/CarpetBlock.php index ed1903aa..0e9959e7 100644 --- a/src/material/block/nonfull/CarpetBlock.php +++ b/src/material/block/nonfull/CarpetBlock.php @@ -35,16 +35,11 @@ class CarpetBlock extends FlowableBlock{ } return false; } - - public function onUpdate($type){ - if($type === BLOCK_UPDATE_NORMAL){ - if($this->getSide(0)->getID() === AIR){ //Replace with common break method - ServerAPI::request()->api->entity->drop($this, BlockAPI::getItem($this->id, $this->meta, 1)); - $this->level->setBlock($this, new AirBlock(), true, false, true); - return BLOCK_UPDATE_NORMAL; - } + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + if($level->level->getBlockID($x, $y - 1, $z) == AIR){ //Replace with common break method + [$id, $meta] = $level->level->getBlock($x, $y, $z); + ServerAPI::request()->api->entity->drop(new Position($x + 0.5, $y, $z + 0.5, $level), BlockAPI::getItem($id, $meta)); + $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); } - return false; } - } \ No newline at end of file diff --git a/src/material/block/nonfull/FarmlandBlock.php b/src/material/block/nonfull/FarmlandBlock.php index 3eb447c6..b7d1fb20 100644 --- a/src/material/block/nonfull/FarmlandBlock.php +++ b/src/material/block/nonfull/FarmlandBlock.php @@ -58,14 +58,10 @@ class FarmlandBlock extends TransparentBlock{ } } - public function onUpdate($type){ - if($type === BLOCK_UPDATE_NORMAL){ - if(!$this->getSide(1)->isTransparent){ - $this->level->setBlock($this, BlockAPI::get(DIRT, 0), true, false, true); - return $type; - } + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + if(!StaticBlock::getIsTransparent($level->level->getBlockID($x, $y + 1, $z))){ + $level->fastSetBlockUpdate($x, $y, $z, DIRT, 0, true); } - return false; } public function getBlockID($x, $y, $z){ diff --git a/src/material/block/nonfull/RailBaseBlock.php b/src/material/block/nonfull/RailBaseBlock.php index d5fb7c0b..49c760c2 100644 --- a/src/material/block/nonfull/RailBaseBlock.php +++ b/src/material/block/nonfull/RailBaseBlock.php @@ -26,37 +26,23 @@ abstract class RailBaseBlock extends FlowableBlock //TODO move some methods here public function updateState(){} - public function onUpdate($type){ - if($type === BLOCK_UPDATE_NORMAL){ - - $meta = $this->getMetadata(); - if($this->id === POWERED_RAIL){ - $meta &= 7; - } - $x = $this->x; - $y = $this->y; - $z = $this->z; - if( - ($this->level->level->getBlockID($x, $y - 1, $z) === 0) || - (($meta == 2) && $this->level->level->getBlockID($x + 1, $y, $z) === 0) || - (($meta == 3) && $this->level->level->getBlockID($x - 1, $y, $z) === 0) || - (($meta == 4) && $this->level->level->getBlockID($x, $y, $z - 1) === 0) || - (($meta == 5) && $this->level->level->getBlockID($x, $y, $z + 1) === 0) - ){ - $this->level->setBlock($this, new AirBlock(), true, false, true); - ServerAPI::request()->api->entity->drop($this, BlockAPI::getItem($this->id, $this->meta, 1)); - }else{ - $this->updateState(); - } - - - //if($this->getSide(0)->getID() === AIR){//Replace with common break method - // ServerAPI::request()->api->entity->drop($this, BlockAPI::getItem($this->id, $this->meta, 1)); - // $this->level->setBlock($this, new AirBlock(), true, false, true); - //} - + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + [$id, $meta] = $level->level->getBlock($x, $y, $z); + if($id === POWERED_RAIL){ + $meta &= 7; + } + if( + ($level->level->getBlockID($x, $y - 1, $z) === 0) || + (($meta == 2) && $level->level->getBlockID($x + 1, $y, $z) === 0) || + (($meta == 3) && $level->level->getBlockID($x - 1, $y, $z) === 0) || + (($meta == 4) && $level->level->getBlockID($x, $y, $z - 1) === 0) || + (($meta == 5) && $level->level->getBlockID($x, $y, $z + 1) === 0) + ){ + $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); + ServerAPI::request()->api->entity->drop(new Position($x, $y, $z, $level), BlockAPI::getItem($id, $meta, 1)); + }else{ + //TODO fix me pls $this->updateState(); } - } } diff --git a/src/material/block/nonfull/SnowLayerBlock.php b/src/material/block/nonfull/SnowLayerBlock.php index 24ac9082..834cc71d 100644 --- a/src/material/block/nonfull/SnowLayerBlock.php +++ b/src/material/block/nonfull/SnowLayerBlock.php @@ -20,14 +20,10 @@ class SnowLayerBlock extends FlowableBlock{ return false; } - public function onUpdate($type){ - if($type === BLOCK_UPDATE_NORMAL){ - if($this->getSide(0)->getID() === AIR){ //Replace with common break method - $this->level->setBlock($this, new AirBlock(), true, false, true); - return BLOCK_UPDATE_NORMAL; - } + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + if($level->level->getBlockID($x, $y - 1, $z) == AIR){ //Replace with common break method + $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); } - return false; } public function getDrops(Item $item, Player $player){ diff --git a/src/material/block/ore/GlowingRedstoneOreBlock.php b/src/material/block/ore/GlowingRedstoneOreBlock.php index 1f679adb..fc123f00 100644 --- a/src/material/block/ore/GlowingRedstoneOreBlock.php +++ b/src/material/block/ore/GlowingRedstoneOreBlock.php @@ -5,16 +5,11 @@ class GlowingRedstoneOreBlock extends SolidBlock implements LightingBlock{ parent::__construct(GLOWING_REDSTONE_ORE, 0, "Glowing Redstone Ore"); $this->hardness = 15; } + public static function onRandomTick(Level $level, $x, $y, $z){ - $level->setBlock(new Position($x, $y, $z, $level), BlockAPI::get(REDSTONE_ORE, $level->level->getBlockDamage($x, $y, $z)), false, false, true); - } - public function onUpdate($type){ - if($type === BLOCK_UPDATE_SCHEDULED){ - $this->level->setBlock($this, BlockAPI::get(REDSTONE_ORE, $this->meta), false, false, true); - return BLOCK_UPDATE_WEAK; - } - return false; + $level->fastSetBlockUpdate($x, $y, $z, REDSTONE_ORE, 0); } + public function getMaxLightValue(){ return 9; } diff --git a/src/material/block/ore/RedstoneOreBlock.php b/src/material/block/ore/RedstoneOreBlock.php index 499a580e..ca134967 100644 --- a/src/material/block/ore/RedstoneOreBlock.php +++ b/src/material/block/ore/RedstoneOreBlock.php @@ -6,13 +6,8 @@ class RedstoneOreBlock extends SolidBlock{ $this->hardness = 15; } - public function onUpdate($type){ - if($type === BLOCK_UPDATE_NORMAL or $type === BLOCK_UPDATE_TOUCH){ - $this->level->setBlock($this, BlockAPI::get(GLOWING_REDSTONE_ORE, $this->meta), false, false, true); - $this->level->scheduleBlockUpdate(new Position($this, 0, 0, $this->level), Utils::getRandomUpdateTicks(), BLOCK_UPDATE_RANDOM); - return BLOCK_UPDATE_WEAK; - } - return false; + public static function interact(Level $level, $x, $y, $z, Player $player){ + $level->fastSetBlockUpdate($x, $y, $z, GLOWING_REDSTONE_ORE, 0); } public function getDrops(Item $item, Player $player){ diff --git a/src/material/block/plant/BeetrootBlock.php b/src/material/block/plant/BeetrootBlock.php index 8957769e..5a1b273a 100644 --- a/src/material/block/plant/BeetrootBlock.php +++ b/src/material/block/plant/BeetrootBlock.php @@ -39,15 +39,11 @@ class BeetrootBlock extends FlowableBlock{ return false; } - public function onUpdate($type){ - if($type === BLOCK_UPDATE_NORMAL){ - if($this->getSide(0)->getID() != 60){ - ServerAPI::request()->api->entity->drop(new Position($this->x + 0.5, $this->y, $this->z + 0.5, $this->level), BlockAPI::getItem(BEETROOT_SEEDS, 0, 1)); - $this->level->setBlock($this, new AirBlock(), false, false, true); - return BLOCK_UPDATE_NORMAL; - } + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + if($level->level->getBlockID($x, $y - 1, $z) != FARMLAND){ + ServerAPI::request()->api->entity->drop(new Position($x + 0.5, $y, $z + 0.5, $level), BlockAPI::getItem(BEETROOT_SEEDS, 0, 1)); + $level->fastSetBlockUpdate($x, $y, $z, 0, 0); } - return false; } public function getDrops(Item $item, Player $player){ diff --git a/src/material/block/plant/BrownMushroomBlock.php b/src/material/block/plant/BrownMushroomBlock.php index 85cb771c..d0892ccb 100644 --- a/src/material/block/plant/BrownMushroomBlock.php +++ b/src/material/block/plant/BrownMushroomBlock.php @@ -6,15 +6,11 @@ class BrownMushroomBlock extends FlowableBlock{ $this->hardness = 0; } - public function onUpdate($type){ - if($type === BLOCK_UPDATE_NORMAL){ - if($this->getSide(0)->isTransparent === true){ //Replace with common break method - ServerAPI::request()->api->entity->drop(new Position($this->x+0.5, $this->y, $this->z+0.5, $this->level), BlockAPI::getItem($this->id)); - $this->level->setBlock($this, new AirBlock(), false, false, true); - return BLOCK_UPDATE_NORMAL; - } + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + if(StaticBlock::getIsTransparent($level->level->getBlockID($x, $y - 1, $z))){ //Replace with common break method + ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(BROWN_MUSHROOM)); + $level->fastSetBlockUpdate($x, $y, $z, 0, 0); } - return false; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ diff --git a/src/material/block/plant/CactusBlock.php b/src/material/block/plant/CactusBlock.php index a4380251..48d1dffb 100644 --- a/src/material/block/plant/CactusBlock.php +++ b/src/material/block/plant/CactusBlock.php @@ -25,8 +25,7 @@ class CactusBlock extends TransparentBlock{ for($yy = 1; $yy < 3; ++$yy){ $bID = $level->level->getBlockID($x, $y + $yy, $z); if($bID === AIR){ - $level->fastSetBlockUpdate($x, $y + $yy, $z, CACTUS, 0); - $level->getBlockWithoutVector($x, $y + $yy, $z)->onUpdate(BLOCK_UPDATE_NORMAL); //TODO rewrite ticking + $level->fastSetBlockUpdate($x, $y + $yy, $z, CACTUS, 0, true); break; } } @@ -39,20 +38,16 @@ class CactusBlock extends TransparentBlock{ } } - public function onUpdate($type){ - if($type === BLOCK_UPDATE_NORMAL){ - $down = $this->getSide(0); - $block0 = $this->getSide(2); - $block1 = $this->getSide(3); - $block2 = $this->getSide(4); - $block3 = $this->getSide(5); - if($block0->isFlowable === false or $block1->isFlowable === false or $block2->isFlowable === false or $block3->isFlowable === false or ($down->getID() !== SAND and $down->getID() !== CACTUS)){ //Replace with common break method - $this->level->setBlock($this, new AirBlock(), true, false, true); - ServerAPI::request()->api->entity->drop(new Position($this->x + 0.5, $this->y, $this->z + 0.5, $this->level), BlockAPI::getItem($this->id)); - return BLOCK_UPDATE_NORMAL; - } + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + $down = $level->level->getBlockID($x, $y, $z); + $b0 = $level->level->getBlockID($x, $y, $z - 1); + $b1 = $level->level->getBlockID($x, $y, $z + 1); + $b2 = $level->level->getBlockID($x - 1, $y, $z); + $b3 = $level->level->getBlockID($x + 1, $y, $z); + if(!StaticBlock::getIsFlowable($b0) || !StaticBlock::getIsFlowable($b1) || !StaticBlock::getIsFlowable($b2) || !StaticBlock::getIsFlowable($b3) || ($down !== SAND and $down !== CACTUS)){ //Replace with common break method + $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); + ServerAPI::request()->api->entity->drop(new Position($x + 0.5, $y, $z + 0.5, $level), BlockAPI::getItem(CACTUS)); } - return false; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ diff --git a/src/material/block/plant/CarrotBlock.php b/src/material/block/plant/CarrotBlock.php index d84f598b..b82ab91a 100644 --- a/src/material/block/plant/CarrotBlock.php +++ b/src/material/block/plant/CarrotBlock.php @@ -41,15 +41,11 @@ class CarrotBlock extends FlowableBlock{ } } } - public function onUpdate($type){ - if($type === BLOCK_UPDATE_NORMAL){ - if($this->getSide(0)->getID() != 60){ - ServerAPI::request()->api->entity->drop(new Position($this->x + 0.5, $this->y, $this->z + 0.5, $this->level), BlockAPI::getItem(CARROT, 0, 1)); - $this->level->setBlock($this, new AirBlock(), false, false, true); - return BLOCK_UPDATE_NORMAL; - } + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + if($level->level->getBlockID($x, $y - 1, $z) != FARMLAND){ + ServerAPI::request()->api->entity->drop(new Position($x + 0.5, $y, $z + 0.5, $level), BlockAPI::getItem(CARROT, 0, 1)); + $level->fastSetBlockUpdate($x, $y, $z, 0, 0); } - return false; } public function getDrops(Item $item, Player $player){ diff --git a/src/material/block/plant/CyanFlowerBlock.php b/src/material/block/plant/CyanFlowerBlock.php index 67225b8b..4addd322 100644 --- a/src/material/block/plant/CyanFlowerBlock.php +++ b/src/material/block/plant/CyanFlowerBlock.php @@ -20,17 +20,15 @@ class CyanFlowerBlock extends FlowableBlock{ return false; } - public function onUpdate($type){ - if($type === BLOCK_UPDATE_NORMAL){ - if($this->getSide(0)->isTransparent === true){ //Replace with common break method - ServerAPI::request()->api->entity->drop(new Position($this->x + 0.5, $this->y, $this->z + 0.5, $this->level), BlockAPI::getItem($this->id)); - $this->level->setBlock($this, new AirBlock(), false, false, true); - return BLOCK_UPDATE_NORMAL; - } + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + if(StaticBlock::getIsTransparent($level->level->getBlockID($x, $y - 1, $z))){ //Replace with common break method + ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(CYAN_FLOWER)); + $level->fastSetBlockUpdate($x, $y, $z, 0, 0); } - return false; } + + public function onActivate(Item $item, Player $player){ if($item->getID() === DYE and $item->getMetadata() === 0x0F){ if(($player->gamemode & 0x01) === 0){ diff --git a/src/material/block/plant/DandelionBlock.php b/src/material/block/plant/DandelionBlock.php index def18792..bdd94561 100644 --- a/src/material/block/plant/DandelionBlock.php +++ b/src/material/block/plant/DandelionBlock.php @@ -18,15 +18,11 @@ class DandelionBlock extends FlowableBlock{ return false; } - public function onUpdate($type){ - if($type === BLOCK_UPDATE_NORMAL){ - if($this->getSide(0)->isTransparent === true){ //Replace with common break method - ServerAPI::request()->api->entity->drop(new Position($this->x+0.5, $this->y, $this->z+0.5, $this->level), BlockAPI::getItem($this->id)); - $this->level->setBlock($this, new AirBlock(), false, false, true); - return BLOCK_UPDATE_NORMAL; - } + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + if(StaticBlock::getIsTransparent($level->level->getBlockID($x, $y - 1, $z))){ //Replace with common break method + ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(DANDELION)); + $level->fastSetBlockUpdate($x, $y, $z, 0, 0); } - return false; } public function onActivate(Item $item, Player $player){ diff --git a/src/material/block/plant/DeadBushBlock.php b/src/material/block/plant/DeadBushBlock.php index a528b6f8..100d51f6 100644 --- a/src/material/block/plant/DeadBushBlock.php +++ b/src/material/block/plant/DeadBushBlock.php @@ -11,14 +11,10 @@ class DeadBushBlock extends FlowableBlock{ return null; } - public function onUpdate($type){ - if($type === BLOCK_UPDATE_NORMAL){ - if($this->getSide(0)->isTransparent === true){ //Replace with common break method - $this->level->setBlock($this, new AirBlock(), false, false, true); - return BLOCK_UPDATE_NORMAL; - } + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + if(StaticBlock::getIsTransparent($level->level->getBlockID($x, $y - 1, $z))){ //Replace with common break method + $level->fastSetBlockUpdate($x, $y, $z, 0, 0); } - return false; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ @@ -30,13 +26,13 @@ class DeadBushBlock extends FlowableBlock{ return false; } - public function getDrops(Item $item, Player $player){ - if($item->isShears()){ - return [ - [DEAD_BUSH, 0, 1], - ]; - } - - return []; + public function getDrops(Item $item, Player $player){ + if($item->isShears()){ + return [ + [DEAD_BUSH, 0, 1], + ]; } + + return []; + } } \ No newline at end of file diff --git a/src/material/block/plant/MelonStemBlock.php b/src/material/block/plant/MelonStemBlock.php index 8b5bbfba..ca9571b6 100644 --- a/src/material/block/plant/MelonStemBlock.php +++ b/src/material/block/plant/MelonStemBlock.php @@ -43,15 +43,11 @@ class MelonStemBlock extends FlowableBlock{ } } } - public function onUpdate($type){ - if($type === BLOCK_UPDATE_NORMAL){ - if($this->getSide(0)->getID() != 60){ - ServerAPI::request()->api->entity->drop(new Position($this->x+0.5, $this->y, $this->z+0.5, $this->level), BlockAPI::getItem(MELON_SEEDS, 0, mt_rand(0, 2))); - $this->level->setBlock($this, new AirBlock(), false, false, true); - return BLOCK_UPDATE_NORMAL; - } + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + if($level->level->getBlockID($x, $y - 1, $z) != FARMLAND){ + ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(MELON_SEEDS, 0, mt_rand(0, 2))); + $level->fastSetBlockUpdate($x, $y, $z, 0, 0); } - return false; } public function onActivate(Item $item, Player $player){ diff --git a/src/material/block/plant/PotatoBlock.php b/src/material/block/plant/PotatoBlock.php index 8a687985..35296a50 100644 --- a/src/material/block/plant/PotatoBlock.php +++ b/src/material/block/plant/PotatoBlock.php @@ -41,15 +41,11 @@ class PotatoBlock extends FlowableBlock{ return false; } - public function onUpdate($type){ - if($type === BLOCK_UPDATE_NORMAL){ - if($this->getSide(0)->getID() != 60){ - ServerAPI::request()->api->entity->drop(new Position($this->x + 0.5, $this->y, $this->z + 0.5, $this->level), BlockAPI::getItem(POTATO, 0, 1)); - $this->level->setBlock($this, new AirBlock(), false, false, true); - return BLOCK_UPDATE_NORMAL; - } + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + if($level->level->getBlockID($x, $y - 1, $z) != FARMLAND){ + ServerAPI::request()->api->entity->drop(new Position($x + 0.5, $y, $z + 0.5, $level), BlockAPI::getItem(POTATO, 0, 1)); + $level->fastSetBlockUpdate($x, $y, $z, 0, 0); } - return false; } public function getDrops(Item $item, Player $player){ diff --git a/src/material/block/plant/PumpkinStemBlock.php b/src/material/block/plant/PumpkinStemBlock.php index bd029c32..df914871 100644 --- a/src/material/block/plant/PumpkinStemBlock.php +++ b/src/material/block/plant/PumpkinStemBlock.php @@ -43,36 +43,11 @@ class PumpkinStemBlock extends FlowableBlock{ } } } - public function onUpdate($type){ - if($type === BLOCK_UPDATE_NORMAL){ - if($this->getSide(0)->getID() != 60){ - ServerAPI::request()->api->entity->drop(new Position($this->x + 0.5, $this->y, $this->z + 0.5, $this->level), BlockAPI::getItem(PUMPKIN_SEEDS, 0, mt_rand(0, 2))); - $this->level->setBlock($this, new AirBlock(), false, false, true); - return BLOCK_UPDATE_NORMAL; - } - }elseif($type === BLOCK_UPDATE_RANDOM){ - if(mt_rand(0, 2) == 1){ - if($this->meta < 0x07){ - ++$this->meta; - $this->level->setBlock($this, $this, true, false, true); - return BLOCK_UPDATE_RANDOM; - }else{ - for($side = 2; $side <= 5; ++$side){ - $b = $this->getSide($side); - if($b->getID() === PUMPKIN){ - return BLOCK_UPDATE_RANDOM; - } - } - $side = $this->getSide(mt_rand(2,5)); - $d = $side->getSide(0); - if($side->getID() === AIR and ($d->getID() === FARMLAND or $d->getID() === GRASS or $d->getID() === DIRT)){ - $this->level->setBlock($side, new PumpkinBlock(), true, false, true); - } - } - } - return BLOCK_UPDATE_RANDOM; + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + if($level->level->getBlockID($x, $y - 1, $z) != FARMLAND){ + ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(PUMPKIN_SEEDS, 0, mt_rand(0, 2))); + $level->fastSetBlockUpdate($x, $y, $z, 0, 0); } - return false; } public function onActivate(Item $item, Player $player){ diff --git a/src/material/block/plant/RedMushroomBlock.php b/src/material/block/plant/RedMushroomBlock.php index c9f43b7b..233ff0fc 100644 --- a/src/material/block/plant/RedMushroomBlock.php +++ b/src/material/block/plant/RedMushroomBlock.php @@ -6,15 +6,11 @@ class RedMushroomBlock extends FlowableBlock{ $this->hardness = 0; } - public function onUpdate($type){ - if($type === BLOCK_UPDATE_NORMAL){ - if($this->getSide(0)->isTransparent === true){ //Replace with common break method - ServerAPI::request()->api->entity->drop(new Position($this->x+0.5, $this->y, $this->z+0.5, $this->level), BlockAPI::getItem($this->id)); - $this->level->setBlock($this, new AirBlock(), false); - return BLOCK_UPDATE_NORMAL; - } + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + if(StaticBlock::getIsTransparent($level->level->getBlockID($x, $y - 1, $z))){ //Replace with common break method + ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(RED_MUSHROOM)); + $level->fastSetBlockUpdate($x, $y, $z, 0, 0); } - return false; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ diff --git a/src/material/block/plant/SaplingBlock.php b/src/material/block/plant/SaplingBlock.php index 0f7dffa4..42bc95a6 100644 --- a/src/material/block/plant/SaplingBlock.php +++ b/src/material/block/plant/SaplingBlock.php @@ -42,15 +42,12 @@ class SaplingBlock extends FlowableBlock{ } return false; } - public function onUpdate($type){ - if($type === BLOCK_UPDATE_NORMAL){ - if($this->getSide(0)->isTransparent === true){ //Replace with common break method - ServerAPI::request()->api->entity->drop(new Position($this->x + 0.5, $this->y, $this->z + 0.5, $this->level), BlockAPI::getItem($this->id, $this->getMetadata())); - $this->level->setBlock($this, new AirBlock(), false, false, true); - return BLOCK_UPDATE_NORMAL; - } + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + if(StaticBlock::getIsTransparent($level->level->getBlockID($x, $y - 1, $z))){ //Replace with common break method + [$id, $meta] = $level->level->getBlock($x, $y, $z); + ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem($id, $meta)); + $level->fastSetBlockUpdate($x, $y, $z, 0, 0); } - return false; } public static function onRandomTick(Level $level, $x, $y, $z){ if(mt_rand(1,7) === 1){ diff --git a/src/material/block/plant/SugarcaneBlock.php b/src/material/block/plant/SugarcaneBlock.php index 1f179a0b..b7eee207 100644 --- a/src/material/block/plant/SugarcaneBlock.php +++ b/src/material/block/plant/SugarcaneBlock.php @@ -36,29 +36,22 @@ class SugarcaneBlock extends FlowableBlock{ return false; } - public function onUpdate($type){ - - $down = $this->getSide(0); - if($down->getID() === GRASS or $down->getID() === DIRT or $down->getID() === SAND){ - $block0 = $down->getSide(2); - $block1 = $down->getSide(3); - $block2 = $down->getSide(4); - $block3 = $down->getSide(5); - if(!($block0 instanceof WaterBlock) and !($block1 instanceof WaterBlock) and !($block2 instanceof WaterBlock) and !($block3 instanceof WaterBlock)){ - $this->level->setBlock($this, new AirBlock(), true, false, true); - ServerAPI::request()->api->entity->drop($this, BlockAPI::getItem(SUGARCANE)); - return true; + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + $down = $level->level->getBlockID($x, $y - 1, $z); + if($down == GRASS or $down == DIRT or $down == SAND){ + $b0 = $level->level->getBlockID($x, $y - 1, $z - 1); + $b1 = $level->level->getBlockID($x, $y - 1, $z + 1); + $b2 = $level->level->getBlockID($x - 1, $y - 1, $z); + $b3 = $level->level->getBlockID($x + 1, $y - 1, $z); + if($b0 != WATER && $b0 != STILL_WATER && $b1 != WATER && $b1 != STILL_WATER && $b2 != WATER && $b2 != STILL_WATER && $b3 != WATER && $b3 != STILL_WATER){ + $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); + ServerAPI::request()->api->entity->drop(new Position($x, $y, $z, $level), BlockAPI::getItem(SUGARCANE)); } } - if($type === BLOCK_UPDATE_NORMAL){ - $down = $this->getSide(0); - if($down->isTransparent === true and $down->getID() !== SUGARCANE_BLOCK){ //Replace with common break method - ServerAPI::request()->api->entity->drop(new Position($this->x+0.5, $this->y, $this->z+0.5, $this->level), BlockAPI::getItem(SUGARCANE)); - $this->level->setBlock($this, new AirBlock(), false, false, true); - return BLOCK_UPDATE_NORMAL; - } + if(StaticBlock::getIsTransparent($down) && $down != SUGARCANE_BLOCK){ //Replace with common break method + ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(SUGARCANE)); + $level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); } - return false; } public static function onRandomTick(Level $level, $x, $y, $z){ $aboveID = $level->level->getBlockID($x, $y + 1, $z); diff --git a/src/material/block/plant/TallGrassBlock.php b/src/material/block/plant/TallGrassBlock.php index ff72cc56..33afa559 100644 --- a/src/material/block/plant/TallGrassBlock.php +++ b/src/material/block/plant/TallGrassBlock.php @@ -13,15 +13,11 @@ class TallGrassBlock extends FlowableBlock{ $this->hardness = 0; } - public function onUpdate($type){ - if($type === BLOCK_UPDATE_NORMAL){ - if($this->getSide(0)->isTransparent === true){//Replace with common break method - $this->level->setBlock($this, new AirBlock(), false, false, true); - if(Utils::chance(15)) ServerAPI::request()->api->entity->drop(new Position($this->x + 0.5, $this->y, $this->z + 0.5, $this->level), BlockAPI::getItem(WHEAT_SEEDS)); - return BLOCK_UPDATE_NORMAL; - } + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + if(StaticBlock::getIsTransparent($level->level->getBlockID($x, $y - 1, $z))){ //Replace with common break method + if(Utils::chance(15)) ServerAPI::request()->api->entity->drop(new Position($x+0.5, $y, $z+0.5, $level), BlockAPI::getItem(WHEAT_SEEDS)); + $level->fastSetBlockUpdate($x, $y, $z, 0, 0); } - return false; } public static function getAABB(Level $level, $x, $y, $z){ diff --git a/src/material/block/plant/WheatBlock.php b/src/material/block/plant/WheatBlock.php index 5b57ca81..80b46af0 100644 --- a/src/material/block/plant/WheatBlock.php +++ b/src/material/block/plant/WheatBlock.php @@ -41,15 +41,11 @@ class WheatBlock extends FlowableBlock{ } } } - public function onUpdate($type){ - if($type === BLOCK_UPDATE_NORMAL){ - if($this->getSide(0)->getID() != 60){ - ServerAPI::request()->api->entity->drop(new Position($this->x + 0.5, $this->y, $this->z + 0.5, $this->level), BlockAPI::getItem(WHEAT_SEEDS, 0, 1)); - $this->level->setBlock($this, new AirBlock(), false, false, true); - return BLOCK_UPDATE_NORMAL; - } + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + if($level->level->getBlockID($x, $y - 1, $z) != FARMLAND){ + ServerAPI::request()->api->entity->drop(new Position($x + 0.5, $y, $z + 0.5, $level), BlockAPI::getItem(WHEAT_SEEDS, 0, 1)); + $level->fastSetBlockUpdate($x, $y, $z, 0, 0); } - return false; } public function getDrops(Item $item, Player $player){ diff --git a/src/material/block/solid/IceBlock.php b/src/material/block/solid/IceBlock.php index e4eb6d59..e394825b 100644 --- a/src/material/block/solid/IceBlock.php +++ b/src/material/block/solid/IceBlock.php @@ -26,11 +26,7 @@ class IceBlock extends TransparentBlock{ } return true; } - /* - Scan for blocks which can emit light - args: offsetX: int, offsetY: int, offsetZ: int - do not set offsets more than 15 : no lighting sources are giving so much light - */ + private function scanForNearbyLightSources($offsetX, $offsetY, $offsetZ){ for($x = -$offsetX; $x <= $offsetX; ++$x){ //i hope it is possible to optimize it for($z = -$offsetZ; $z <= $offsetZ; ++$z){ @@ -38,7 +34,7 @@ class IceBlock extends TransparentBlock{ $pX = $this->x+$x; $pY = $this->y+$y; $pZ = $this->z+$z; - $block = $this->level->getBlock(new Vector3($pX, $pY, $pZ)); + $block = $this->level->getBlock(new Vector3($pX, $pY, $pZ)); //D= slow what was u thinking of gameherobrine from 2022 if($block instanceof LightingBlock){ //idk is it possible to make it better return $block; } @@ -46,7 +42,7 @@ class IceBlock extends TransparentBlock{ } } } - public function onUpdate($type){ /*Taken from https://github.com/PocketMine/PocketMine-MP/issues/3249*/ + //public static function onUpdate(Level $level, $x, $y, $z, $type){ /*Taken from https://github.com/PocketMine/PocketMine-MP/issues/3249*/ /*if($type === BLOCK_UPDATE_RANDOM){ $light = $this->scanForNearbyLightSources(3,3,3); if(LightUtils::getLightValueFromNearbySource($light,$this) > 12){ @@ -55,8 +51,8 @@ class IceBlock extends TransparentBlock{ } return BLOCK_UPDATE_RANDOM; } - return false;*/ //TODO i dont want it now - } + return false;*/ //TODO ice melting(after light?) + //} public function getBreakTime(Item $item, Player $player){ if(($player->gamemode & 0x01) === 0x01){ return 0.20; diff --git a/src/material/block/solid/LeavesBlock.php b/src/material/block/solid/LeavesBlock.php index b03e726e..c64564a8 100644 --- a/src/material/block/solid/LeavesBlock.php +++ b/src/material/block/solid/LeavesBlock.php @@ -64,14 +64,12 @@ class LeavesBlock extends TransparentBlock{ } } } - public function onUpdate($type){ - if($type === BLOCK_UPDATE_NORMAL){ - if(($this->meta & 0b00001100) === 0){ - $this->meta |= 0x08; - $this->level->fastSetBlockUpdate($this->x, $this->y, $this->z, $this->id, $this->meta); - } + public static function neighborChanged(Level $level, $x, $y, $z, $nX, $nY, $nZ, $oldID){ + [$id, $meta] = $level->level->getBlock($x, $y, $z); + if(($meta & 0b00001100) === 0){ + $meta |= 0x08; + $level->fastSetBlockUpdate($x, $y, $z, $id, $meta); //TODO maybe use $level->level->setBlock directly, the client doesnt need to know thism info. } - return false; } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ diff --git a/src/material/item/generic/BucketItem.php b/src/material/item/generic/BucketItem.php index 4a71ecbf..e887585f 100644 --- a/src/material/item/generic/BucketItem.php +++ b/src/material/item/generic/BucketItem.php @@ -16,7 +16,7 @@ class BucketItem extends Item{ public function onActivate(Level $level, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ if($this->meta === AIR){ - if($target instanceof LiquidBlock){ + if($block instanceof LiquidBlock){ $level->setBlock($target, new AirBlock(), true, false, true); if(($player->gamemode & 0x01) === 0){ $this->meta = ($target instanceof WaterBlock) ? WATER : LAVA; @@ -24,11 +24,9 @@ class BucketItem extends Item{ return true; } }elseif($this->meta === WATER){ - //Support Make Non-Support Water to Support Water - if($block->getID() === AIR || ( $block instanceof WaterBlock && ($block->getMetadata() & 0x07) != 0x00 ) || ($block instanceof LavaBlock)){ + if($block->getID() === AIR || $block instanceof LiquidBlock){ $water = new WaterBlock(); $level->setBlock($block, $water, true, false, true); - ServerAPI::request()->api->block->scheduleBlockUpdate($block, 5, BLOCK_UPDATE_NORMAL); //$water->place($this, $player, $block, $target, $face, $fx, $fy, $fz); if(($player->gamemode & 0x01) === 0){ $this->meta = 0; @@ -36,21 +34,14 @@ class BucketItem extends Item{ return true; } }elseif($this->meta === LAVA){ - if($block->getID() === AIR || ($block instanceof LavaBlock && ($block->getMetadata() & 0x6) != 0)){ + if($block->getID() === AIR || $block instanceof LiquidBlock){ $lava = new LavaBlock(); $level->setBlock($block, $lava, true, false, true); - ServerAPI::request()->api->block->scheduleBlockUpdate($block, 40, BLOCK_UPDATE_NORMAL); //$lava->place(clone $this, $player, $block, $target, $face, $fx, $fy, $fz); if(($player->gamemode & 0x01) === 0){ $this->meta = 0; } return true; - }elseif($block instanceof WaterBlock){ - $level->setBlock($block, new ObsidianBlock(), true, false, true); - if(($player->gamemode & 0x01) === 0){ - $this->meta = 0; - } - return true; } } return false; diff --git a/src/material/item/tool/FlintSteelItem.php b/src/material/item/tool/FlintSteelItem.php index 357ccac7..134b160a 100644 --- a/src/material/item/tool/FlintSteelItem.php +++ b/src/material/item/tool/FlintSteelItem.php @@ -11,9 +11,9 @@ class FlintSteelItem extends ItemTool{ if(($player->gamemode & 0x01) === 0 and $this->useOn($block) and $this->getMetadata() >= $this->getMaxDurability()){ $player->setSlot($player->slot, new Item(AIR, 0, 0), false); } - if($block->getID() === AIR and ($target instanceof SolidBlock)){ - $level->setBlock($block, new FireBlock(), true, false, true); - $level->scheduleBlockUpdate($block, 30, BLOCK_UPDATE_SCHEDULED); + + if($block->getID() === AIR && $target->isSolid){ + $level->fastSetBlockUpdate($block->x, $block->y, $block->z, FIRE, 0, true); return true; } return false; diff --git a/src/pmf/PMFLevel.php b/src/pmf/PMFLevel.php index 0eb99304..3dfb7cfa 100644 --- a/src/pmf/PMFLevel.php +++ b/src/pmf/PMFLevel.php @@ -369,7 +369,14 @@ class PMFLevel extends PMF{ $aX = $x & 0xf; $aZ = $z & 0xf; $aY = $y & 0xf; - $this->chunks[$index][$Y][(int) ($aY + ($aX << 5) + ($aZ << 9))] = chr($block); + $bind = (int) ($aY + ($aX << 5) + ($aZ << 9)); + if($this->chunks[$index][$Y][$bind] == chr($block)){ + return false; //no changes done + }else{ + $this->chunks[$index][$Y][$bind] = chr($block); + if($block > 0) StaticBlock::getBlock($block)::onPlace($this->level, $x, $y, $z); + } + if(!isset($this->chunkChange[$index][$Y])){ $this->chunkChange[$index][$Y] = 1; }else{ @@ -500,13 +507,8 @@ class PMFLevel extends PMF{ ++$this->chunkChange[$index][$Y]; } $this->chunkChange[$index][-1] = true; - if($old_b instanceof LiquidBlock){ - $pos = new Position($x, $y, $z, $this->level); - for($side = 0; $side <= 5; ++$side){ - $b = $pos->getSide($side); - ServerAPI::request()->api->block->scheduleBlockUpdate($b, ($b instanceof LavaBlock ? 40: 10), BLOCK_UPDATE_NORMAL); - } - } + + if($block > 0) StaticBlock::getBlock($block)::onPlace($this->level, $x, $y, $z); return true; } return false; diff --git a/src/world/Explosion.php b/src/world/Explosion.php index 96514a0e..71e75c8e 100644 --- a/src/world/Explosion.php +++ b/src/world/Explosion.php @@ -27,7 +27,7 @@ class Explosion{ $this->nullPlayer = new PlayerNull(); } - public function sub_expl(&$visited, $i, $mRays, $j, $k){ + public function sub_expl($i, $mRays, $j, $k){ $vector = new Vector3($i / $mRays * 2 - 1, $j / $mRays * 2 - 1, $k / $mRays * 2 - 1); //($i / $mRays) * 2 - 1 $vector = $vector->normalize()->multiply($this->stepLen); $pointer = clone $this->source; @@ -39,12 +39,6 @@ class Explosion{ $blockMeta = $BIDM[1]; if($blockID > 0){ $index = ($vBlock->x << 16) + ($vBlock->z << 8) + $vBlock->y; - - if(StaticBlock::getIsLiquid($blockID) && !isset($visited[$index])){ - ServerAPI::request()->api->block->scheduleBlockUpdate(new Position($vBlock->x, $vBlock->y, $vBlock->z, $this->level), 5, BLOCK_UPDATE_NORMAL); - $visited[$index] = true; - } - $blastForce -= (StaticBlock::getHardness($blockID) / 5 + 0.3) * $this->stepLen; if($blastForce > 0){ if(!isset($this->affectedBlocks[$index])){ @@ -103,45 +97,45 @@ class Explosion{ ]) === false){ return false; } - $visited = []; + $mRays = $this->rays - 1; $i = 0; for($j = 0; $j <= $mRays; ++$j){ for($k = 0; $k <= $mRays; ++$k){ - $this->sub_expl($visited, $i, $mRays, $j, $k); //i wish there was #define or inline + $this->sub_expl($i, $mRays, $j, $k); //i wish there was #define or inline } } $i = $mRays; for($j = 0; $j <= $mRays; ++$j){ for($k = 0; $k <= $mRays; ++$k){ - $this->sub_expl($visited, $i, $mRays, $j, $k); + $this->sub_expl($i, $mRays, $j, $k); } } $j = 0; for($i = 1; $i < $mRays; ++$i){ for($k = 0; $k <= $mRays; ++$k){ - $this->sub_expl($visited, $i, $mRays, $j, $k); + $this->sub_expl($i, $mRays, $j, $k); } } $j = $mRays; for($i = 1; $i < $mRays; ++$i){ for($k = 0; $k <= $mRays; ++$k){ - $this->sub_expl($visited, $i, $mRays, $j, $k); + $this->sub_expl($i, $mRays, $j, $k); } } $k = 0; for($i = 1; $i < $mRays; ++$i){ for($j = 1; $j < $mRays; ++$j){ - $this->sub_expl($visited, $i, $mRays, $j, $k); + $this->sub_expl($i, $mRays, $j, $k); } } $k = $mRays; for($i = 1; $i < $mRays; ++$i){ for($j = 1; $j < $mRays; ++$j){ - $this->sub_expl($visited, $i, $mRays, $j, $k); + $this->sub_expl($i, $mRays, $j, $k); } } //if($i == 0 or $i == $mRays or $j == 0 or $j == $mRays or $k == 0 or $k == $mRays){ @@ -202,7 +196,7 @@ class Explosion{ } } } - $this->level->fastSetBlockUpdate($x, $y, $z, 0, 0); + $this->level->fastSetBlockUpdate($x, $y, $z, 0, 0, true); $send[] = $xyz; } $pk = new ExplodePacket; diff --git a/src/world/Level.php b/src/world/Level.php index ea0c6b1a..8b02b4d4 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -459,18 +459,42 @@ class Level{ } public function fastSetBlockUpdateMeta($x, $y, $z, $meta, $updateBlock = false){ $this->level->setBlockDamage($x, $y, $z, $meta); - $this->addBlockToSendQueue($x, $y, $z, $this->level->getBlockID($x, $y, $z), $meta); + $id = $this->level->getBlockID($x, $y, $z); + $this->addBlockToSendQueue($x, $y, $z, $id, $meta); if($updateBlock){ - $this->server->api->block->blockUpdateAround(new Position($x, $y, $z, $this), BLOCK_UPDATE_NORMAL, 1); + $this->updateNeighborsAt($x, $y, $z, $id); } } - public function fastSetBlockUpdate($x, $y, $z, $id, $meta, $updateBlocksAround = false){ + public function updateNeighborsAt($x, $y, $z, $oldID){ + $block = $this->level->getBlockID($x - 1, $y, $z); + if($block) StaticBlock::getBlock($block)::neighborChanged($this, $x - 1, $y, $z, $x, $y, $z, $oldID); + $block = $this->level->getBlockID($x + 1, $y, $z); + if($block) StaticBlock::getBlock($block)::neighborChanged($this, $x + 1, $y, $z, $x, $y, $z, $oldID); + + $block = $this->level->getBlockID($x, $y - 1, $z); + if($block) StaticBlock::getBlock($block)::neighborChanged($this, $x, $y - 1, $z, $x, $y, $z, $oldID); + $block = $this->level->getBlockID($x, $y + 1, $z); + if($block) StaticBlock::getBlock($block)::neighborChanged($this, $x, $y + 1, $z, $x, $y, $z, $oldID); + + $block = $this->level->getBlockID($x, $y, $z - 1); + if($block) StaticBlock::getBlock($block)::neighborChanged($this, $x, $y, $z - 1, $x, $y, $z, $oldID); + $block = $this->level->getBlockID($x, $y, $z + 1); + if($block) StaticBlock::getBlock($block)::neighborChanged($this, $x, $y, $z + 1, $x, $y, $z, $oldID); + } + + public function fastSetBlockUpdate($x, $y, $z, $id, $meta, $updateBlocksAround = false, $tiles = false){ + $oldID = $this->level->getBlockID($x, $y, $z); + $this->level->setBlock($x, $y, $z, $id, $meta); - $this->addBlockToSendQueue($x, $y, $z, $id, $meta); - if($updateBlocksAround){ - $this->server->api->block->blockUpdateAround(new Position($x, $y, $z, $this), BLOCK_UPDATE_NORMAL, 1); + + if($tiles && ($t = $this->server->api->tile->getXYZ($this, $x, $y, $z)) != false){ //TODO rewrite + $t->close(); } + if($updateBlocksAround){ + self::updateNeighborsAt($x, $y, $z, $oldID); + } + $this->addBlockToSendQueue($x, $y, $z, $id, $meta); } public function onTick(PocketMinecraftServer $server, $currentTime){ @@ -679,6 +703,7 @@ class Level{ if(!isset($this->level) or (($pos instanceof Position) and $pos->level !== $this) or $pos->x < 0 or $pos->y < 0 or $pos->z < 0){ return false; } + $oldID = $this->level->getBlockID($pos->x, $pos->y, $pos->z); $ret = $this->level->setBlock($pos->x, $pos->y, $pos->z, $block->getID(), $block->getMetadata()); if($ret === true){ if(!($pos instanceof Position)){ @@ -696,15 +721,17 @@ class Level{ $this->changedBlocks[$i][] = [$block->x, $block->y, $block->z, $block->id, $block->getMetadata()]; ++$this->changedCount[$i]; } - - if($update === true){ - $this->server->api->block->blockUpdateAround($pos, BLOCK_UPDATE_NORMAL, 1); - } + if($tiles === true){ if(($t = $this->server->api->tile->get($pos)) !== false){ $t->close(); } } + + if($update === true){ + $this->updateNeighborsAt($pos->x, $pos->y, $pos->z, $oldID); + } + } return $ret; }