Skip to content

Commit

Permalink
Merge pull request #7 from aternosorg/1-12-support
Browse files Browse the repository at this point in the history
1 12 block-/entities support
  • Loading branch information
rico132 authored Jan 11, 2023
2 parents 2cda3f6 + 192d12d commit 5c680c3
Show file tree
Hide file tree
Showing 122 changed files with 535 additions and 44 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ vendor/
.idea/
tests/files/
create_versions_folder.php
src/Versions/v1139
src/Versions/v1241
src/Versions/v1343
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
Hawk is a PHP library to get and/or replace blocks and get and/or delete entities in Minecraft region files.
This allows the user to replace blocks or delete entities that will crash the server when loaded.

Currently, only the Minecraft Anvil world format (Minecraft Java Edition Version 1.16+) is supported.
Currently, following versions are supported:

1.12+ for entities
1.16+ for blocks

### Installation

Expand Down Expand Up @@ -43,7 +46,7 @@ $blockPos = new McCoordinates3D(1, 2, 3);

// Path to your region file and calculating the filename from the coordinates
$inputPath = "/your/world/region/directory";
$blockFiles[] = new File($inputPath . "/" . Region::getRegionFileNameFromBlock($blockPos);
$blockFiles[] = new File($inputPath . "/" . Region::getRegionFileNameFromBlock($blockPos));

// Instantiating Hawk only with blockFiles
$hawk = new Hawk(blockFiles: $blockFiles);
Expand All @@ -57,7 +60,7 @@ $entityPos = new McCoordinatesFloat(1.2, 2.3, 3.4);

// Path to your region file and calculating the filename from the coordinates
$inputPath = "/your/world/region/directory";
$entitiesFiles[] = new File($inputPath . "/" . Region::getRegionFileNameFromBlock(McCoordinatesFloat::get3DCoordinates($entityPos));
$entitiesFiles[] = new File($inputPath . "/" . Region::getRegionFileNameFromBlock(McCoordinatesFloat::get3DCoordinates($entityPos)));

// Instantiating Hawk only with blockFiles because entities used to be in the same file
$hawk = new Hawk(blockFiles: $entitiesFiles);
Expand All @@ -68,7 +71,7 @@ Setup for entities starting from 1.17:
```php
// Path to your entities directory and calculating the filename from the coordinates
$inputPath = "/your/world/entities/directory";
$entitiesFiles[] = new File($inputPath . "/" . Region::getRegionFileNameFromBlock(McCoordinatesFloat::get3DCoordinates($entityPos));
$entitiesFiles[] = new File($inputPath . "/" . Region::getRegionFileNameFromBlock(McCoordinatesFloat::get3DCoordinates($entityPos)));

$hawk = new Hawk(entitiesFiles: $entitiesFiles);
```
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
11 changes: 10 additions & 1 deletion src/BlockChunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Aternos\Hawk;

use Aternos\Hawk\Exceptions\VersionNotSupportedException;
use Aternos\Nbt\IO\Writer\ZLibCompressedStringWriter;
use Aternos\Nbt\NbtFormat;
use Aternos\Nbt\Tag\CompoundTag;
Expand Down Expand Up @@ -44,8 +45,10 @@ public function __construct(int $location, int $offset, int $compressedDataLengt
{
parent::__construct($location, $offset, $compressedDataLength, $compressionScheme, $tag, $coordinates, $version);
$this->version = $version;
$this->loadSections();
$this->loadBlockEntities();
if (VersionHelper::areBlocksSupported($this->version)) {
$this->loadSections();
}
}

/**
Expand Down Expand Up @@ -111,6 +114,9 @@ public function getSections(): array
*/
public function getBlock(McCoordinates3D $coordinates): DataBlock
{
if (!VersionHelper::areBlocksSupported($this->version)) {
throw new VersionNotSupportedException($this->version);
}
$section = $this->getSection($coordinates);
if ($section === null) {
throw new Exception("No such section.");
Expand All @@ -130,6 +136,9 @@ public function getBlock(McCoordinates3D $coordinates): DataBlock
*/
public function replaceBlock(McCoordinates3D $coordinates, string $blockName = "minecraft:stone"): void
{
if (!VersionHelper::areBlocksSupported($this->version)) {
throw new VersionNotSupportedException($this->version);
}
$section = $this->getSection($coordinates);
if ($section === null) {
$section = $this->addEmptySection($coordinates);
Expand Down
139 changes: 115 additions & 24 deletions src/VersionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


use Aternos\Hawk\Exceptions\VersionNotSupportedException;
use Aternos\Hawk\Versions\v1139\BlockChunkV1139;
use Aternos\Hawk\Versions\v2566\BlockChunkV2566;
use Aternos\Hawk\Versions\v2567\BlockChunkV2567;
use Aternos\Hawk\Versions\v2578\BlockChunkV2578;
Expand All @@ -20,19 +21,22 @@

class VersionHelper
{
private const VERSIONS = [
private const BLOCK_SUPPORT = 2566;
private const DATA_VERSIONS = [

3218 => [
"name" => "1.19.3",
"class" => BlockChunkV3105::class,
"level" => false,
"entities" => false,
],3120 => [
],
3120 => [
"name" => "1.19.2",
"class" => BlockChunkV3105::class,
"level" => false,
"entities" => false,
],3117 => [
],
3117 => [
"name" => "1.19.1",
"class" => BlockChunkV3105::class,
"level" => false,
Expand Down Expand Up @@ -110,11 +114,81 @@ class VersionHelper
"level" => true,
"entities" => true,
],

/* Unsupported
2230 => [
"name" => "1.15.2",
"class" => BlockChunkV1139::class,
"level" => true,
"entities" => true,
],
2227 => [
"name" => "1.15.1",
"class" => BlockChunkV1139::class,
"level" => true,
"entities" => true,
],
2225 => [
"name" => "1.15",
"class" => BlockChunkV1139::class,
"level" => true,
"entities" => true,
],
1976 => [
"name" => "1.14.4",
"class" => BlockChunkV1139::class,
"level" => true,
"entities" => true,
],
1968 => [
"name" => "1.14.3",
"class" => BlockChunkV1139::class,
"level" => true,
"entities" => true,
],
1963 => [
"name" => "1.14.2",
"class" => BlockChunkV1139::class,
"level" => true,
"entities" => true,
],
1957 => [
"name" => "1.14.1",
"class" => BlockChunkV1139::class,
"level" => true,
"entities" => true,
],
1952 => [
"name" => "1.14",
"class" => BlockChunkV1139::class,
"level" => true,
"entities" => true,
],
1631 => [
"name" => "1.13.2",
"class" => BlockChunkV1139::class,
"level" => true,
"entities" => true,
],
1628 => [
"name" => "1.13.1",
"class" => BlockChunkV1139::class,
"level" => true,
"entities" => true,
],
1519 => [
"name" => "1.13",
"class" => BlockChunkV1139::class,
"level" => true,
"entities" => true,
],
1343 => [
"name" => "1.12.2",
"class" => BlockChunkV1343::class,
"class" => BlockChunkV1139::class,
"level" => true,
"entities" => true,
],
1241 => [
"name" => "1.12.1",
"class" => BlockChunkV1139::class,
"level" => true,
"entities" => true,
],
Expand All @@ -123,51 +197,68 @@ class VersionHelper
"class" => BlockChunkV1139::class,
"level" => true,
"entities" => true,
],*/
],
];

/**
* @param int $version
* @return void
* @param int $dataVersion
* @return int
* @throws Exception
*/
protected static function versionSupported(int $version): void
protected static function getSupportedVersion(int $dataVersion): int
{
if (array_key_exists($dataVersion, static::DATA_VERSIONS)) {
return $dataVersion;
}
$latest = max(array_keys(static::DATA_VERSIONS));
if ($dataVersion > $latest) {
return $latest;
}
throw new VersionNotSupportedException(static::DATA_VERSIONS[$dataVersion]["name"]);
}

/**
* @param int $dataVersion
* @return bool
*/
public static function areBlocksSupported(int $dataVersion): bool
{
if (!array_key_exists($version, static::VERSIONS)) {
throw new VersionNotSupportedException(static::VERSIONS[$version]["name"]);
if ($dataVersion >= self::BLOCK_SUPPORT) {
return true;
}
return false;
}

/**
* @param int $version
* @param int $dataVersion
* @return string
* @throws Exception
*/
public static function getChunkClassFromVersion(int $version): string
public static function getChunkClassFromVersion(int $dataVersion): string
{
self::versionSupported($version);
return static::VERSIONS[$version]["class"];
$dataVersion = self::getSupportedVersion($dataVersion);
return static::DATA_VERSIONS[$dataVersion]["class"];
}

/**
* @param int $version
* @param int $dataVersion
* @return bool
* @throws Exception
*/
public static function hasLevelTag(int $version): bool
public static function hasLevelTag(int $dataVersion): bool
{
self::versionSupported($version);
return static::VERSIONS[$version]["level"];
$dataVersion = self::getSupportedVersion($dataVersion);
return static::DATA_VERSIONS[$dataVersion]["level"];
}

/**
* @param int $version
* @param int $dataVersion
* @return bool
* @throws Exception
*/
public static function hasEntitiesTag(int $version): bool
public static function hasEntitiesTag(int $dataVersion): bool
{
self::versionSupported($version);
return static::VERSIONS[$version]["entities"];
$dataVersion = self::getSupportedVersion($dataVersion);
return static::DATA_VERSIONS[$dataVersion]["entities"];
}
}
35 changes: 35 additions & 0 deletions src/Versions/v1139/BlockChunkV1139.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Aternos\Hawk\Versions\v1139;

use Aternos\Hawk\BlockChunk;
use Aternos\Hawk\McCoordinates2D;
use Aternos\Hawk\McCoordinates3D;
use Aternos\Hawk\Section;
use Aternos\Nbt\Tag\CompoundTag;
use Aternos\Nbt\Tag\Tag;
use Exception;

class BlockChunkV1139 extends BlockChunk
{
/**
* @param CompoundTag $tag
* @param McCoordinates2D $coordinates
* @param int $version
* @return Section|null
*/
public function newSectionFromTag(CompoundTag $tag, McCoordinates2D $coordinates, int $version): ?Section
{
return SectionV1139::newFromTag($tag, $coordinates, $version);
}

/**
* @param McCoordinates3D $coordinates
* @param int $version
* @return Section
*/
public function newEmptySection(McCoordinates3D $coordinates, int $version): Section
{
return SectionV1139::newEmpty($coordinates, $version);
}
}
Loading

0 comments on commit 5c680c3

Please sign in to comment.