added some vanilla block props, some changes for block placement

This commit is contained in:
GameHerobrine 2026-04-28 15:46:03 +03:00
parent cf4dc73a6c
commit 8f9a9debc5
9 changed files with 178 additions and 63 deletions

View file

@ -24,14 +24,14 @@ class TaskEatTileGoal extends TaskBase
if (--$this->selfCounter == 4)
{
$id = $ai->entity->level->level->getBlockID($ai->entity->x, $ai->entity->y, $ai->entity->z);
$idb = $ai->entity->level->level->getBlockID($ai->entity->x, $ai->entity->y - 1, $ai->entity->z);
$id = $ai->entity->level->level->getBlockID((int)$ai->entity->x, (int)$ai->entity->y, (int)$ai->entity->z);
$idb = $ai->entity->level->level->getBlockID((int)$ai->entity->x, (int)$ai->entity->y - 1, (int)$ai->entity->z);
if($id === TALL_GRASS){
$ai->entity->level->fastSetBlockUpdate($ai->entity->x, $ai->entity->y, $ai->entity->z, AIR, 0);
$ai->entity->level->fastSetBlockUpdate((int)$ai->entity->x, (int)$ai->entity->y, (int)$ai->entity->z, AIR, 0);
$ai->entity->eatGrass();
}elseif($idb === GRASS){
$ai->entity->level->fastSetBlockUpdate($ai->entity->x, $ai->entity->y - 1, $ai->entity->z, DIRT, 0);
$ai->entity->level->fastSetBlockUpdate((int)$ai->entity->x, (int)$ai->entity->y - 1, (int)$ai->entity->z, DIRT, 0);
$ai->entity->eatGrass();
}
}
@ -41,8 +41,8 @@ class TaskEatTileGoal extends TaskBase
{
if($ai->isStarted("TaskRandomWalk") || $ai->isStarted("TaskEatTileGoal")) return false;
if(mt_rand(0, ($ai->entity instanceof Ageable && $ai->entity->isBaby()) ? 50 : 1000) == 0){
$idm = $ai->entity->level->level->getBlock($ai->entity->x, $ai->entity->y, $ai->entity->z);
$idb = $ai->entity->level->level->getBlockID($ai->entity->x, $ai->entity->y - 1, $ai->entity->z);
$idm = $ai->entity->level->level->getBlock((int)$ai->entity->x, (int)$ai->entity->y, (int)$ai->entity->z);
$idb = $ai->entity->level->level->getBlockID((int)$ai->entity->x, (int)$ai->entity->y - 1, (int)$ai->entity->z);
return ($idm[0] === TALL_GRASS && $idm[1] === 1) || $idb === GRASS;
}
return false;

View file

@ -253,6 +253,12 @@ abstract class Block extends Position{
$this->boundingBox = new AxisAlignedBB($this->x, $this->y, $this->z, $this->x + 1, $this->y + 1, $this->z + 1);
}
abstract function onBreak(Item $item, Player $player);
abstract function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz);
abstract function onActivate(Item $item, Player $player);
final public function getHardness(){
return ($this->hardness);
}
@ -325,12 +331,6 @@ abstract class Block extends Position{
public function isBreakable(Item $item, Player $player){
return $this->hardness >= 0;
}
abstract function onBreak(Item $item, Player $player);
abstract function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz);
abstract function onActivate(Item $item, Player $player);
}
/***REM_START***/

View file

