Dylan K. Taylor f24f2d9ca9
Hit block legacy metadata with the biggest nuke you've ever seen
This commit completely revamps the way that blocks are represented in memory at runtime.

Instead of being represented by legacy Mojang block IDs and metadata, which are dated, limited and unchangeable, we now use custom PM block IDs, which are generated from VanillaBlocks.
This means we have full control of how they are assigned, which opens the doors to finally addressing inconsistencies like glazed terracotta, stripped logs handling, etc.

To represent state, BlockDataReader and BlockDataWriter have been introduced, and are used by blocks with state information to pack said information into a binary form that can be stored on a chunk at runtime.
Conceptually it's pretty similar to legacy metadata, but the actual format shares no resemblance whatsoever to legacy metadata, and is fully controlled by PM.
This means that the 'state data' may change in serialization format at any time, so it should **NOT** be stored on disk or in a config.

In the future, this will be improved using more auto-generated code and attributes, instead of hand-baked decodeState() and encodeState(). For now, this opens the gateway to a significant expansion of features.
It's not ideal, but it's a big step forwards.
2022-06-24 23:19:37 +01:00

129 lines
5.1 KiB
PHP

<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\block;
use PHPUnit\Framework\TestCase;
use function asort;
use function file_get_contents;
use function is_array;
use function json_decode;
use function print_r;
class BlockTest extends TestCase{
/** @var BlockFactory */
private $blockFactory;
public function setUp() : void{
$this->blockFactory = new BlockFactory();
}
/**
* Test registering a block which would overwrite another block, without forcing it
*/
public function testAccidentalOverrideBlock() : void{
$block = new MyCustomBlock(new BlockIdentifier(BlockTypeIds::COBBLESTONE, BlockLegacyIds::COBBLESTONE, 0), "Cobblestone", BlockBreakInfo::instant());
$this->expectException(\InvalidArgumentException::class);
$this->blockFactory->register($block);
}
/**
* Test registering a block deliberately overwriting another block works as expected
*/
public function testDeliberateOverrideBlock() : void{
$block = new MyCustomBlock(new BlockIdentifier(BlockTypeIds::COBBLESTONE, BlockLegacyIds::COBBLESTONE, 0), "Cobblestone", BlockBreakInfo::instant());
$this->blockFactory->register($block, true);
self::assertInstanceOf(MyCustomBlock::class, $this->blockFactory->get($block->getTypeId(), 0));
}
/**
* Test registering a new block which does not yet exist
*/
public function testRegisterNewBlock() : void{
for($i = 0; $i < 256; ++$i){
if(!$this->blockFactory->isRegistered($i)){
$b = new StrangeNewBlock(new BlockIdentifier(BlockTypeIds::FIRST_UNUSED_BLOCK_ID, $i, 0), "Strange New Block", BlockBreakInfo::instant());
$this->blockFactory->register($b);
self::assertInstanceOf(StrangeNewBlock::class, $this->blockFactory->get($b->getTypeId(), 0));
return;
}
}
throw new \RuntimeException("Can't test registering new blocks because no unused spaces left");
}
/**
* Verifies that blocks with IDs smaller than 0 can't be registered
*/
public function testRegisterIdTooSmall() : void{
self::expectException(\InvalidArgumentException::class);
$this->blockFactory->register(new OutOfBoundsBlock(new BlockIdentifier(-1, -1, 0), "Out Of Bounds Block", BlockBreakInfo::instant()));
}
/**
* Test that the block factory doesn't return the same object twice - it has to clone it first
* This is necessary because the block factory currently holds lots of partially-initialized copies of block
* instances which would hold position data and other things, so it's necessary to clone them to avoid astonishing behaviour.
*/
public function testBlockFactoryClone() : void{
for($i = 0; $i < 256; ++$i){
$b1 = $this->blockFactory->get($i, 0);
$b2 = $this->blockFactory->get($i, 0);
self::assertNotSame($b1, $b2);
}
}
/**
* Test that light filters in the static arrays have valid values. Wrong values can cause lots of unpleasant bugs
* (like freezes) when doing light population.
*/
public function testLightFiltersValid() : void{
foreach($this->blockFactory->lightFilter as $id => $value){
self::assertLessThanOrEqual(15, $value, "Light filter value for $id is larger than the expected 15");
self::assertGreaterThan(0, $value, "Light filter value for $id must be larger than 0");
}
}
public function testConsistency() : void{
$list = json_decode(file_get_contents(__DIR__ . '/block_factory_consistency_check.json'), true);
if(!is_array($list)){
throw new \pocketmine\utils\AssumptionFailedError("Old table should be array{knownStates: array<string, string>, stateDataBits: int}");
}
$knownStates = $list["knownStates"];
$oldStateDataSize = $list["stateDataBits"];
self::assertSame($oldStateDataSize, Block::INTERNAL_STATE_DATA_BITS, "Changed number of state data bits - consistency check probably need regenerating");
$states = BlockFactory::getInstance()->getAllKnownStates();
foreach(BlockFactory::getInstance()->getAllKnownStates() as $stateId => $state){
self::assertArrayHasKey($stateId, $knownStates, "New block state $stateId (" . $state->getTypeId() . ":" . $state->computeStateData() . ", " . print_r($state, true) . ") - consistency check may need regenerating");
self::assertSame($knownStates[$stateId], $state->getName());
}
asort($knownStates, SORT_STRING);
foreach($knownStates as $k => $name){
self::assertArrayHasKey($k, $states, "Missing previously-known block state $k " . ($k >> Block::INTERNAL_STATE_DATA_BITS) . ":" . ($k & Block::INTERNAL_STATE_DATA_MASK) . " ($name)");
self::assertSame($name, $states[$k]->getName());
}
}
}