├── resources └── config.yml ├── .poggit.yml ├── src └── muqsit │ └── worldstyler │ ├── executors │ ├── MissingOptionException.php │ ├── PosCommandExecutor.php │ ├── CopyCommandExecutor.php │ ├── PasteCommandExecutor.php │ ├── SetCommandExecutor.php │ ├── StackCommandExecutor.php │ ├── ReplaceCommandExecutor.php │ ├── BaseCommandExecutor.php │ └── SchemCommandExecutor.php │ ├── EventHandler.php │ ├── utils │ ├── BlockIterator.php │ └── Utils.php │ ├── shapes │ ├── async │ │ ├── tasks │ │ │ ├── AsyncCuboidSetTask.php │ │ │ ├── AsyncCuboidTask.php │ │ │ ├── AsyncCommonShapeTask.php │ │ │ ├── AsyncCuboidReplaceTask.php │ │ │ ├── AsyncCommonShapePasteTask.php │ │ │ ├── AsyncCommonShapeStackTask.php │ │ │ ├── AsyncCuboidCopyTask.php │ │ │ └── AsyncChunksChangeTask.php │ │ ├── AsyncCuboid.php │ │ └── AsyncCommonShape.php │ ├── CommonShape.php │ └── Cuboid.php │ ├── schematics │ ├── async │ │ ├── AsyncSchematic.php │ │ └── tasks │ │ │ └── AsyncSchematicPasteTask.php │ └── Schematic.php │ ├── Selection.php │ └── WorldStyler.php ├── README.md ├── plugin.yml └── LICENSE /resources/config.yml: -------------------------------------------------------------------------------- 1 | # Configuration for WorldStyler 2 | 3 | # Whether to execute everything asynchronously. 4 | use-async-tasks: false -------------------------------------------------------------------------------- /.poggit.yml: -------------------------------------------------------------------------------- 1 | --- # Poggit-CI Manifest. Open the CI at https://poggit.pmmp.io/ci/Muqsit/WorldStyler 2 | branches: 3 | - master 4 | projects: 5 | WorldStyler: 6 | path: "" 7 | ... 8 | -------------------------------------------------------------------------------- /src/muqsit/worldstyler/executors/MissingOptionException.php: -------------------------------------------------------------------------------- 1 | 6 | Click the badge above to redirect to WorldStyler's Poggit page where you can get the latest compiled `.phar` format of WorldStyler. 7 | 8 | ## Wiki 9 | Refer to the [WorldStyler Wiki](https://github.com/Muqsit/WorldStyler/wiki) to get help with commands and everything else. 10 | -------------------------------------------------------------------------------- /src/muqsit/worldstyler/EventHandler.php: -------------------------------------------------------------------------------- 1 | plugin = $plugin; 17 | } 18 | 19 | public function onPlayerQuit(PlayerQuitEvent $event) : void 20 | { 21 | $this->plugin->removeSelection($event->getPlayer()->getId()); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/muqsit/worldstyler/utils/BlockIterator.php: -------------------------------------------------------------------------------- 1 | currentSubChunk === null) { 17 | $this->currentSubChunk = $this->level->getChunk($this->currentX, $this->currentZ)->getSubChunk($y >> 4, $this->allocateEmptySubs); 18 | return true; 19 | } 20 | 21 | return false; 22 | } 23 | } -------------------------------------------------------------------------------- /src/muqsit/worldstyler/shapes/async/tasks/AsyncCuboidSetTask.php: -------------------------------------------------------------------------------- 1 | block = $block; 16 | } 17 | 18 | public function onRun() : void 19 | { 20 | $level = $this->getChunkManager(); 21 | $cuboid = $this->getCuboid(); 22 | 23 | $cuboid->set($level, $this->block, [$this, "updateStatistics"]); 24 | $this->saveChunks($level, $cuboid->pos1, $cuboid->pos2); 25 | } 26 | } -------------------------------------------------------------------------------- /src/muqsit/worldstyler/shapes/async/tasks/AsyncCuboidTask.php: -------------------------------------------------------------------------------- 1 | cuboid = serialize($cuboid); 18 | 19 | $this->setLevel($level); 20 | $this->setChunks($chunks); 21 | $this->setCallable($callable); 22 | } 23 | 24 | protected function getCuboid() : Cuboid 25 | { 26 | return unserialize($this->cuboid); 27 | } 28 | } -------------------------------------------------------------------------------- /src/muqsit/worldstyler/shapes/async/tasks/AsyncCommonShapeTask.php: -------------------------------------------------------------------------------- 1 | common_shape = serialize($common_shape); 18 | 19 | $this->setLevel($level); 20 | $this->setChunks($chunks); 21 | $this->setCallable($callable); 22 | } 23 | 24 | protected function getCommonShape() : CommonShape 25 | { 26 | return unserialize($this->common_shape); 27 | } 28 | } -------------------------------------------------------------------------------- /src/muqsit/worldstyler/shapes/async/tasks/AsyncCuboidReplaceTask.php: -------------------------------------------------------------------------------- 1 | find = $block; 19 | } 20 | 21 | public function replace(Block $block) : void 22 | { 23 | $this->replace = $block; 24 | } 25 | 26 | public function onRun() : void 27 | { 28 | $level = $this->getChunkManager(); 29 | $cuboid = $this->getCuboid(); 30 | 31 | $cuboid->replace($level, $this->find, $this->replace, [$this, "updateStatistics"]); 32 | $this->saveChunks($level, $cuboid->pos1, $cuboid->pos2); 33 | } 34 | } -------------------------------------------------------------------------------- /src/muqsit/worldstyler/schematics/async/AsyncSchematic.php: -------------------------------------------------------------------------------- 1 | file, $replace_pc_blocks, $callable); 22 | $task->setLevel($level); 23 | $level->getServer()->getAsyncPool()->submitTask($task); 24 | } 25 | } -------------------------------------------------------------------------------- /src/muqsit/worldstyler/executors/PosCommandExecutor.php: -------------------------------------------------------------------------------- 1 | position_index = $position_index; 22 | } 23 | 24 | public function onCommandExecute(CommandSender $sender, Command $command, string $label, array $args, array $opts) : bool 25 | { 26 | $this->plugin->getPlayerSelection($sender)->setPosition($this->position_index, $sender->asVector3()); 27 | $sender->sendMessage(TF::GREEN . 'Selected position #' . $this->position_index . ' as X=' . $sender->getFloorX() . ', Y=' . $sender->getFloorY() . ', Z=' . $sender->getFloorZ()); 28 | return true; 29 | } 30 | } -------------------------------------------------------------------------------- /src/muqsit/worldstyler/shapes/async/tasks/AsyncCommonShapePasteTask.php: -------------------------------------------------------------------------------- 1 | relative_pos = $relative_pos; 19 | } 20 | 21 | public function replaceAir(bool $replace_air) : void 22 | { 23 | $this->replace_air = $replace_air; 24 | } 25 | 26 | public function onRun() : void 27 | { 28 | $level = $this->getChunkManager(); 29 | $common_shape = $this->getCommonShape(); 30 | $common_shape->paste($level, $this->relative_pos, $this->replace_air, [$this, "updateStatistics"]); 31 | 32 | $caps = $common_shape->selection->getClipboardCaps(); 33 | $min_pos = $this->relative_pos; 34 | $max_pos = $this->relative_pos->add($caps); 35 | $this->saveChunks($level, $min_pos, $max_pos); 36 | } 37 | } -------------------------------------------------------------------------------- /plugin.yml: -------------------------------------------------------------------------------- 1 | name: WorldStyler 2 | main: muqsit\worldstyler\WorldStyler 3 | api: 3.0.0 4 | version: 1.1 5 | 6 | commands: 7 | /pos1: 8 | permission: worldstyler.selection 9 | /pos2: 10 | permission: worldstyler.selection 11 | /set: 12 | permission: worldstyler.set 13 | /replace: 14 | permission: worldstyler.replace 15 | /schem: 16 | permission: worldstyler.schem 17 | /copy: 18 | permission: worldstyler.copy 19 | /paste: 20 | permission: worldstyler.paste 21 | /stack: 22 | permission: worldstyler.stack 23 | 24 | permissions: 25 | worldstyler: 26 | default: false 27 | description: "Grants access to all WorldStyler permissions." 28 | children: 29 | worldstyler.selection: 30 | description: "Grants access to //pos1 and //pos2 commands." 31 | default: op 32 | worldstyler.set: 33 | description: "Grants access to //set command." 34 | default: op 35 | worldstyler.replace: 36 | description: "Grants access to //replace command." 37 | default: op 38 | worldstyler.schem: 39 | description: "Grants access to //schem command." 40 | default: op 41 | worldstyler.copy: 42 | description: "Grants access to //copy command." 43 | default: op 44 | worldstyler.paste: 45 | description: "Grants access to //paste command." 46 | default: op 47 | worldstyler.stack: 48 | description: "Grants access to //stack command." 49 | default: op 50 | -------------------------------------------------------------------------------- /src/muqsit/worldstyler/shapes/async/tasks/AsyncCommonShapeStackTask.php: -------------------------------------------------------------------------------- 1 | start = $pos; 25 | } 26 | 27 | public function increaseBy(Vector3 $pos) : void 28 | { 29 | $this->increase = $pos; 30 | } 31 | 32 | public function repeat(int $repetitions) : void 33 | { 34 | $this->repetitions = $repetitions; 35 | } 36 | 37 | public function replaceAir(bool $replace_air) : void 38 | { 39 | $this->replace_air = $replace_air; 40 | } 41 | 42 | public function onRun() : void 43 | { 44 | $level = $this->getChunkManager(); 45 | $common_shape = $this->getCommonShape(); 46 | $common_shape->stack($level, $this->start, $this->increase, $this->repetitions, $this->replace_air, [$this, "updateStatistics"]); 47 | 48 | $caps = $common_shape->selection->getClipboardCaps(); 49 | $min_pos = $this->start->add($this->increase->x * $caps->x, 0, $this->increase->z * $caps->z); 50 | $max_pos = $min_pos->multiply($this->repetitions); 51 | $this->saveChunks($level, $min_pos, $max_pos); 52 | } 53 | } -------------------------------------------------------------------------------- /src/muqsit/worldstyler/executors/CopyCommandExecutor.php: -------------------------------------------------------------------------------- 1 | addOption("async", self::OPT_OPTIONAL, true); 17 | } 18 | 19 | public function onCommandExecute(CommandSender $sender, Command $command, string $label, array $args, array $opts) : bool 20 | { 21 | $selection = $this->plugin->getPlayerSelection($sender); 22 | $count = $selection->getPositionCount(); 23 | 24 | if ($count < 2) { 25 | $sender->sendMessage(TF::RED . 'You have not selected enough vertices.'); 26 | return false; 27 | } 28 | 29 | $cuboid = Cuboid::fromSelection($selection); 30 | $force_async = $opts["async"] ?? null; 31 | if ($force_async !== null ? ($force_async = $this->getBool($force_async)) : $this->plugin->getConfig()->get("use-async-tasks", false)) { 32 | $cuboid = $cuboid->async(); 33 | } 34 | 35 | if ($force_async !== null) { 36 | $sender->sendMessage(TF::GRAY . 'Asynchronous /' . $label . ' ' . ($force_async ? 'enabled' : 'disabled')); 37 | } 38 | 39 | $cuboid->copy( 40 | $sender->getLevel(), 41 | $sender->asVector3(), 42 | function (float $time, int $changed) use ($sender) : void { 43 | $sender->sendMessage(TF::GREEN . 'Copied ' . number_format($changed) . ' blocks in ' . number_format($time, 10) . 's into your clipboard.'); 44 | } 45 | ); 46 | return true; 47 | } 48 | } -------------------------------------------------------------------------------- /src/muqsit/worldstyler/shapes/async/tasks/AsyncCuboidCopyTask.php: -------------------------------------------------------------------------------- 1 | relative_pos = $relative_pos; 30 | } 31 | 32 | public function onRun() : void 33 | { 34 | $level = $this->getChunkManager(); 35 | $cuboid = $this->getCuboid(); 36 | 37 | $cuboid->copy($level, $this->relative_pos, [$this, "updateStatistics"]); 38 | $this->saveChunks($level, $cuboid->pos1, $cuboid->pos2); 39 | 40 | $this->clipboard = self::serialize($cuboid->selection->getClipboard()); 41 | $this->relative_pos = $cuboid->selection->getClipboardRelativePos(); 42 | $this->clipboard_caps = self::serialize($cuboid->selection->getClipboardCaps()); 43 | $this->selectionId = $cuboid->selection->getId(); 44 | } 45 | 46 | public function onCompletion(Server $server) : void 47 | { 48 | parent::onCompletion($server); 49 | 50 | /** @var Selection $selection */ 51 | $selection = $server->getPluginManager()->getPlugin("WorldStyler")->getSelection($this->selectionId); 52 | if ($selection !== null) { 53 | $selection->setClipboard(self::unserialize($this->clipboard), $this->relative_pos, self::unserialize($this->clipboard_caps)); 54 | } 55 | } 56 | } -------------------------------------------------------------------------------- /src/muqsit/worldstyler/executors/PasteCommandExecutor.php: -------------------------------------------------------------------------------- 1 | addOption("async", self::OPT_OPTIONAL, true); 17 | } 18 | 19 | public function onCommandExecute(CommandSender $sender, Command $command, string $label, array $args, array $opts) : bool 20 | { 21 | $selection = $this->plugin->getPlayerSelection($sender); 22 | 23 | if (!$selection->hasClipboard()) { 24 | $sender->sendMessage(TF::RED . 'You have copied nothing into your clipboard.'); 25 | return false; 26 | } 27 | 28 | $air = !(isset($args[0]) && $args[0] === "noair"); 29 | 30 | $common_shape = CommonShape::fromSelection($selection); 31 | $force_async = $opts["async"] ?? null; 32 | if ($force_async !== null ? ($force_async = $this->getBool($force_async)) : $this->plugin->getConfig()->get("use-async-tasks", false)) { 33 | $cuboid = $common_shape->async(); 34 | } 35 | 36 | if ($force_async !== null) { 37 | $sender->sendMessage(TF::GRAY . 'Asynchronous /' . $label . ' ' . ($force_async ? 'enabled' : 'disabled')); 38 | } 39 | 40 | $common_shape->paste( 41 | $sender->getLevel(), 42 | $sender->asVector3(), 43 | $air, 44 | function (float $time, int $changed) use ($sender, $air) : void { 45 | $sender->sendMessage(TF::GREEN . 'Pasted ' . number_format($changed) . ' blocks in ' . number_format($time, 10) . 's from your clipboard' . ($air ? null : ' (no-air)') . '.'); 46 | } 47 | ); 48 | return true; 49 | } 50 | } -------------------------------------------------------------------------------- /src/muqsit/worldstyler/executors/SetCommandExecutor.php: -------------------------------------------------------------------------------- 1 | addOption("async", self::OPT_OPTIONAL, true); 19 | } 20 | 21 | public function onCommandExecute(CommandSender $sender, Command $command, string $label, array $args, array $opts) : bool 22 | { 23 | $selection = $this->plugin->getPlayerSelection($sender); 24 | $count = $selection->getPositionCount(); 25 | 26 | if ($count < 2) { 27 | $sender->sendMessage(TF::RED . 'You have not selected enough vertices.'); 28 | return false; 29 | } 30 | 31 | if (!isset($args[0])) { 32 | $sender->sendMessage(TF::RED . '//set '); 33 | return false; 34 | } 35 | 36 | $block = Utils::getBlockFromString($args[0]); 37 | if ($block === null) { 38 | $sender->sendMessage(TF::RED . 'Invalid block given.'); 39 | return false; 40 | } 41 | 42 | $cuboid = Cuboid::fromSelection($selection); 43 | $force_async = $opts["async"] ?? null; 44 | if ($force_async !== null ? ($force_async = $this->getBool($force_async)) : $this->plugin->getConfig()->get("use-async-tasks", false)) { 45 | $cuboid = $cuboid->async(); 46 | } 47 | 48 | if ($force_async !== null) { 49 | $sender->sendMessage(TF::GRAY . 'Asynchronous /' . $label . ' ' . ($force_async ? 'enabled' : 'disabled')); 50 | } 51 | 52 | $cuboid->set( 53 | $sender->getLevel(), 54 | $block, 55 | function (float $time, int $changed) use ($sender) : void { 56 | $sender->sendMessage(TF::GREEN . 'Set ' . number_format($changed) . ' blocks in ' . number_format($time, 10) . 's'); 57 | } 58 | ); 59 | return true; 60 | } 61 | } -------------------------------------------------------------------------------- /src/muqsit/worldstyler/Selection.php: -------------------------------------------------------------------------------- 1 | id = $id; 29 | } 30 | 31 | public function getId() : int 32 | { 33 | return $this->id; 34 | } 35 | 36 | public function setClipboard(array $blockArray, Vector3 $relative_pos, Vector3 $caps) : void 37 | { 38 | $this->clipboard = empty($blockArray) ? null : $blockArray; 39 | $this->clipboard_relative_pos = $relative_pos; 40 | $this->clipboard_caps = $caps; 41 | } 42 | 43 | public function getClipboard() : ?array 44 | { 45 | return $this->clipboard; 46 | } 47 | 48 | public function getClipboardRelativePos() : ?Vector3 49 | { 50 | return $this->clipboard_relative_pos; 51 | } 52 | 53 | public function getClipboardCaps() : ?Vector3 54 | { 55 | return $this->clipboard_caps; 56 | } 57 | 58 | public function hasClipboard() : bool 59 | { 60 | return $this->clipboard !== null; 61 | } 62 | 63 | public function setPosition(int $index, Vector3 $pos) : void 64 | { 65 | $this->positions[$index] = $pos->floor(); 66 | } 67 | 68 | public function reset() : void 69 | { 70 | $this->resetPositions(); 71 | } 72 | 73 | public function resetPositions() : void 74 | { 75 | $this->positions = []; 76 | } 77 | 78 | public function getPosition(int $index) : ?Vector3 79 | { 80 | return $this->positions[$index] ?? null; 81 | } 82 | 83 | public function removePosition(int $index) : void 84 | { 85 | unset($this->positions[$index]); 86 | } 87 | 88 | public function getPositionCount() : int 89 | { 90 | return count($this->positions); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/muqsit/worldstyler/schematics/async/tasks/AsyncSchematicPasteTask.php: -------------------------------------------------------------------------------- 1 | relative_pos = $relative_pos; 27 | $this->file = $file; 28 | $this->replace_pc_blocks = $replace_pc_blocks; 29 | $this->setCallable($callable); 30 | } 31 | 32 | public function onRun() : void 33 | { 34 | $schematic = new Schematic($this->file); 35 | $schematic->load(); 36 | $width = $schematic->getWidth(); 37 | $length = $schematic->getLength(); 38 | $this->publishProgress([$width, $length]); 39 | 40 | while ($this->chunks === null); 41 | 42 | $level = $this->getChunkManager(); 43 | $rel_pos = $this->relative_pos; 44 | $schematic->paste($level, $rel_pos, $this->replace_pc_blocks, [$this, "updateStatistics"]); 45 | $schematic->invalidate(); 46 | 47 | $this->saveChunks($level, $rel_pos, $rel_pos->add($width, 0, $length)); 48 | } 49 | 50 | public function onProgressUpdate(Server $server, $progress) : void 51 | { 52 | $level = $server->getLevel($this->levelId); 53 | [$width, $length] = $progress; 54 | 55 | $chunks = []; 56 | 57 | $rel_pos = $this->relative_pos; 58 | $relx = $rel_pos->x; 59 | $rely = $rel_pos->y; 60 | $relz = $rel_pos->z; 61 | 62 | for ($x = 0; $x < $width; ++$x) { 63 | $chunkX = ($x + $relx) >> 4; 64 | for ($z = 0; $z < $length; ++$z) { 65 | $chunkZ = ($z + $relz) >> 4; 66 | $chunks[Level::chunkHash($chunkX, $chunkZ)] = $level->getChunk($chunkX, $chunkZ, true); 67 | } 68 | } 69 | 70 | $this->setChunks($chunks); 71 | } 72 | } -------------------------------------------------------------------------------- /src/muqsit/worldstyler/WorldStyler.php: -------------------------------------------------------------------------------- 1 | getServer()->getPluginManager()->registerEvents(new EventHandler($this), $this); 25 | 26 | if (!is_dir($this->getDataFolder())) { 27 | mkdir($this->getDataFolder()); 28 | } 29 | 30 | if (!is_dir($this->getDataFolder() . 'schematics/')) { 31 | mkdir($this->getDataFolder() . 'schematics/'); 32 | } 33 | 34 | $this->saveResource("config.yml"); 35 | 36 | $commands = $this->getServer()->getCommandMap(); 37 | $commands->getCommand("/copy")->setExecutor(new CopyCommandExecutor($this)); 38 | $commands->getCommand("/paste")->setExecutor(new PasteCommandExecutor($this)); 39 | $commands->getCommand("/pos1")->setExecutor(new PosCommandExecutor($this, 1)); 40 | $commands->getCommand("/pos2")->setExecutor(new PosCommandExecutor($this, 2)); 41 | $commands->getCommand("/replace")->setExecutor(new ReplaceCommandExecutor($this)); 42 | $commands->getCommand("/schem")->setExecutor(new SchemCommandExecutor($this)); 43 | $commands->getCommand("/set")->setExecutor(new SetCommandExecutor($this)); 44 | $commands->getCommand("/stack")->setExecutor(new StackCommandExecutor($this)); 45 | } 46 | 47 | public function getPlayerSelection(Player $player) : ?Selection 48 | { 49 | return $this->getSelection($player->getId()); 50 | } 51 | 52 | public function getSelection(int $pid) : ?Selection 53 | { 54 | return $this->selections[$pid] ?? ($this->selections[$pid] = new Selection($pid)); 55 | } 56 | 57 | public function removeSelection(int $pid) : void 58 | { 59 | unset($this->selections[$pid]); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/muqsit/worldstyler/executors/StackCommandExecutor.php: -------------------------------------------------------------------------------- 1 | addOption("async", self::OPT_OPTIONAL, true); 18 | } 19 | 20 | public function onCommandExecute(CommandSender $sender, Command $command, string $label, array $args, array $opts) : bool 21 | { 22 | $selection = $this->plugin->getPlayerSelection($sender); 23 | 24 | if (!$selection->hasClipboard()) { 25 | $sender->sendMessage(TF::RED . 'You have copied nothing into your clipboard.'); 26 | return false; 27 | } 28 | 29 | if (!isset($args[0]) || !is_numeric($args[0]) || strpos($args[0], '.') !== false || $args[0] <= 0) { 30 | $sender->sendMessage(TF::RED . '//stack '); 31 | return false; 32 | } 33 | 34 | $air = !(isset($args[1]) && $args[1] === "noair"); 35 | 36 | $common_shape = CommonShape::fromSelection($selection); 37 | $force_async = $opts["async"] ?? null; 38 | if ($force_async !== null ? ($force_async = $this->getBool($force_async)) : $this->plugin->getConfig()->get("use-async-tasks", false)) { 39 | $cuboid = $common_shape->async(); 40 | } 41 | 42 | if ($force_async !== null) { 43 | $sender->sendMessage(TF::GRAY . 'Asynchronous /' . $label . ' ' . ($force_async ? 'enabled' : 'disabled')); 44 | } 45 | 46 | $increase = $sender->getDirectionVector()->round(); 47 | $repititions = (int) $args[0]; 48 | 49 | $sender->sendMessage(TF::YELLOW . 'Stacking (Multiplying by ' . $increase->__toString() . ')...'); 50 | 51 | $common_shape->stack( 52 | $sender->getLevel(), 53 | $sender->asVector3(), 54 | $increase, 55 | $repititions, 56 | $air, 57 | function (float $time, int $changed) use ($sender, $air) : void { 58 | $sender->sendMessage(TF::GREEN . 'Stacked ' . number_format($changed) . ' blocks in ' . number_format($time, 10) . 's from your clipboard' . ($air ? null : ' (no-air)') . '.'); 59 | } 60 | ); 61 | return true; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/muqsit/worldstyler/executors/ReplaceCommandExecutor.php: -------------------------------------------------------------------------------- 1 | addOption("async", self::OPT_OPTIONAL, true); 18 | } 19 | 20 | public function onCommandExecute(CommandSender $sender, Command $command, string $label, array $args, array $opts) : bool 21 | { 22 | $selection = $this->plugin->getPlayerSelection($sender); 23 | 24 | $count = $selection->getPositionCount(); 25 | if ($count < 2) { 26 | $sender->sendMessage(TF::RED . 'You have not selected enough vertices.'); 27 | return false; 28 | } 29 | 30 | if (!isset($args[1])) { 31 | $sender->sendMessage(TF::RED . '//replace '); 32 | return false; 33 | } 34 | 35 | [$block1, $block2] = $args; 36 | 37 | $block1 = Utils::getBlockFromString($block1); 38 | if ($block1 === null) { 39 | $sender->sendMessage(TF::RED . 'Invalid block ' . $block1 . ' given.'); 40 | return false; 41 | } 42 | 43 | $block2 = Utils::getBlockFromString($block2); 44 | if ($block2 === null) { 45 | $sender->sendMessage(TF::RED . 'Invalid block ' . $block2 . ' given.'); 46 | return false; 47 | } 48 | 49 | $cuboid = Cuboid::fromSelection($selection); 50 | $force_async = $opts["async"] ?? null; 51 | if ($force_async !== null ? ($force_async = $this->getBool($force_async)) : $this->plugin->getConfig()->get("use-async-tasks", false)) { 52 | $cuboid = $cuboid->async(); 53 | } 54 | 55 | if ($force_async !== null) { 56 | $sender->sendMessage(TF::GRAY . 'Asynchronous /' . $label . ' ' . ($force_async ? 'enabled' : 'disabled')); 57 | } 58 | 59 | $cuboid->replace( 60 | $sender->getLevel(), 61 | $block1, 62 | $block2, 63 | function (float $time, int $changed) use ($sender) : void { 64 | $sender->sendMessage(TF::GREEN . 'Replaced ' . number_format($changed) . ' blocks in ' . number_format($time, 10) . 's'); 65 | } 66 | ); 67 | return true; 68 | } 69 | } -------------------------------------------------------------------------------- /src/muqsit/worldstyler/shapes/async/AsyncCuboid.php: -------------------------------------------------------------------------------- 1 | selection), $level, $this->getChunks($level), $callable); 25 | $task->setRelativePos($relative_pos); 26 | $level->getServer()->getAsyncPool()->submitTask($task); 27 | } 28 | 29 | public function set(ChunkManager $level, Block $block, ?callable $callable = null) : void 30 | { 31 | if (!($level instanceof Level)) { 32 | throw new \InvalidArgumentException("\$level should be an instance of " . Level::class . " in asynchronous classes, got " . get_class($level)); 33 | } 34 | 35 | $task = new AsyncCuboidSetTask(Cuboid::fromSelection($this->selection), $level, $this->getChunks($level), $callable); 36 | $task->setBlock($block); 37 | $level->getServer()->getAsyncPool()->submitTask($task); 38 | } 39 | 40 | public function replace(ChunkManager $level, Block $find, Block $replace, ?callable $callable = null) : void 41 | { 42 | if (!($level instanceof Level)) { 43 | throw new \InvalidArgumentException("\$level should be an instance of " . Level::class . " in asynchronous classes, got " . get_class($level)); 44 | } 45 | 46 | $task = new AsyncCuboidReplaceTask(Cuboid::fromSelection($this->selection), $level, $this->getChunks($level), $callable); 47 | $task->find($find); 48 | $task->replace($replace); 49 | $level->getServer()->getAsyncPool()->submitTask($task); 50 | } 51 | 52 | private function getChunks(Level $level, bool $create = true) : array 53 | { 54 | $chunks = []; 55 | 56 | $minChunkX = $this->pos1->x >> 4; 57 | $maxChunkX = $this->pos2->x >> 4; 58 | $minChunkZ = $this->pos1->z >> 4; 59 | $maxChunkZ = $this->pos2->z >> 4; 60 | 61 | for ($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX) { 62 | for ($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ) { 63 | $chunks[] = $level->getChunk($chunkX, $chunkZ, $create); 64 | } 65 | } 66 | 67 | return $chunks; 68 | } 69 | } -------------------------------------------------------------------------------- /src/muqsit/worldstyler/shapes/async/AsyncCommonShape.php: -------------------------------------------------------------------------------- 1 | selection->getClipboardCaps(); 24 | $minChunkX = ($start->x + ($increase->x * $caps->x)) >> 4; 25 | $minChunkZ = ($start->z + ($increase->z * $caps->z)) >> 4; 26 | $maxChunkX = ($start->x + ($increase->x * $caps->x * $repetitions)) >> 4; 27 | $maxChunkZ = ($start->z + ($increase->z * $caps->z * $repetitions)) >> 4; 28 | 29 | for ($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX) { 30 | for ($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ) { 31 | $chunks[] = $level->getChunk($chunkX, $chunkZ, true); 32 | } 33 | } 34 | 35 | $task = new AsyncCommonShapeStackTask(CommonShape::fromSelection($this->selection), $level, $chunks, $callable); 36 | $task->startFrom($start); 37 | $task->increaseBy($increase); 38 | $task->repeat($repetitions); 39 | $task->replaceAir($replace_air); 40 | $level->getServer()->getAsyncPool()->submitTask($task); 41 | } 42 | 43 | public function paste(ChunkManager $level, Vector3 $relative_pos, bool $replace_air = true, ?callable $callable = null) : void 44 | { 45 | if (!($level instanceof Level)) { 46 | throw new \InvalidArgumentException("\$level should be an instance of " . Level::class . " in asynchronous classes, got " . get_class($level)); 47 | } 48 | 49 | $chunks = []; 50 | 51 | $caps = $this->selection->getClipboardCaps(); 52 | $minChunkX = $relative_pos->x >> 4; 53 | $minChunkZ = $relative_pos->z >> 4; 54 | $maxChunkX = ($relative_pos->x + $caps->x) >> 4; 55 | $maxChunkZ = ($relative_pos->z + $caps->z) >> 4; 56 | 57 | for ($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX) { 58 | for ($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ) { 59 | $chunks[] = $level->getChunk($chunkX, $chunkZ, true); 60 | } 61 | } 62 | 63 | $task = new AsyncCommonShapePasteTask(CommonShape::fromSelection($this->selection), $level, $chunks, $callable); 64 | $task->setRelativePos($relative_pos); 65 | $task->replaceAir($replace_air); 66 | $level->getServer()->getAsyncPool()->submitTask($task); 67 | } 68 | } -------------------------------------------------------------------------------- /src/muqsit/worldstyler/executors/BaseCommandExecutor.php: -------------------------------------------------------------------------------- 1 | plugin = $plugin; 27 | $this->initExecutor(); 28 | } 29 | 30 | protected function initExecutor() : void 31 | { 32 | } 33 | 34 | public function addOption(string $option, int $type, bool $is_long) : void 35 | { 36 | $this->options[($is_long ? "--" : "-") . $option] = $type; 37 | } 38 | 39 | protected function parseOptions(array $args, &$indexes) : array 40 | { 41 | $options = []; 42 | $indexes = []; 43 | 44 | if (!empty($this->options)) { 45 | foreach ($args as $index => $arg) { 46 | if ($arg{0} === "-") { 47 | $offset = 0; 48 | $len = strlen($arg); 49 | while ($offset < $len) { 50 | if (isset($this->options[$opt = substr($arg, 0, $offset)])) { 51 | $indexes[$index] = $index; 52 | $value = true; 53 | $type = $this->options[$opt]; 54 | if ($type !== self::OPT_NO_VALUE) { 55 | $value = $offset === $len ? true : ltrim(substr($arg, $offset), "="); 56 | if ($value === "") { 57 | $value = true; 58 | } 59 | if ($value === true && $type === self::OPT_REQUIRED) { 60 | throw new MissingOptionException("Value of flag '{$opt}' is mandatory"); 61 | } 62 | } 63 | 64 | $options[ltrim($opt, "-")] = $value; 65 | break; 66 | } 67 | ++$offset; 68 | } 69 | } 70 | } 71 | } 72 | 73 | return $options; 74 | } 75 | 76 | protected function getBool(string $option) : bool 77 | { 78 | return $option === "true" || $option === "1"; 79 | } 80 | 81 | final public function onCommand(CommandSender $sender, Command $command, string $label, array $args) : bool 82 | { 83 | $opts = $this->parseOptions($args, $indexes); 84 | $args = array_values(array_diff_key($args, $indexes)); 85 | return $this->onCommandExecute($sender, $command, $label, $args, $opts); 86 | } 87 | 88 | abstract public function onCommandExecute(CommandSender $sender, Command $command, string $label, array $args, array $opts) : bool; 89 | } -------------------------------------------------------------------------------- /src/muqsit/worldstyler/executors/SchemCommandExecutor.php: -------------------------------------------------------------------------------- 1 | addOption("async", self::OPT_OPTIONAL, true); 19 | } 20 | 21 | public function onCommandExecute(CommandSender $sender, Command $command, string $label, array $args, array $opts) : bool 22 | { 23 | if (!isset($args[0]) || ($args[0] !== 'list' && $args[0] !== 'paste') || ($args[0] === 'paste' && !isset($args[1]))) { 24 | $sender->sendMessage(TF::RED . '//schem list'); 25 | $sender->sendMessage(TF::RED . '//schem paste '); 26 | return false; 27 | } 28 | 29 | if ($args[0] === 'list') { 30 | $dir = $this->plugin->getDataFolder() . 'schematics/'; 31 | if (!is_dir($dir)) { 32 | $sender->sendMessage(TF::RED . 'Directory ' . $dir . ' does not exist.'); 33 | return false; 34 | } 35 | 36 | $files = 0; 37 | $sender->sendMessage(TF::YELLOW . 'Schematics:'); 38 | foreach (scandir($dir) as $file) { 39 | $expl = explode(".", $file, 2); 40 | if (count($expl) === 2 && $expl[1] === 'schematic') { 41 | $sender->sendMessage(TF::GREEN . ++$files . '. ' . $expl[0] . TF::GRAY . ' (' . Utils::humanFilesize($dir . $file) . ')'); 42 | } 43 | } 44 | $sender->sendMessage(TF::ITALIC . TF::GRAY . 'Found ' . $files . ' schematics!'); 45 | return true; 46 | } 47 | 48 | if ($args[0] === 'paste') { 49 | $file = $this->plugin->getDataFolder() . 'schematics/' . $args[1] . '.schematic'; 50 | if (!is_file($file)) { 51 | $sender->sendMessage(TF::RED . 'File "' . $file . '" not found.'); 52 | return false; 53 | } 54 | 55 | $schematic = new Schematic($file); 56 | $force_async = $opts["async"] ?? null; 57 | $is_async = $force_async !== null ? ($force_async = $this->getBool($force_async)) : $this->plugin->getConfig()->get("use-async-tasks", false); 58 | 59 | if ($force_async !== null) { 60 | $sender->sendMessage(TF::GRAY . 'Asynchronous /' . $label . ' ' . ($force_async ? 'enabled' : 'disabled')); 61 | } 62 | 63 | if ($is_async) { 64 | $schematic = $schematic->async(); 65 | } else { 66 | $schematic->load(); 67 | } 68 | 69 | $schematic->paste( 70 | $sender->getLevel(), 71 | $sender->asVector3(), 72 | true, 73 | function (float $time, int $changed) use ($sender) : void { 74 | $sender->sendMessage(TF::GREEN . 'Took ' . number_format($time, 10) . 's to paste ' . number_format($changed) . ' blocks.'); 75 | } 76 | ); 77 | 78 | if (!$is_async) { 79 | $schematic->invalidate(); 80 | } 81 | } 82 | return true; 83 | } 84 | } -------------------------------------------------------------------------------- /src/muqsit/worldstyler/schematics/Schematic.php: -------------------------------------------------------------------------------- 1 | file = $file; 27 | } 28 | 29 | public function load() : void 30 | { 31 | $this->namedtag = (new BigEndianNBTStream())->readCompressed(file_get_contents($this->file)); 32 | } 33 | 34 | public function getWidth() : int 35 | { 36 | return $this->namedtag->getShort("Width"); 37 | } 38 | 39 | public function getLength() : int 40 | { 41 | return $this->namedtag->getShort("Length"); 42 | } 43 | 44 | public function getHeight() : int 45 | { 46 | return $this->namedtag->getShort("Height"); 47 | } 48 | 49 | public function paste(ChunkManager $level, Vector3 $relative_pos, bool $replace_pc_blocks = true, ?callable $callable = null) : void 50 | { 51 | $time = microtime(true); 52 | 53 | $blockIds = $this->namedtag->getByteArray("Blocks"); 54 | $blockDatas = $this->namedtag->getByteArray("Data"); 55 | 56 | $width = $this->getWidth(); 57 | $length = $this->getLength(); 58 | $height = $this->getHeight(); 59 | 60 | $relative_pos = $relative_pos->floor(); 61 | $relx = $relative_pos->x; 62 | $rely = $relative_pos->y; 63 | $relz = $relative_pos->z; 64 | 65 | $iterator = new BlockIterator($level); 66 | 67 | $wl = $width * $length; 68 | 69 | for ($x = 0; $x < $width; ++$x) { 70 | $xPos = $x + $relx; 71 | 72 | for ($z = 0; $z < $length; ++$z) { 73 | $zPos = $z + $relz; 74 | $zwx = $z * $width + $x; 75 | 76 | for ($y = 0; $y < $height; ++$y) { 77 | $index = $y * $wl + $zwx; 78 | 79 | $id = ord($blockIds{$index}); 80 | $damage = ord($blockDatas{$index}); 81 | 82 | if ($replace_pc_blocks && isset(Utils::REPLACEMENTS[$id])) { 83 | [$new_id, $new_damage] = Utils::REPLACEMENTS[$id][$damage] ?? Utils::REPLACEMENTS[$id][-1] ?? [$id, $damage]; 84 | $id = $new_id ?? $id; 85 | $damage = $new_damage ?? $damage; 86 | } 87 | 88 | $yPos = $y + $rely; 89 | $iterator->moveTo($xPos, $yPos, $zPos); 90 | $iterator->currentSubChunk->setBlock($xPos & 0x0f, $yPos & 0x0f, $zPos & 0x0f, $id, $damage); 91 | } 92 | } 93 | } 94 | 95 | if ($level instanceof Level) { 96 | Utils::updateChunks($level, $relx >> 4, ($relx + $width - 1) >> 4, $relz >> 4, ($relz + $length - 1) >> 4); 97 | } 98 | 99 | $time = microtime(true) - $time; 100 | if ($callable !== null) { 101 | $callable($time, $width * $length * $height); 102 | } 103 | } 104 | 105 | public function invalidate() : void 106 | { 107 | $this->namedtag = null; 108 | } 109 | 110 | public function async() : AsyncSchematic 111 | { 112 | return new AsyncSchematic($this->file); 113 | } 114 | } -------------------------------------------------------------------------------- /src/muqsit/worldstyler/shapes/CommonShape.php: -------------------------------------------------------------------------------- 1 | selection = $selection; 29 | } 30 | 31 | public function stack(ChunkManager $level, Vector3 $start, Vector3 $increase, int $repetitions, bool $replace_air = true, ?callable $callable = null) : void 32 | { 33 | $totalTime = 0; 34 | $changed = 0; 35 | 36 | $caps = $this->selection->getClipboardCaps(); 37 | $xCap = $caps->x; 38 | $yCap = $caps->y; 39 | $zCap = $caps->z; 40 | 41 | $xIncrease = $increase->x; 42 | $yIncrease = $increase->y; 43 | $zIncrease = $increase->z; 44 | 45 | $paste_callable = function (float $timeTaken, int $blocksChanged) use(&$totalTime, &$changed) : void { 46 | $totalTime += $timeTaken; 47 | $changed += $blocksChanged; 48 | }; 49 | 50 | while (--$repetitions >= 0) { 51 | $this->paste($level, $start, $replace_air, $paste_callable); 52 | 53 | $start->x += $xIncrease * $xCap; 54 | $start->y += $yIncrease * $yCap; 55 | $start->z += $zIncrease * $zCap; 56 | } 57 | 58 | if ($callable !== null) { 59 | $callable($totalTime, $changed); 60 | } 61 | } 62 | 63 | public function paste(ChunkManager $level, Vector3 $relative_pos, bool $replace_air = true, ?callable $callable = null) : void 64 | { 65 | $changed = 0; 66 | $time = microtime(true); 67 | 68 | $relative_pos = $relative_pos->floor()->add($this->selection->getClipboardRelativePos()); 69 | $relx = $relative_pos->x; 70 | $rely = $relative_pos->y; 71 | $relz = $relative_pos->z; 72 | 73 | $clipboard = $this->selection->getClipboard(); 74 | 75 | $caps = $this->selection->getClipboardCaps(); 76 | $xCap = $caps->x; 77 | $yCap = $caps->y; 78 | $zCap = $caps->z; 79 | 80 | $iterator = new BlockIterator($level); 81 | 82 | for ($x = 0; $x <= $xCap; ++$x) { 83 | $xPos = $relx + $x; 84 | for ($z = 0; $z <= $zCap; ++$z) { 85 | $zPos = $relz + $z; 86 | for ($y = 0; $y <= $yCap; ++$y) { 87 | $fullBlock = $clipboard[Level::blockHash($x, $y, $z)] ?? null; 88 | if ($fullBlock !== null) { 89 | if ($replace_air || ($fullBlock >> 4) !== Block::AIR) { 90 | $yPos = $rely + $y; 91 | $iterator->moveTo($xPos, $yPos, $zPos); 92 | $iterator->currentSubChunk->setBlock($xPos & 0x0f, $yPos & 0x0f, $zPos & 0x0f, $fullBlock >> 4, $fullBlock & 0xf); 93 | ++$changed; 94 | } 95 | } 96 | } 97 | } 98 | } 99 | 100 | if ($level instanceof Level) { 101 | Utils::updateChunks($level, $relx >> 4, ($relx + $xCap) >> 4, $relz >> 4, ($relz + $zCap) >> 4); 102 | } 103 | 104 | $time = microtime(true) - $time; 105 | if ($callable !== null) { 106 | $callable($time, $changed); 107 | } 108 | } 109 | 110 | public function async() : AsyncCommonShape 111 | { 112 | return new AsyncCommonShape($this->selection); 113 | } 114 | } -------------------------------------------------------------------------------- /src/muqsit/worldstyler/shapes/async/tasks/AsyncChunksChangeTask.php: -------------------------------------------------------------------------------- 1 | levelId = $level->getId(); 52 | $this->seed = $level->getSeed(); 53 | $this->worldHeight = $level->getWorldHeight(); 54 | } 55 | 56 | public function updateStatistics(float $time, int $changed) : void 57 | { 58 | $this->time = $time; 59 | $this->changed = $changed; 60 | } 61 | 62 | public function setChunks(array $chunks) : void 63 | { 64 | $serialized_chunks = []; 65 | foreach ($chunks as $chunk) { 66 | $serialized_chunks[Level::chunkHash($chunk->getX(), $chunk->getZ())] = $chunk->fastSerialize(); 67 | } 68 | 69 | $this->chunks = self::serialize($serialized_chunks); 70 | } 71 | 72 | protected function setCallable(?callable $callable) : void 73 | { 74 | if ($callable !== null) { 75 | $this->storeLocal($callable); 76 | $this->has_callable = true; 77 | } 78 | } 79 | 80 | protected function getChunkManager() : SimpleChunkManager 81 | { 82 | $manager = new SimpleChunkManager($this->seed, $this->worldHeight); 83 | 84 | foreach (self::unserialize($this->chunks) as $hash => $serialized_chunk) { 85 | Level::getXZ($hash, $chunkX, $chunkZ); 86 | $manager->setChunk($chunkX, $chunkZ, Chunk::fastDeserialize($serialized_chunk)); 87 | } 88 | 89 | return $manager; 90 | } 91 | 92 | public function saveChunks(SimpleChunkManager $level, Vector3 $pos1, Vector3 $pos2) : void 93 | { 94 | if (!$this->set_chunks) { 95 | $this->chunks = null; 96 | return; 97 | } 98 | 99 | $minChunkX = min($pos1->x, $pos2->x) >> 4; 100 | $maxChunkX = max($pos1->x, $pos2->x) >> 4; 101 | $minChunkZ = min($pos1->z, $pos2->z) >> 4; 102 | $maxChunkZ = max($pos1->z, $pos2->z) >> 4; 103 | 104 | $chunks = []; 105 | 106 | for ($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX) { 107 | for ($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ) { 108 | $chunks[Level::chunkHash($chunkX, $chunkZ)] = $level->getChunk($chunkX, $chunkZ)->fastSerialize(); 109 | } 110 | } 111 | 112 | $this->chunks = self::serialize($chunks); 113 | } 114 | 115 | public function onCompletion(Server $server) : void 116 | { 117 | if ($this->set_chunks) { 118 | $level = $server->getLevel($this->levelId); 119 | foreach (self::unserialize($this->chunks) as $hash => $chunk) { 120 | Level::getXZ($hash, $chunkX, $chunkZ); 121 | $level->setChunk($chunkX, $chunkZ, Chunk::fastDeserialize($chunk)); 122 | } 123 | } 124 | 125 | if ($this->has_callable) { 126 | $this->fetchLocal()($this->time, $this->changed); 127 | } 128 | } 129 | } -------------------------------------------------------------------------------- /src/muqsit/worldstyler/utils/Utils.php: -------------------------------------------------------------------------------- 1 | [ 15 | -1 => [Block::WOODEN_SLAB, null]//-1 = all block metas, null = dont change the block's meta. 16 | ], 17 | Block::INVISIBLE_BEDROCK => [ 18 | -1 => [Block::STAINED_GLASS, null] 19 | ], 20 | Block::DROPPER => [ 21 | -1 => [Block::DOUBLE_WOODEN_SLAB, null] 22 | ], 23 | 188 => [ 24 | -1 => [Block::FENCE, WoodenFence::FENCE_SPRUCE] 25 | ], 26 | 189 => [ 27 | -1 => [Block::FENCE, WoodenFence::FENCE_BIRCH] 28 | ], 29 | 190 => [ 30 | -1 => [Block::FENCE, WoodenFence::FENCE_JUNGLE] 31 | ], 32 | 191 => [ 33 | -1 => [Block::FENCE, WoodenFence::FENCE_DARKOAK] 34 | ], 35 | 192 => [ 36 | -1 => [Block::FENCE, WoodenFence::FENCE_ACACIA] 37 | ], 38 | Block::DOUBLE_STONE_SLAB => [ 39 | 6 => [Block::DOUBLE_STONE_SLAB, 7], 40 | 7 => [Block::DOUBLE_STONE_SLAB, 6], 41 | ], 42 | Block::STONE_SLAB => [ 43 | 6 => [Block::STONE_SLAB, 7], 44 | 7 => [Block::STONE_SLAB, 6], 45 | 15 => [Block::STONE_SLAB, 14] 46 | ], 47 | Block::TRAPDOOR => [ 48 | 0 => [null, 3], 49 | 1 => [null, 2], 50 | 2 => [null, 1], 51 | 3 => [null, 0], 52 | 4 => [null, 7], 53 | 5 => [null, 6], 54 | 6 => [null, 5], 55 | 7 => [null, 4], 56 | 8 => [null, 11], 57 | 9 => [null, 10], 58 | 10 => [null, 9], 59 | 11 => [null, 8], 60 | 12 => [null, 15], 61 | 13 => [null, 14], 62 | 14 => [null, 13], 63 | 15 => [null, 12] 64 | ], 65 | Block::IRON_TRAPDOOR => [ 66 | 0 => [null, 3], 67 | 1 => [null, 2], 68 | 2 => [null, 1], 69 | 3 => [null, 0], 70 | 4 => [null, 7], 71 | 5 => [null, 6], 72 | 6 => [null, 5], 73 | 7 => [null, 4], 74 | 8 => [null, 11], 75 | 9 => [null, 10], 76 | 10 => [null, 9], 77 | 11 => [null, 8], 78 | 12 => [null, 15], 79 | 13 => [null, 14], 80 | 14 => [null, 13], 81 | 15 => [null, 12] 82 | ], 83 | 166 => [ 84 | -1 => [Block::INVISIBLE_BEDROCK, null] 85 | ], 86 | 202 => [ 87 | -1 => [Block::PURPUR_BLOCK, null] 88 | ] 89 | ]; 90 | 91 | const FILESIZES = 'BKMGTP'; 92 | 93 | public static function humanFilesize(string $file, int $decimals = 2) : string 94 | { 95 | //from https://stackoverflow.com/questions/15188033/human-readable-file-size but customized a bit 96 | $bytes = (string) filesize($file); 97 | $factor = floor((strlen($bytes) - 1) / 3); 98 | return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . (self::FILESIZES[$factor] ?? ""); 99 | } 100 | 101 | public static function updateChunks(Level $level, int $minChunkX, int $maxChunkX, int $minChunkZ, int $maxChunkZ) : void 102 | { 103 | for ($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX) { 104 | for ($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ) { 105 | $level->clearChunkCache($chunkX, $chunkZ); 106 | foreach ($level->getChunkLoaders($chunkX, $chunkZ) as $loader) { 107 | $loader->onChunkChanged($level->getChunk($chunkX, $chunkZ)); 108 | } 109 | } 110 | } 111 | /* 112 | for ($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX) { 113 | for ($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ) { 114 | $level->setChunk($chunkX, $chunkZ, $level->getChunk($chunkX, $chunkZ), false); 115 | } 116 | } 117 | */ 118 | } 119 | 120 | public static function getBlockFromString(string $block) : ?Block 121 | { 122 | $blockdata = explode(":", $block, 2); 123 | $data = array_map("intval", $blockdata); 124 | 125 | $name = strtolower($blockdata[0]); 126 | foreach (BlockFactory::getBlockStatesArray() as $bl) { 127 | if (strtolower($bl->getName()) === $name) { 128 | return Block::get($bl->getId(), $data[1] ?? $bl->getDamage()); 129 | } 130 | } 131 | 132 | return Block::get(...$data); 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /src/muqsit/worldstyler/shapes/Cuboid.php: -------------------------------------------------------------------------------- 1 | getPosition(1), $selection->getPosition(2), $selection); 21 | } 22 | 23 | /** @var Vector3 */ 24 | public $pos1; 25 | 26 | /** @var Vector3 */ 27 | public $pos2; 28 | 29 | /** @var Selection */ 30 | public $selection; 31 | 32 | public function __construct(Vector3 $pos1, Vector3 $pos2, Selection $selection) 33 | { 34 | $minX = min($pos1->x, $pos2->x); 35 | $maxX = max($pos1->x, $pos2->x); 36 | $minY = min($pos1->y, $pos2->y); 37 | $maxY = max($pos1->y, $pos2->y); 38 | $minZ = min($pos1->z, $pos2->z); 39 | $maxZ = max($pos1->z, $pos2->z); 40 | 41 | $pos1->x = $minX; 42 | $pos1->y = $minY; 43 | $pos1->z = $minZ; 44 | 45 | $pos2->x = $maxX; 46 | $pos2->y = $maxY; 47 | $pos2->z = $maxZ; 48 | 49 | $this->pos1 = $pos1; 50 | $this->pos2 = $pos2; 51 | $this->selection = $selection; 52 | } 53 | 54 | public function copy(ChunkManager $level, Vector3 $relative_pos, ?callable $callable = null) : void 55 | { 56 | $time = microtime(true); 57 | 58 | $s_pos = $this->pos1->subtract($relative_pos->floor()); 59 | 60 | $cap = $this->pos2->subtract($this->pos1); 61 | $xCap = $cap->x; 62 | $yCap = $cap->y; 63 | $zCap = $cap->z; 64 | 65 | $minX = $this->pos1->x; 66 | $minY = $this->pos1->y; 67 | $minZ = $this->pos1->z; 68 | 69 | $blocks = []; 70 | $iterator = new BlockIterator($level); 71 | 72 | for ($x = 0; $x <= $xCap; ++$x) { 73 | $ax = $minX + $x; 74 | for ($z = 0; $z <= $zCap; ++$z) { 75 | $az = $minZ + $z; 76 | for ($y = 0; $y <= $yCap; ++$y) { 77 | $ay = $minY + $y; 78 | $iterator->moveTo($ax, $ay, $az); 79 | $blocks[Level::blockHash($x, $y, $z)] = $iterator->currentSubChunk->getFullBlock($ax & 0x0f, $ay & 0x0f, $az & 0x0f); 80 | } 81 | } 82 | } 83 | 84 | $this->selection->setClipboard($blocks, $s_pos, $cap); 85 | 86 | $time = microtime(true) - $time; 87 | if ($callable !== null) { 88 | $callable($time, ($xCap + 1) * ($yCap + 1) * ($zCap + 1)); 89 | } 90 | } 91 | 92 | public function set(ChunkManager $level, Block $block, ?callable $callable = null) : void 93 | { 94 | $time = microtime(true); 95 | 96 | $minX = $this->pos1->x; 97 | $maxX = $this->pos2->x; 98 | $minY = $this->pos1->y; 99 | $maxY = $this->pos2->y; 100 | $minZ = $this->pos1->z; 101 | $maxZ = $this->pos2->z; 102 | 103 | $blockId = $block->getId(); 104 | $blockMeta = $block->getDamage(); 105 | 106 | $iterator = new BlockIterator($level); 107 | 108 | for ($x = $minX; $x <= $maxX; ++$x) { 109 | for ($z = $minZ; $z <= $maxZ; ++$z) { 110 | for ($y = $minY; $y <= $maxY; ++$y) { 111 | $iterator->moveTo($x, $y, $z); 112 | $iterator->currentSubChunk->setBlock($x & 0x0f, $y & 0x0f, $z & 0x0f, $blockId, $blockMeta); 113 | } 114 | } 115 | } 116 | 117 | if ($level instanceof Level) { 118 | Utils::updateChunks($level, $minX >> 4, $maxX >> 4, $minZ >> 4, $maxZ >> 4); 119 | } 120 | 121 | $time = microtime(true) - $time; 122 | if ($callable !== null) { 123 | $callable($time, (1 + $maxX - $minX) * (1 + $maxY - $minY) * (1 + $maxZ - $minZ)); 124 | } 125 | } 126 | 127 | public function replace(ChunkManager $level, Block $find, Block $replace, ?callable $callable = null) : void 128 | { 129 | $time = microtime(true); 130 | 131 | $minX = $this->pos1->x; 132 | $maxX = $this->pos2->x; 133 | $minY = $this->pos1->y; 134 | $maxY = $this->pos2->y; 135 | $minZ = $this->pos1->z; 136 | $maxZ = $this->pos2->z; 137 | 138 | $find = ($find->getId() << 4) | $find->getDamage();//fullBlock 139 | 140 | $replaceId = $replace->getId(); 141 | $replaceMeta = $replace->getDamage(); 142 | 143 | $iterator = new BlockIterator($level); 144 | 145 | for ($x = $minX; $x <= $maxX; ++$x) { 146 | for ($z = $minZ; $z <= $maxZ; ++$z) { 147 | for ($y = $minY; $y <= $maxY; ++$y) { 148 | $iterator->moveTo($x, $y, $z); 149 | if ($iterator->currentSubChunk->getFullBlock($x & 0x0f, $y & 0x0f, $z & 0x0f) === $find) { 150 | $iterator->currentSubChunk->setBlock($x & 0x0f, $y & 0x0f, $z & 0x0f, $replaceId, $replaceMeta); 151 | } 152 | } 153 | } 154 | } 155 | 156 | if ($level instanceof Level) { 157 | Utils::updateChunks($level, $minX >> 4, $maxX >> 4, $minZ >> 4, $maxZ >> 4); 158 | } 159 | 160 | $time = microtime(true) - $time; 161 | if ($callable !== null) { 162 | $callable($time, (1 + $maxX - $minX) * (1 + $maxY - $minY) * (1 + $maxZ - $minZ)); 163 | } 164 | } 165 | 166 | public function async() : AsyncCuboid 167 | { 168 | return new AsyncCuboid($this->pos1, $this->pos2, $this->selection); 169 | } 170 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | Copyright (C) 2007 Free Software Foundation, Inc. 4 | Everyone is permitted to copy and distribute verbatim copies 5 | of this license document, but changing it is not allowed. 6 | This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 7 | 0. Additional Definitions. 8 | As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. 9 | "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. 10 | An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface 11 | provided by the Library. 12 | A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". 13 | The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the 14 | Application, and not on the Linked Version. 15 | The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but 16 | excluding the System Libraries of the Combined Work. 17 | 1. Exception to Section 3 of the GNU GPL. 18 | You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 19 | 2. Conveying Modified Versions. 20 | If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), 21 | then you may convey a copy of the modified version: 22 | a) under this License, provided that you make a good faith effort to 23 | ensure that, in the event an Application does not supply the 24 | function or data, the facility still operates, and performs 25 | whatever part of its purpose remains meaningful, or 26 | b) under the GNU GPL, with none of the additional permissions of 27 | this License applicable to that copy. 28 | 3. Object Code Incorporating Material from Library Header Files. 29 | The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not 30 | limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: 31 | a) Give prominent notice with each copy of the object code that the 32 | Library is used in it and that the Library and its use are 33 | covered by this License. 34 | b) Accompany the object code with a copy of the GNU GPL and this license 35 | document. 36 | 4. Combined Works. 37 | You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such 38 | modifications, if you also do each of the following: 39 | a) Give prominent notice with each copy of the Combined Work that 40 | the Library is used in it and that the Library and its use are 41 | covered by this License. 42 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 43 | document. 44 | c) For a Combined Work that displays copyright notices during 45 | execution, include the copyright notice for the Library among 46 | these notices, as well as a reference directing the user to the 47 | copies of the GNU GPL and this license document. 48 | d) Do one of the following: 49 | 0) Convey the Minimal Corresponding Source under the terms of this 50 | License, and the Corresponding Application Code in a form 51 | suitable for, and under terms that permit, the user to 52 | recombine or relink the Application with a modified version of 53 | the Linked Version to produce a modified Combined Work, in the 54 | manner specified by section 6 of the GNU GPL for conveying 55 | Corresponding Source. 56 | 1) Use a suitable shared library mechanism for linking with the 57 | Library. A suitable mechanism is one that (a) uses at run time 58 | a copy of the Library already present on the user's computer 59 | system, and (b) will operate properly with a modified version 60 | of the Library that is interface-compatible with the Linked 61 | Version. 62 | e) Provide Installation Information, but only if you would otherwise 63 | be required to provide such information under section 6 of the 64 | GNU GPL, and only to the extent that such information is 65 | necessary to install and execute a modified version of the 66 | Combined Work produced by recombining or relinking the 67 | Application with a modified version of the Linked Version. (If 68 | you use option 4d0, the Installation Information must accompany 69 | the Minimal Corresponding Source and Corresponding Application 70 | Code. If you use option 4d1, you must provide the Installation 71 | Information in the manner specified by section 6 of the GNU GPL 72 | for conveying Corresponding Source.) 73 | 5. Combined Libraries. 74 | You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a 75 | combined library under terms of your choice, if you do both of the following: 76 | a) Accompany the combined library with a copy of the same work based 77 | on the Library, uncombined with any other library facilities, 78 | conveyed under the terms of this License. 79 | b) Give prominent notice with the combined library that part of it 80 | is a work based on the Library, and explaining where to find the 81 | accompanying uncombined form of the same work. 82 | 6. Revised Versions of the GNU Lesser General Public License. 83 | The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to 84 | address new problems or concerns. 85 | Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the 86 | option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU 87 | Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. 88 | If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent 89 | authorization for you to choose that version for the Library. 90 | --------------------------------------------------------------------------------