-
-
Save Frago9876543210/f1a79cfc09405ec06234f29e67ec0dfc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** @noinspection PhpInternalEntityUsedInspection */ | |
declare(strict_types=1); | |
namespace VC\regions; | |
use Generator; | |
use pocketmine\block\BlockLegacyIds; | |
use pocketmine\math\Vector3; | |
use pocketmine\nbt\tag\CompoundTag; | |
use pocketmine\nbt\TreeRoot; | |
use pocketmine\network\mcpe\protocol\{BlockActorDataPacket, types\RuntimeBlockMapping, UpdateBlockPacket}; | |
use pocketmine\network\mcpe\serializer\NetworkNbtSerializer; | |
use pocketmine\player\Player; | |
class RegionVisualizer{ | |
/** @var NetworkNbtSerializer|null */ | |
private static $nbtSerializer; | |
public static function getRegionSides(Vector3 $pos1, Vector3 $pos2) : Generator{ | |
yield $pos1; | |
yield $pos2; | |
yield new Vector3($pos1->x, $pos2->y, $pos1->z); | |
yield new Vector3($pos2->x, $pos1->y, $pos2->z); | |
yield new Vector3($pos2->x, $pos1->y, $pos1->z); | |
yield new Vector3($pos2->x, $pos2->y, $pos1->z); | |
yield new Vector3($pos1->x, $pos1->y, $pos2->z); | |
yield new Vector3($pos1->x, $pos2->y, $pos2->z); | |
} | |
public static function getSidesOffsets(Vector3 $diff) : Generator{ | |
yield new Vector3(0, 0, 0); | |
yield $diff; | |
yield new Vector3(0, $diff->y, 0); | |
yield new Vector3($diff->x, 0, $diff->z); | |
yield new Vector3($diff->x, 0, 0); | |
yield new Vector3($diff->x, $diff->y, 0); | |
yield new Vector3(0, 0, $diff->z); | |
yield new Vector3(0, $diff->y, $diff->z); | |
} | |
public static function visualize(Player $player, Vector3 $pos1, Vector3 $pos2) : void{ | |
$session = $player->getNetworkSession(); | |
if(self::$nbtSerializer === null){ | |
self::$nbtSerializer = new NetworkNbtSerializer(); | |
} | |
$sides = self::getRegionSides($pos1, $pos2); | |
$offsets = self::getSidesOffsets($pos2->subtract($pos1)->multiply(-1)->floor()); | |
$blockRuntimeId = RuntimeBlockMapping::toStaticRuntimeId(BlockLegacyIds::STRUCTURE_BLOCK); | |
while($sides->valid() && $offsets->valid()){ | |
$side = $sides->current(); | |
$offset = $offsets->current(); | |
$session->sendDataPacket(UpdateBlockPacket::create($side->x, $side->y, $side->z, $blockRuntimeId)); | |
$session->sendDataPacket(BlockActorDataPacket::create($side->x, $side->y, $side->z, self::$nbtSerializer->write(new TreeRoot( | |
CompoundTag::create() | |
->setInt("data", 5) | |
->setString("dataField", "") | |
->setString("id", "StructureBlock") | |
->setByte("ignoreEntities", 0) | |
->setByte("includePlayers", 0) | |
->setFloat("integrity", 100.0) | |
->setByte("isMovable", 1) | |
->setByte("isPowered", 0) | |
->setByte("mirror", 0) | |
->setByte("removeBlocks", 0) | |
->setByte("rotation", 0) | |
->setLong("seed", 0) | |
->setByte("showBoundingBox", 1) | |
->setString("structureName", "") | |
->setInt("x", $side->x) | |
->setInt("xStructureOffset", $offset->x) | |
->setInt("xStructureSize", $pos2->x - $pos1->x + 1) | |
->setInt("y", $side->y) | |
->setInt("yStructureOffset", $offset->y) | |
->setInt("yStructureSize", $pos2->y - $pos1->y + 1) | |
->setInt("z", $side->z) | |
->setInt("zStructureOffset", $offset->z) | |
->setInt("zStructureSize", $pos2->z - $pos1->z + 1) | |
)))); | |
$sides->next(); | |
$offsets->next(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment