Skip to content

Commit

Permalink
add deleting block entities
Browse files Browse the repository at this point in the history
  • Loading branch information
rico132 committed Oct 4, 2022
1 parent a8564d4 commit 43810b5
Show file tree
Hide file tree
Showing 18 changed files with 718 additions and 189 deletions.
114 changes: 114 additions & 0 deletions examples/deleteBlockEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

require_once __DIR__ . "/../vendor/autoload.php";

use Aternos\Hawk\File;
use Aternos\Hawk\Hawk;
use Aternos\Hawk\McCoordinatesFloat;
use Aternos\Hawk\Region;


if (!isset($argc)) {
echo "argc and argv disabled. check php.ini " . PHP_EOL;
return;
}

$yes = ["Y", "y", false];
$no = ["N", "n"];

switch ($argc) {
case 1:
case 2:
case 3:
echo "too few params given.
\targument 1: path of world folder " . PHP_EOL . "
\targument 2: name of block entity e.g. 'minecraft:chest' " . PHP_EOL . "
\targument 3: block entity coordinates(x,y,z) " . PHP_EOL;
return;

case 4:
if (!file_exists($argv[1])) {
echo "directory does not exist. " . PHP_EOL;
return;
}
break;

default:
echo "too many params given. choose: " . PHP_EOL . "
\targument 1: path of world folder " . PHP_EOL . "
\targument 2: name of block entity e.g. 'minecraft:chest' " . PHP_EOL . "
\targument 3: block entity coordinates(x,y,z) " . PHP_EOL;
return;
}

$inputPath = $argv[1];
$entityName = $argv[2];
$coordinates = explode(",", $argv[3]);
if (count($coordinates) !== 3) {
echo "Wrong coordinates " . PHP_EOL;
return;
}
$entityPos = new McCoordinatesFloat($coordinates[0], $coordinates[1], $coordinates[2]);

$fileName = Region::getRegionFileNameFromBlock(McCoordinatesFloat::get3DCoordinates($entityPos));
$blockPath = $inputPath . "/region/" . $fileName;

$blockFiles = (file_exists($blockPath)) ? [new File($blockPath)] : [];

$hawk = new Hawk(blockFiles: $blockFiles);
$entities = $hawk->getBlockEntities($entityName, $entityPos);
switch (count($entities)) {
case 0:
throw new Exception("How did you get here?");
case 1:
$hawk->deleteBlockEntity($entities[0]);
echo "1 entity deleted " . PHP_EOL;
save();
return;
default:
$count = count($entities);
foreach ($entities as $index => $entity) {
echo $index . ": " . $entity->getName() . " at " . $entity->getCoordinates() . " " . PHP_EOL;
}
echo $count . ": delete all " . PHP_EOL;
$answer = "";
while (true) {
$answer = readline("Choose which entity will be deleted. -1 will cancel. " . PHP_EOL);
if (!is_numeric($answer) || intval($answer) > $count) {
echo "Wrong input try again. " . PHP_EOL;
continue;
}
switch (intval($answer)) {
case -1:
echo "Exiting... " . PHP_EOL;
closeFiles();
return;
case $count:
foreach ($entities as $entity) {
$hawk->deleteBlockEntity($entity);
}
save();
return;
default:
$hawk->deleteBlockEntity($entities[intval($answer)]);
save();
return;
}

}
}

function save(): void
{
global $hawk;
$hawk->save();
closeFiles();
}

function closeFiles(): void
{
global $blockFiles;
foreach ($blockFiles as $file) {
$file->close();
}
}
66 changes: 66 additions & 0 deletions examples/getAllBlockEntitiesInChunk.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

require_once __DIR__ . "/../vendor/autoload.php";

use Aternos\Hawk\Chunk;
use Aternos\Hawk\File;
use Aternos\Hawk\Hawk;
use Aternos\Hawk\McCoordinates3D;
use Aternos\Hawk\Region;