@ -21,6 +21,8 @@ class StaticBlock
public static $isLiquid = [];
public static $isFullBlock = [];
public static $vIsSolid = []; //vanilla solid
public static $isCubeShaped = [];
public static $material = [];
public static $hardness = [];
@ -33,10 +35,18 @@ class StaticBlock
public static function init(){
self::$NULL_BOUNDS = new AxisAlignedBB(0, 0, 0, 0, 0, 0);
for($i = 0; $i < 256; ++$i) self::$lightBlock[$i] = self::$lightEmission[$i] = 0;
for($i = 0; $i < 256; ++$i) {
self::$lightBlock[$i] = self::$lightEmission[$i] = 0;
}
foreach(Block::$class as $nonstaticname){
/**@var Block $b*/
$b = new $nonstaticname();
//TODO merge vIsSolid and isSolid later?
//TODO dont use arrays
self::$vIsSolid[$b->getID()] = in_array($b->getID(), [1,2,3,4,5,7,12,13,14,15,16,17,19,21,22,24,35,41,42,43,45,46,47,48,49,56,57,58,61,62,73,74,79,80,82,86,87,89,91,95,98,103,112,155,157,170,173,245,246,247,248,249,255]);
self::$isCubeShaped[$b->getID()] = in_array($b->getID(), [1,2,3,4,5,7,12,13,14,15,16,17,18,19,20,21,22,24,35,41,42,43,45,46,47,48,49,56,57,58,61,62,73,74,79,80,82,86,87,89,91,95,98,103,112,155,157,170,173,245,246,247,248,249,255]);
self::$isSolid[$b->getID()] = $b->isSolid;
self::$isTransparent[$b->getID()] = $b->isTransparent;
@ -59,6 +69,7 @@ class StaticBlock
self::setBlockBounds($b->getID(), 0, 0, 0, 1, 1, 1);
}
self::setBlockBounds(BED_BLOCK, 0, 0, 0, 1, 0.5625, 1);
//Cake: has bounds based on world state
//Chest: has bounds based on world state
@ -157,10 +168,17 @@ class StaticBlock
return self::$slipperiness[$id] ?? StaticBlock::DEFAULT_SLIPPERINESS;
}
public static function getIsVSolid($id){
return self::$vIsSolid[$id] ?? false;
}
public static function getIsSolid($id){
return self::$isSolid[$id] ?? false;
}
public static function getIsCubeShaped($id){
return self::$isCubeShaped[$id] ?? false;
}
public static function getIsTransparent($id){
return self::$isTransparent[$id] ?? false;
}

View file

