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 1369c044..9609d4c0 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/world/Level.php b/src/world/Level.php index a8707245..524af4f2 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){