if (!isset($argc)) {
echo "argc and argv disabled. check php.ini " . PHP_EOL;
return;
}

$yes = ["Y", "y", false];
$no = ["N", "n"];

switch ($argc) {
case 1:
case 2:
echo "too few params given.
\targument 1: path of world folder " . PHP_EOL . "
\targument 2: block coordinates(x,y,z) " . PHP_EOL;
return;

case 3:
if (!file_exists($argv[1])) {
echo "$argv[1] does not exist. " . PHP_EOL;
return;
}
break;

default:
echo "too many params given. choose: " . PHP_EOL . "
\targument 1: path of word folder " . PHP_EOL . "
\targument 2: block coordinates(x,y,z) " . PHP_EOL;
return;
}

$inputPath = $argv[1];
$coordinates = explode(",", $argv[2]);
if(count($coordinates) !== 3){
echo "Wrong coordinates " . PHP_EOL;
return;
}
$blockPos = new McCoordinates3D($coordinates[0], $coordinates[1], $coordinates[2]);

$fileName = Region::getRegionFileNameFromBlock($blockPos);
$blockPath = $inputPath . "/region/" . $fileName;

$blockFiles = (file_exists($blockPath)) ? [new File($blockPath)] : [];

$hawk = new Hawk(blockFiles: $blockFiles);
$entities = $hawk->getAllBlockEntitiesFromChunk($blockPos);
foreach ($entities as $entity){
echo $entity . " " . PHP_EOL;
}
echo count($entities) . " entities in chunk " . Chunk::getChunkCoordinatesFromBlock($blockPos) . " " . PHP_EOL;

// Close files

foreach ($blockFiles as $file){
$file->close();
}
76 changes: 76 additions & 0 deletions examples/getBlockEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

require_once __DIR__ . "/../vendor/autoload.php";

use Aternos\Hawk\File;
use Aternos\Hawk\Hawk;
use Aternos\Hawk\McCoordinatesFloat;
use Aternos\Hawk\Region;


if (!isset($argc)) {
echo "argc and argv disabled. check php.ini " . PHP_EOL;
return;
}

$yes = ["Y", "y", false];
$no = ["N", "n"];

switch ($argc) {
case 1:
case 2:
case 3:
echo "too few params given.
\targument 1: path of world folder " . PHP_EOL . "
\targument 2: name of block entity e.g. 'minecraft:chest' " . PHP_EOL . "
\targument 3: block entity coordinates(x,y,z) " . PHP_EOL;
return;

case 4:
if (!file_exists($argv[1])) {
echo "directory does not exist. " . PHP_EOL;
return;
}
break;

default:
echo "too many params given.
\targument 1: path of world folder " . PHP_EOL . "
\targument 2: name of block entity e.g. 'minecraft:chest' " . PHP_EOL . "
\targument 3: block entity coordinates(x,y,z) " . PHP_EOL;
return;
}

$inputPath = $argv[1];
$entityName = $argv[2];
$coordinates = explode(",", $argv[3]);
if(count($coordinates) !== 3){
echo "Wrong coordinates " . PHP_EOL;
return;
}
$entityPos = new McCoordinatesFloat($coordinates[0], $coordinates[1], $coordinates[2]);

$fileName = Region::getRegionFileNameFromBlock(McCoordinatesFloat::get3DCoordinates($entityPos));
$blockPath = $inputPath . "/region/" . $fileName;

$blockFiles = (file_exists($blockPath)) ? [new File($blockPath)] : [];

$hawk = new Hawk(blockFiles: $blockFiles);
$entities = $hawk->getBlockEntities($entityName,$entityPos);

foreach ($entities as $index => $entity) {
echo $index . ": " . $entity->getName() . " at " . $entity->getCoordinates() . " " . PHP_EOL;
}