@ -36,15 +36,27 @@ class LadderBlock extends TransparentBlock{
$this->breakTime = 0.4;
$this->material = Material::$decoration;
}
public function getPlacementDataValue(Level $level, $x, $y, $z, $side){
$v = 0; //getLevelDataForAuxValue($item_aux_value); - should be 0
if(($v == 0 || $side == 2) && $level->isSolidBlockingTile($x, $y, $z + 1)) $v = 2;
if(($v == 0 || $side == 3) && $level->isSolidBlockingTile($x, $y, $z - 1)) $v = 3;
if(($v == 0 || $side == 4) && $level->isSolidBlockingTile($x + 1, $y, $z)) $v = 4;
if(($v == 0 || $side == 5) && $level->isSolidBlockingTile($x - 1, $y, $z)) $v = 5;
return $v; //should never happen due to previous check in ::place
}
public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){
if($face === 0 || $face === 1){
return false; //fix of placing invalid ids without array
}
if($target->isTransparent === false){
$this->meta = $face;
$x = $block->x;
$y = $block->y;
$z = $block->z;
$level = $block->level;
if($level->isSolidBlockingTile($x - 1, $y, $z) || $level->isSolidBlockingTile($x + 1, $y, $z) || $level->isSolidBlockingTile($x, $y, $z - 1) || $level->isSolidBlockingTile($x, $y, $z+1)){
$this->meta = static::getPlacementDataValue($level, $x, $y, $z, $face);
$this->level->setBlock($block, $this, true, false, true);
return true;
}
return false;
}

View file

@ -19,40 +19,85 @@ class TorchBlock extends FlowableBlock implements LightingBlock{
}
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);
if(static::checkCanSurvive($level, $x, $y, $z)){
$data = $level->level->getBlockDamage($x, $y, $z);
$destroy = !$level->isSolidBlockingTile($x-1, $y, $z) && $data == 1;
if(!$level->isSolidBlockingTile($x+1, $y, $z) && $data == 2) $destroy = true;
if(!$level->isSolidBlockingTile($x, $y, $z-1) && $data == 3) $destroy = true;
if(!$level->isSolidBlockingTile($x, $y, $z+1) && $data == 4) $destroy = true;
if((!static::isConnection($level, $x, $y-1, $z) && $data == 5) || $destroy){
ServerAPI::request()->api->entity->drop(new Position($x, $y, $z, $level), BlockAPI::getItem(TORCH, 0, 1));
$level->fastSetBlockUpdate($x, $y, $z, 0, 0);
}
}
}
public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){
if($target->isTransparent === false and $face !== 0){
$faces = array(
1 => 5,
2 => 4,
3 => 3,
4 => 2,
5 => 1,
);
$this->meta = $faces[$face];
$this->level->setBlock($block, $this, true, false, true);
public static function isConnection(Level $level, $x, $y, $z){
if($level->isSolidBlockingTile($x, $y, $z)){
return true;
}elseif($this->getSide(0)->isTransparent === false or $this->getSide(0)->getID() === FENCE or $this->getSide(0)->getID() === STONE_WALL){
$this->meta = 0;
}
$id = $level->level->getBlockID($x, $y, $z);
return $id == FENCE || $id == GLASS || $id == COBBLE_WALL;
}
public static function getPlacementDataValue(Level $level, $x, $y, $z, $side){
$def = 0;
if($side == 1){
if(static::isConnection($level, $x, $y-1, $z)) return 5;
}else if($side == 2){
if($level->isSolidBlockingTile($x, $y, $z+1)) return 4;
}
if($side == 5){
if($level->isSolidBlockingTile($x-1, $y, $z)) return 1;
}else if($side == 3){
if($level->isSolidBlockingTile($x, $y, $z-1)) return 3;
}else if($side == 4){
if($level->isSolidBlockingTile($x+1, $y, $z)) return 2;
}
return $def;
}
public static function checkCanSurvive(Level $level, $x, $y, $z){
if(static::mayPlace($level, $x, $y, $z)) return true;
if($level->level->getBlockID($x, $y, $z) == TORCH){
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 static function onPlace(Level $level, $x, $y, $z){
if($level->level->getBlockDamage($x, $y, $z) == 0){
if($level->isSolidBlockingTile($x-1, $y, $z)) $level->fastSetBlockUpdateMeta($x, $y, $z, 1);
if($level->isSolidBlockingTile($x+1, $y, $z)) $level->fastSetBlockUpdateMeta($x, $y, $z, 2);
if($level->isSolidBlockingTile($x, $y, $z-1)) $level->fastSetBlockUpdateMeta($x, $y, $z, 3);
if($level->isSolidBlockingTile($x, $y, $z+1)) $level->fastSetBlockUpdateMeta($x, $y, $z, 4);
if(static::isConnection($level, $x, $y-1, $z)) $level->fastSetBlockUpdateMeta($x, $y, $z, 5);
}
static::checkCanSurvive($level, $x, $y, $z);
}
public static function mayPlace(Level $level, $x, $y, $z){
return $level->isSolidBlockingTile($x - 1, $y, $z) || $level->isSolidBlockingTile($x + 1, $y, $z) || $level->isSolidBlockingTile($x, $y, $z - 1) || $level->isSolidBlockingTile($x, $y, $z+1) || static::isConnection($level, $x, $y-1, $z);
}
public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){
$x = $block->x;
$y = $block->y;
$z = $block->z;
$level = $block->level;
if(static::mayPlace($level, $x, $y, $z)){
$this->meta = static::getPlacementDataValue($level, $x, $y, $z, $face);
$this->level->setBlock($block, $this, true, false, true);
return true;
}
return false;
}
public function getDrops(Item $item, Player $player){
return array(
array($this->id, 0, 1),

View file

@ -14,10 +14,13 @@ class TrapdoorBlock extends TransparentBlock{
$this->breakTime = 3;
$this->material = Material::$wood;
}
public function canAttachTo(Block $target){
$id = $target->getID();
return $id === SLAB || $id === GLOWSTONE || $id === SLAB || $id === WOOD_SLAB || (!$target->isTransparent || $target instanceof StairBlock);
public static function attachesTo($id){
if($id <= 0) return false;
//check for non existent tiles is here - not needed?
//image using tile->getRenderShape() == 10 to check is stairs </3
return StaticBlock::getMaterial($id)->isSolidBlocking() && StaticBlock::getIsCubeShaped($id) || $id == GLOWSTONE || $id == SLAB || $id == WOODEN_SLAB || (StaticBlock::getBlock($id) instanceof StairBlock);
}
public static function isOpen($meta){
@ -59,18 +62,29 @@ class TrapdoorBlock extends TransparentBlock{
}
}
public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){
if(($this->canAttachTo($target)) and $face !== 0 and $face !== 1){
$faces = array(
2 => 0,
3 => 1,
4 => 2,
5 => 3,
);
$this->meta = $faces[$face] & 0x03;
$this->level->setBlock($block, $this, true, false, true);
return true;
}
if($face == 0 || $face == 1) return false;
$level = $block->level;
$xx = $block->x;
$y = $block->y;
$zz = $block->z;
if($face == 2) ++$zz;
if($face == 3) --$zz;
if($face == 4) ++$xx;
if($face == 5) --$xx;
if(static::attachesTo($level->level->getBlockID($xx, $y, $zz))){
$this->meta = match($face){
3 => 1,
4 => 2,
5 => 3,
default => 0
};
$this->level->setBlock($block, $this, true, false, true);
return true;
}
return false;
}
public function getDrops(Item $item, Player $player){
return array(
@ -97,4 +111,15 @@ class TrapdoorBlock extends TransparentBlock{
$this->level->setBlock($this, $this, true, false, true);
return true;
}
/**
* @deprecated use TrapdoorBlock::attachesTo($target->getID());
* @param Block $target
* @return boolean
*/
public function canAttachTo(Block $target){
$id = $target->getID();
return static::attachesTo($id);
}
}

View file

@ -14,6 +14,10 @@ class BaseMushroomBlock extends FlowableBlock{
$level->fastSetBlockUpdate($x, $y, $z, 0, 0);
}
}
public static function canSurvive(Level $level, $x, $y, $z){
return $level->getRawBrightness($x, $y, $z) <= 12 && StaticBlock::getIsSolid($level->level->getBlockID($x, $y-1, $z));
}
public static function onRandomTick(Level $level, $x, $y, $z){
if(Block::$mushroomSpread && mt_rand(0, 24) == 0){
@ -46,11 +50,11 @@ class BaseMushroomBlock extends FlowableBlock{
}
public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){
$down = $this->getSide(0);
if($down->isTransparent === false){
$this->level->setBlock($block, $this, true, false, true);
return true;
if($block->getID() == 0 || $block->material->replaceable){ //Tile::mayPlace
if(static::canSurvive($block->level, $block->x, $block->y, $block->z)){
$this->level->setBlock($block, $this, true, false, true);
return true;
}
}
return false;
}
}

View file

@ -8,6 +8,7 @@ class StoneBlock extends SolidBlock{
$this->breakTime = 1.5;
$this->material = Material::$stone;
$this->lightBlock = 255;
$this->vIsSolid = true;
}
public function getBreakTime(Item $item, Player $player){

View file

@ -518,6 +518,16 @@ class Level{
}
return $ret;
}
public function isSolidBlockingTile($x, $y, $z){
$id = $this->level->getBlockID($x, $y, $z);
if($id != 0 && StaticBlock::getMaterial($id)->isSolidBlocking()){
return StaticBlock::getIsCubeShaped($id);
}else{
return false;
}
}
public function fastSetBlockUpdateMeta($x, $y, $z, $meta, $updateBlock = false){
$this->level->setBlockDamage($x, $y, $z, $meta);
$id = $this->level->getBlockID($x, $y, $z);