Skip to content

Commit

Permalink
ext-pmmpthread changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinHDev committed Jul 8, 2023
1 parent 2b99991 commit a68aaf8
Showing 1 changed file with 20 additions and 24 deletions.
44 changes: 20 additions & 24 deletions src/ColinHDev/ActualAntiXRay/tasks/ChunkRequestTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace ColinHDev\ActualAntiXRay\tasks;

use ColinHDev\ActualAntiXRay\utils\SubChunkExplorer;
use pmmp\thread\ThreadSafeArray;
use pocketmine\block\VanillaBlocks;
use pocketmine\math\Facing;
use pocketmine\math\Vector3;
Expand All @@ -28,13 +29,12 @@
use function assert;
use function is_array;
use function is_int;
use function mt_rand;

class ChunkRequestTask extends PMMPChunkRequestTask {

/** @var int[] */
private static array $replaceableBlocks = [];
/** @var int[] */
private static array $replacingBlocks = [];
private ThreadSafeArray $replaceableBlocks;
private ThreadSafeArray $replacingBlocks;

private int $worldMinY;
private int $worldMaxY;
Expand All @@ -44,24 +44,20 @@ class ChunkRequestTask extends PMMPChunkRequestTask {

public function __construct(World $world, int $chunkX, int $chunkZ, Chunk $chunk, CompressBatchPromise $promise, Compressor $compressor, ?\Closure $onError = null) {
parent::__construct($chunkX, $chunkZ, $chunk, $promise, $compressor, $onError);
if (empty(self::$replaceableBlocks)) {
self::$replaceableBlocks = [
VanillaBlocks::STONE()->getStateId(),
VanillaBlocks::DIRT()->getStateId(),
VanillaBlocks::GRAVEL()->getStateId()
];
}
if (empty(self::$replacingBlocks)) {
self::$replacingBlocks = [
VanillaBlocks::COAL_ORE()->getStateId(),
VanillaBlocks::IRON_ORE()->getStateId(),
VanillaBlocks::LAPIS_LAZULI_ORE()->getStateId(),
VanillaBlocks::REDSTONE_ORE()->getStateId(),
VanillaBlocks::GOLD_ORE()->getStateId(),
VanillaBlocks::DIAMOND_ORE()->getStateId(),
VanillaBlocks::EMERALD_ORE()->getStateId()
];
}
$this->replaceableBlocks = ThreadSafeArray::fromArray([
VanillaBlocks::STONE()->getStateId() => true,
VanillaBlocks::DIRT()->getStateId() => true,
VanillaBlocks::GRAVEL()->getStateId() => true
]);
$this->replacingBlocks = ThreadSafeArray::fromArray([
VanillaBlocks::COAL_ORE()->getStateId(),
VanillaBlocks::IRON_ORE()->getStateId(),
VanillaBlocks::LAPIS_LAZULI_ORE()->getStateId(),
VanillaBlocks::REDSTONE_ORE()->getStateId(),
VanillaBlocks::GOLD_ORE()->getStateId(),
VanillaBlocks::DIAMOND_ORE()->getStateId(),
VanillaBlocks::EMERALD_ORE()->getStateId()
]);

$this->worldMinY = $world->getMinY();
$this->worldMaxY = $world->getMaxY();
Expand Down Expand Up @@ -152,7 +148,7 @@ public function onRun() : void {
}
}

$randomBlockId = self::$replacingBlocks[array_rand(self::$replacingBlocks)];
$randomBlockId = $this->replacingBlocks[mt_rand(0, count($this->replacingBlocks) - 1)];
assert($explorer->currentSubChunk instanceof SubChunk);
$explorer->currentSubChunk->setBlockStateId($x, $y, $z, $randomBlockId);
}
Expand Down Expand Up @@ -207,7 +203,7 @@ private function isBlockReplaceable(SubChunkExplorer $explorer, Vector3 $vector,

$explorer->moveToChunk($chunkX, $subChunkY, $chunkZ);
if ($explorer->currentSubChunk instanceof SubChunk) {
return in_array($explorer->currentSubChunk->getBlockStateId($x, $y, $z), self::$replaceableBlocks, true);
return isset($this->replaceableBlocks[$explorer->currentSubChunk->getBlockStateId($x, $y, $z)]);
}
return false;
}
Expand Down

0 comments on commit a68aaf8

Please sign in to comment.