$count = count($entities);
$message = " entity found " . PHP_EOL;
if($count !== 1){
$message = " entities found " . PHP_EOL;
}
echo $count . $message;

// Close files

foreach ($blockFiles as $file){
$file->close();
}
32 changes: 23 additions & 9 deletions src/BlockChunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function loadSections(): void
if ($sectionsTag !== null) {
foreach ($sectionsTag as $sectionTag) {
$section = $this->newSectionFromTag($sectionTag, $this->coordinates, $this->version);
if($section !== null){
if ($section !== null) {
$this->sections[] = $section;
}
}
Expand Down Expand Up @@ -135,19 +135,20 @@ public function replaceBlock(McCoordinates3D $coordinates, string $blockName = "
$section = $this->addEmptySection($coordinates);
}
$section->replaceBlock($coordinates, $blockName);
$this->deleteBlockEntity($coordinates);
$blockEntities = $this->getBlockEntities($blockName, $coordinates);
foreach ($blockEntities as $blockEntity) {
$this->deleteBlockEntity($blockEntity);
}
}

/**
* Deletes block entity
*
* @param McCoordinates3D $coordinates
* @param BlockEntity $entity
* @return void
*/
public function deleteBlockEntity(McCoordinates3D $coordinates): void
public function deleteBlockEntity(BlockEntity $entity): void
{
foreach ($this->blockEntities as $index => $blockEntity) {
if ($coordinates->equals($blockEntity->getCoordinates())) {
if ($blockEntity === $entity) {
unset($this->blockEntities[$index]);
return;
}
Expand Down Expand Up @@ -187,7 +188,19 @@ public function replaceTags(): void
/**
* @return BlockEntity[]
*/
public function getBlockEntities(): array
public function getBlockEntities(string $name, McCoordinates $coordinates): array
{
$blockEntities = [];
foreach ($this->blockEntities as $blockEntity) {
$pos = $blockEntity->getCoordinates()->equals($coordinates);
if ($blockEntity->getName() === $name && $pos) {
$blockEntities[] = $blockEntity;
}
}
return $blockEntities;
}

public function getAllBlockEntities(): array
{
return $this->blockEntities;
}
Expand Down Expand Up @@ -217,7 +230,7 @@ public function setTags(): void
$this->tag->set($this->sectionsTagName, $this->createSectionsTag());
$this->tag->set($this->blockEntitiesTagName, $this->createBlockEntitiesTag());
$this->tag->set($this->entitiesTagName, $this->createEntitiesTag());
if($this->hasLevelTag){
if ($this->hasLevelTag) {
$tag = new CompoundTag();
$tag->set("Level", $this->tag);
$tag->set("DataVersion", (new IntTag())->setValue($this->version));
Expand All @@ -240,6 +253,7 @@ protected function createBlockEntitiesTag(): ListTag
}
return $blockEntities;
}

/**
* Creates "entities" tag
*
Expand Down
19 changes: 19 additions & 0 deletions src/BlockEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class BlockEntity
{
protected McCoordinates3D $coordinates;

protected string $name;

protected CompoundTag $tag;

/**
Expand All @@ -23,6 +25,7 @@ public function __construct(Tag $tag)
}
$this->coordinates = new McCoordinates3D($tag->getInt("x")->getValue(),$tag->getInt("y")->getValue(),$tag->getInt("z")->getValue());
$this->tag = $tag;
$this->name = $this->tag["id"]->getValue();
}

/**
Expand All @@ -33,11 +36,27 @@ public function getCoordinates(): McCoordinates3D
return $this->coordinates;
}

/**
* @return string
*/
public function getName(): string
{
return $this->name;
}

/**
* @return CompoundTag|ListTag
*/
public function createTag(): CompoundTag|ListTag
{
return $this->tag;
}

/**
* @return string
*/
public function __toString(): string
{
return $this->name . " at: " . $this->coordinates;
}
}
Loading

0 comments on commit 43810b5

Please sign in to comment.