Skip to content

Commit

Permalink
Merge pull request #3 from aternosorg/FileStream
Browse files Browse the repository at this point in the history
fix getContent(); update README
  • Loading branch information
rico132 authored Jul 26, 2022
2 parents aa75aec + c0f2be0 commit f4e0464
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 21 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ How to use setContent() to set up a file stream and getContent() to get the cont
```php
$file = new File();
$file->setContent(file_get_contents("your/region/file"));
$file->setFileName("your/file/name");
"...do stuff here..."
$contentToBeWritten = $file->getContent();
```
Expand Down
12 changes: 9 additions & 3 deletions src/AbstractFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,19 @@ public function setContent(string $content): void
{
$this->fileStream = fopen("php://memory", "r+");
fputs($this->fileStream, $content);
fseek($this->fileStream,0,SEEK_SET);
}

public function getContent(): string
{
$content = fgets($this->fileStream);
if ($content === false) {
throw new Exception("Error while getting content");
fseek($this->fileStream,0,SEEK_SET);
$content = "";
while (!feof($this->fileStream)){
$currentContent = fgets($this->fileStream);
if ($currentContent === false) {
throw new Exception("Error while getting content");
}
$content .= $currentContent;
}
return $content;
}
Expand Down
6 changes: 2 additions & 4 deletions src/Chunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ public function readEntitiesTag(): ?ListTag
* @param string $name
* @param McCoordinatesFloat $coordinates
* @return Entity[]
* @throws Exception
*/
public function getEntities(string $name, McCoordinatesFloat $coordinates): array
{
Expand All @@ -233,7 +232,6 @@ public function getEntities(string $name, McCoordinatesFloat $coordinates): arra
$entities = [];
$found = false;
$results = [];
$exception = new Exception("Entity not found.");

// Filters entities by name for the while loop
foreach ($this->entities as $entity) {
Expand All @@ -250,7 +248,7 @@ public function getEntities(string $name, McCoordinatesFloat $coordinates): arra
return $results;
}
if (count($entities) === 0) {
throw $exception;
return [];
}
// Increases delta up to 13 times
while ($counter < 13) {
Expand All @@ -267,7 +265,7 @@ public function getEntities(string $name, McCoordinatesFloat $coordinates): arra
$delta = $delta * 10;
$counter++;
}
throw $exception;
return [];
}

/**
Expand Down
25 changes: 25 additions & 0 deletions src/Exceptions/VersionNotSupportedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Aternos\Hawk\Exceptions;

class VersionNotSupportedException extends \Exception
{
protected string $version;

/**
* @param string $version
*/
public function __construct(string $version)
{
$this->version = $version;
parent::__construct("Version " . $version . " is not supported.");
}

/**
* @return string
*/
public function getVersion(): string
{
return $this->version;
}
}
11 changes: 6 additions & 5 deletions src/VersionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Aternos\Hawk;


use Aternos\Hawk\Exceptions\VersionNotSupportedException;
use Aternos\Hawk\Versions\v2566\BlockChunkV2566;
use Aternos\Hawk\Versions\v2567\BlockChunkV2567;
use Aternos\Hawk\Versions\v2578\BlockChunkV2578;
Expand Down Expand Up @@ -116,8 +117,8 @@ class VersionHelper
*/
protected static function versionSupported(int $version): void
{
if (!array_key_exists($version, self::VERSIONS)) {
throw new Exception("Version " . $version . " is not supported.");
if (!array_key_exists($version, static::VERSIONS)) {
throw new VersionNotSupportedException(static::VERSIONS[$version]["name"]);
}
}

Expand All @@ -129,7 +130,7 @@ protected static function versionSupported(int $version): void
public static function getChunkClassFromVersion(int $version): string
{
self::versionSupported($version);
return self::VERSIONS[$version]["class"];
return static::VERSIONS[$version]["class"];
}

/**
Expand All @@ -140,7 +141,7 @@ public static function getChunkClassFromVersion(int $version): string
public static function hasLevelTag(int $version): bool
{
self::versionSupported($version);
return self::VERSIONS[$version]["level"];
return static::VERSIONS[$version]["level"];
}

/**
Expand All @@ -151,6 +152,6 @@ public static function hasLevelTag(int $version): bool
public static function hasEntitiesTag(int $version): bool
{
self::versionSupported($version);
return self::VERSIONS[$version]["entities"];
return static::VERSIONS[$version]["entities"];
}
}
10 changes: 4 additions & 6 deletions tests/integration/HawkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,8 @@ public function testDeleteEntity(array $blockFiles, array $entitiesFiles): void
$hawk->save();

$hawk = new Hawk(blockFiles: $blockFiles, entitiesFiles: $entitiesFiles);
$this->expectException(Exception::class);
$this->expectExceptionMessage("Entity not found.");
$hawk->getEntities("minecraft:chicken", $this->getEntityCoords());
$entities = $hawk->getEntities("minecraft:chicken", $this->getEntityCoords());
$this->assertEmpty($entities);
}

/**
Expand All @@ -314,8 +313,7 @@ public function testDeleteNegativeEntity(array $blockFiles, array $entitiesFiles
$hawk->save();

$hawk = new Hawk(blockFiles: $blockFiles, entitiesFiles: $entitiesFiles);
$this->expectException(Exception::class);
$this->expectExceptionMessage("Entity not found.");
$hawk->getEntities("minecraft:chicken", $this->getNegativeEntityCoords());
$entities = $hawk->getEntities("minecraft:chicken", $this->getNegativeEntityCoords());
$this->assertEmpty($entities);
}
}
4 changes: 1 addition & 3 deletions tests/unit/HawkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,9 @@ public function testDeleteEntity(array $blockFiles, array $entitiesFiles): void
{
$hawk = new Hawk($blockFiles, $entitiesFiles);
$entities = $hawk->getEntities("minecraft:chicken", $this->getEntityCoords());
$count = count($entities);
$hawk->deleteEntity($entities[0]);

$this->expectException(Exception::class);
$this->expectExceptionMessage("Entity not found.");
$entities = $hawk->getEntities("minecraft:chicken", $this->getEntityCoords());
$this->assertEmpty($entities);
}
}

0 comments on commit f4e0464

Please sign in to comment.