diff --git a/src/API/EntityAPI.php b/src/API/EntityAPI.php index 4c9e5dcd..6aa80e5b 100644 --- a/src/API/EntityAPI.php +++ b/src/API/EntityAPI.php @@ -100,45 +100,53 @@ class EntityAPI{ case "despawn": $cnt = 0; if(!isset($args[0])){ - $output .= "/despawn "; - break; - }else{ - if($args[0] === "all"){ - $cnt = 0; - foreach($this->entities as $e){ - if(isset($e) && $e != null && !$e->isPlayer()){ //if player, not despawning - $this->remove($e->eid); - $cnt++; + return "/despawn "; + } + + $despawnclass = 0; + $despawntype = 0; + switch($args[0]){ + case "all": + foreach($this->entities as $entity){ + if($entity instanceof Entity && !$entity->isPlayer()){ + $entity->close(); + ++$cnt; } } - }else{ - $array = explode(",", strtolower($args[0])); - if(count($array) > 4){ - $output .= "Many arguments!"; - break; - } - //terrible code - $list = ""; - $temp = ["mobs" => "2", "objects" => "3", "items" => "4", "fallings" => "5"]; - foreach($array as $value){ - $list .= $temp[$value]." or "; - } - $despawning = substr($list, 0, -4); - $l = $this->server->query("SELECT EID FROM entities WHERE class = ".$despawning.";"); - if($l !== false and $l !== true){ - while(($e = $l->fetchArray(SQLITE3_ASSOC)) !== false){ - $e = $this->get($e["EID"]); - if($e instanceof Entity){ - $this->remove($e->eid); - $cnt++; - } - } + break; + case "minecarts": + $despawntype = OBJECT_MINECART; + break; + case "mobs": + $despawnclass = ENTITY_MOB; + break; + case "objects": + $despawnclass = ENTITY_OBJECT; + break; + case "items": + $despawnclass = ENTITY_ITEM; + break; + case "fallings": + $despawnclass = ENTITY_FALLING; + break; + } + if($despawnclass){ + foreach($this->entities as $entity){ + if($entity instanceof Entity && !$entity->isPlayer() && $entity->class === $despawnclass){ + $entity->close(); + ++$cnt; } } } - - $output = "$cnt entities have been despawned!"; - break; + if($despawntype){ + foreach($this->entities as $entity){ + if($entity instanceof Entity && !$entity->isPlayer() && $entity->type === $despawntype){ + $entity->close(); + ++$cnt; + } + } + } + return "$cnt entities have been despawned!"; } return $output; } diff --git a/src/entity/Entity.php b/src/entity/Entity.php index e30e6cf5..dc17b738 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -601,7 +601,7 @@ class Entity extends Position } $savedSpeedY = $this->speedY; - if($this->class === ENTITY_MOB || $this->class === ENTITY_ITEM || ($this->class === ENTITY_OBJECT && $this->type === OBJECT_PRIMEDTNT)){ + if($this->class === ENTITY_MOB || $this->class === ENTITY_ITEM || ($this->class === ENTITY_OBJECT && $this->type === OBJECT_PRIMEDTNT) || $this->class === ENTITY_FALLING){ $water = false; $aABB = $this->boundingBox->addCoord($this->speedX, $this->speedY, $this->speedZ); $x0 = floor($aABB->minX); @@ -645,26 +645,19 @@ class Entity extends Position } if($this->speedY != 0){ $ny = $this->y + $this->speedY; - if($this->class === ENTITY_FALLING && $ny <= $this->y){ - $x = (int) ($this->x - 0.5); - $z = (int) ($this->z - 0.5); - $lim = (int) floor($ny); - for($y = (int) ceil($this->y) - 1; $y >= $lim; -- $y){ - if($this->level->getBlockWithoutVector($x, $y, $z)->isSolid === true){ - //$support = true; - $this->y = $ny; - $fall = $this->level->getBlock(new Vector3(intval($this->x - 0.5), intval(ceil($this->y)), intval($this->z - 0.5))); - $down = $this->level->getBlock(new Vector3(intval($this->x - 0.5), intval(ceil($this->y) - 1), intval($this->z - 0.5))); - if($fall->isFullBlock === false or $down->isFullBlock === false){ - $this->server->api->entity->drop($this, BlockAPI::getItem($this->data["Tile"] & 0xFFFF, $this->data["Metadata"], 1), true); - } else{ - $this->level->setBlock($fall, BlockAPI::get($this->data["Tile"], $this->data["Metadata"]), true, false, true); - } - $this->server->api->handle("entity.motion", $this); - $this->close(); - return false; - } + if($this->class === ENTITY_FALLING){ + if($support){ + $this->level->fastSetBlockUpdate($this->x, $this->y, $this->z, $this->data["Tile"], 0); + $this->close(); + return; } + $id = $this->level->level->getBlockID($this->x, $this->y, $this->z); + if($id > 0 && !StaticBlock::getIsSolid($id) && !StaticBlock::getIsLiquid($id)){ + $this->server->api->entity->drop($this, BlockAPI::getItem($this->data["Tile"], 0, 1)); + $this->close(); + return; + } + } $this->y = $ny; diff --git a/src/material/block/misc/BedBlock.php b/src/material/block/misc/BedBlock.php index 176e1dab..1a12b74a 100644 --- a/src/material/block/misc/BedBlock.php +++ b/src/material/block/misc/BedBlock.php @@ -1,6 +1,14 @@ 3, + 1 => 4, + 2 => 2, + 3 => 5, + ]; + public function __construct($type = 0){ parent::__construct(BED_BLOCK, $type, "Bed Block"); $this->isActivable = true; @@ -16,28 +24,28 @@ class BedBlock extends TransparentBlock{ return true; } - $blockNorth = $this->getSide(2); //Gets the blocks around them - $blockSouth = $this->getSide(3); - $blockEast = $this->getSide(5); - $blockWest = $this->getSide(4); - if(($this->meta & 0x08) === 0x08){ //This is the Top part of bed - $b = $this; - }else{ //Bottom Part of Bed - if($blockNorth->getID() === $this->id and ($blockNorth->meta & 0x08) === 0x08){ - $b = $blockNorth; - }elseif($blockSouth->getID() === $this->id and ($blockSouth->meta & 0x08) === 0x08){ - $b = $blockSouth; - }elseif($blockEast->getID() === $this->id and ($blockEast->meta & 0x08) === 0x08){ - $b = $blockEast; - }elseif($blockWest->getID() === $this->id and ($blockWest->meta & 0x08) === 0x08){ - $b = $blockWest; - }else{ - $pk = new ChatPacket; - $pk->message = "This bed is incomplete"; - $player->dataPacket($pk); - return true; - } + $blockNorth = $this->getSide(2); //Gets the blocks around them + $blockSouth = $this->getSide(3); + $blockEast = $this->getSide(5); + $blockWest = $this->getSide(4); + if(($this->meta & 0x08) === 0x08){ //This is the Top part of bed + $b = $this; + }else{ //Bottom Part of Bed + if($blockNorth->getID() === $this->id and ($blockNorth->meta & 0x08) === 0x08){ + $b = $blockNorth; + }elseif($blockSouth->getID() === $this->id and ($blockSouth->meta & 0x08) === 0x08){ + $b = $blockSouth; + }elseif($blockEast->getID() === $this->id and ($blockEast->meta & 0x08) === 0x08){ + $b = $blockEast; + }elseif($blockWest->getID() === $this->id and ($blockWest->meta & 0x08) === 0x08){ + $b = $blockWest; + }else{ + $pk = new ChatPacket; + $pk->message = "This bed is incomplete"; + $player->dataPacket($pk); + return true; } + } if($player->sleepOn($b) === false){ $pk = new ChatPacket; @@ -48,56 +56,61 @@ class BedBlock extends TransparentBlock{ } public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ - $down = $this->getSide(0); - if($down->isTransparent === false){ - $faces = array( - 0 => 3, - 1 => 4, - 2 => 2, - 3 => 5, - ); - $d = $player->entity->getDirection(); - $next = $this->getSide($faces[(($d + 3) % 4)]); - $downNext = $this->getSide(0); - if($next->isReplaceable === true and $downNext->isTransparent === false){ - $meta = (($d + 3) % 4) & 0x03; - $this->level->setBlock($block, BlockAPI::get($this->id, $meta), true, false, true); - $this->level->setBlock($next, BlockAPI::get($this->id, $meta | 0x08), true, false, true); - return true; - } + $down = $this->getSide(0); + if($down->isTransparent === false){ + + $d = $player->entity->getDirection(); + $next = $this->getSide(self::$faces[(($d + 3) % 4)]); + $downNext = $next->getSide(0); + if($next->isReplaceable === true and $downNext->isTransparent === false){ + $meta = (($d + 3) % 4) & 0x03; + $this->level->setBlock($block, BlockAPI::get($this->id, $meta), true, false, true); + $this->level->setBlock($next, BlockAPI::get($this->id, $meta | 0x08), true, false, true); + return true; } + } return false; } public function onBreak(Item $item, Player $player){ - $blockNorth = $this->getSide(2); //Gets the blocks around them - $blockSouth = $this->getSide(3); - $blockEast = $this->getSide(5); - $blockWest = $this->getSide(4); - - if(($this->meta & 0x08) === 0x08){ //This is the Top part of bed - if($blockNorth->getID() === $this->id and $blockNorth->meta !== 0x08){ //Checks if the block ID and meta are right - $this->level->setBlock($blockNorth, new AirBlock(), true, false, true); - }elseif($blockSouth->getID() === $this->id and $blockSouth->meta !== 0x08){ - $this->level->setBlock($blockSouth, new AirBlock(), true, false, true); - }elseif($blockEast->getID() === $this->id and $blockEast->meta !== 0x08){ - $this->level->setBlock($blockEast, new AirBlock(), true, false, true); - }elseif($blockWest->getID() === $this->id and $blockWest->meta !== 0x08){ - $this->level->setBlock($blockWest, new AirBlock(), true, false, true); - } - }else{ //Bottom Part of Bed - if($blockNorth->getID() === $this->id and ($blockNorth->meta & 0x08) === 0x08){ - $this->level->setBlock($blockNorth, new AirBlock(), true, false, true); - }elseif($blockSouth->getID() === $this->id and ($blockSouth->meta & 0x08) === 0x08){ - $this->level->setBlock($blockSouth, new AirBlock(), true, false, true); - }elseif($blockEast->getID() === $this->id and ($blockEast->meta & 0x08) === 0x08){ - $this->level->setBlock($blockEast, new AirBlock(), true, false, true); - }elseif($blockWest->getID() === $this->id and ($blockWest->meta & 0x08) === 0x08){ - $this->level->setBlock($blockWest, new AirBlock(), true, false, true); - } + $blockNorth = $this->getSide(2); //Gets the blocks around them + $blockSouth = $this->getSide(3); + $blockEast = $this->getSide(5); + $blockWest = $this->getSide(4); + + if(($this->meta & 0x08) === 0x08){ //This is the Top part of bed + switch($this->meta & 0x7){ + case 0: + if($blockNorth->id === $this->id) $this->level->setBlock($blockNorth, new AirBlock(), true, false, true); + break; + case 1: + if($blockEast->id === $this->id) $this->level->setBlock($blockEast, new AirBlock(), true, false, true); + break; + case 2: + if($blockSouth->id === $this->id) $this->level->setBlock($blockSouth, new AirBlock(), true, false, true); + break; + case 3: + if($blockWest->id === $this->id) $this->level->setBlock($blockWest, new AirBlock(), true, false, true); + break; } - $this->level->setBlock($this, new AirBlock(), true, false, true); - return true; + }else{ //Bottom Part of Bed + switch($this->meta & 0x7){ + case 0: + if($blockSouth->id === $this->id) $this->level->setBlock($blockSouth, new AirBlock(), true, false, true); + break; + case 1: + if($blockWest->id === $this->id) $this->level->setBlock($blockWest, new AirBlock(), true, false, true); + break; + case 2: + if($blockNorth->id === $this->id) $this->level->setBlock($blockNorth, new AirBlock(), true, false, true); + break; + case 3: + if($blockEast->id === $this->id) $this->level->setBlock($blockEast, new AirBlock(), true, false, true); + break; + } + } + $this->level->setBlock($this, new AirBlock(), true, false, true); + return true; } public function getDrops(Item $item, Player $player){ diff --git a/src/world/Level.php b/src/world/Level.php index dc26073d..ccf43a52 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -189,7 +189,7 @@ class Level{ } $nbt = new NBT_new(NBT_new::LITTLE_ENDIAN); - foreach($chunkTiles as $tile){ //TODO rewrite TileEntity system + foreach($chunkTiles as $tile){ //TODO rewrite TileEntity system switch($tile->class){ case TILE_SIGN: $text = $tile->getText(); @@ -380,17 +380,6 @@ class Level{ } if($this->nextSave < $now){ - foreach($this->usedChunks as $i => $c){ - if(count($c) === 0){ - unset($this->usedChunks[$i]); - $X = explode(".", $i); - $Z = array_pop($X); - $X = array_pop($X); - if(!$this->isSpawnChunk($X, $Z)){ - $this->level->unloadChunk((int) $X, (int) $Z, $this->server->saveEnabled); - } - } - } $this->save(false, false); } } @@ -464,6 +453,10 @@ class Level{ $this->freeChunk($xz[0], $xz[1], $p); $p->stopUsingChunk($xz[0], $xz[1]); } + }else{ + unset($eids[$cid]); + unset($this->usedChunks[$c][$cid]); + ConsoleAPI::debug("Unloaded $c because $cid is not Player"); } } if(count($eids) <= 0){