Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- _____ _ _ _____
- | __ | |___ ___| |_| |___ ___
- | __ -| | . | _| '_| | | . |_ -| BlockOps
- |_____|_|___|___|_,_|_____| _|___| aka: Block Operations
- |_|
- Written by: PsyOps
- Description: Copy the current Minecraft PE commands - /setblock /fill /clone
- SETBLOCK
- ----------------------------------------------------------------------------------------------------------------------------
- /setblock <position: x y z> <tileName: string> [tileData: int] [oldBlockHandling]
- ----------------------------------------------------------------------------------------------------------------------------
- -- oldBlockHandling ---
- * destroy – Destroys the original block as if done by The Player, before placing the new block
- * keep – Only replaces air blocks with the new block
- * replace – Replaces without regards of old block. No dropping of block or content. This is the default
- FILL
- ----------------------------------------------------------------------------------------------------------------------------
- /fill <from: x y z> <to: x y z> <tileName: string> [tileData:int] [oldBlockHandling]
- ----------------------------------------------------------------------------------------------------------------------------
- -- oldBlockHandling ---
- * destroy – Replace all blocks, drops blocks and contents as if mined with unenchanted diamond pickaxe or shovel.
- * hollow – Replace only the blocks on the outer edge of the region. Drops all inner blocks as if they were mined, and replaced them with air blocks.
- * keep – Only replace the air blocks in the fill region with the specified block.
- * outline – Only replace the outer edge of region with specified block. Leave all inner blocks as they were.
- * replace – Replace all blocks, with no dropping of current blocks. This is the default.
- Clone
- ----------------------------------------------------------------------------------------------------------------------------
- command: /clone <begin x y z> <end x y z> <destination: x y z> [maskMode] [cloneMode] <tileName: string> [tileData:int]
- ----------------------------------------------------------------------------------------------------------------------------
- -- Legal values for maskMode are:
- * filtered – Only blocks matching tileName is copied.
- * masked – Only non-air blocks are copied.
- * replace – All blocks are copied as-is. This is the default.
- -- Legal values for cloneMode are:
- * force – Force moving of blocks even if regions overlap.
- * move – Move only non-air blocks, and replace them with air-blocks. If filtering is done, this only applies to the filtered blocks.
- * normal – Do not force or move blocks from source region. This is the default.
- */
- namespace S8Plugin\BlockOps;
- use pocketmine\command\CommandSender;
- use pocketmine\command\Command;
- use pocketmine\plugin\PluginBase;
- use pocketmine\Server;
- use pocketmine\Player;
- use pocketmine\block\Block;
- use pocketmine\item\Item;
- use pocketmine\math\Vector3;
- use pocketmine\tile\Sign;
- use pocketmine\nbt\NBT;
- use pocketmine\nbt\tag\StringTag;
- use pocketmine\nbt\tag\CompoundTag;
- use pocketmine\nbt\tag\ListTag;
- use pocketmine\nbt\tag\IntTag;
- use pocketmine\tile\Tile;
- use pocketmine\tile\Chest;
- use pocketmine\level\Position;
- class Main extends PluginBase {
- public function onEnable() {
- $this->saveDefaultConfig();
- }
- /* ___ _
- / __|___ _ __ _ __ __ _ _ _ __| |___
- | (__/ _ \ ' \| ' \/ _` | ' \/ _` (_-<
- \___\___/_|_|_|_|_|_\__,_|_||_\__,_/__/ - Commands
- --------------------------------------------------------------------------------------------------------------- */
- // Command handler
- public function onCommand(CommandSender $sender, Command $cmd, $label, array $args) {
- switch($cmd->getName()) {
- case "setblock":
- $this->doSetblock($sender, $args);
- break;
- case "fill":
- $this->doFill($sender, $args);
- break;
- case "clone":
- $this->doClone($sender, $args);
- break;
- case "test":
- $this->doTest($sender, $args);
- break;
- }
- }
- // I use this for my own testing
- private function doTest(CommandSender $sender, array $args){
- if(!$sender->hasPermission("test")) return;
- /* Code to test here */
- $sender->sendMessage("§cTest Fail!");
- //$tile = $sender->getLevel()->getTile(new Vector3($x,$y,$z));
- }
- //
- private function doClone(CommandSender $sender, array $args){
- // Check for permission
- if(!$sender->hasPermission("clone")) return;
- // Make sure command is formatted right
- if ( $this->areValidCoords( $sender, $args, 9 ) ){
- $this->printRegion($sender, false, $args);
- return;
- }
- $sender->sendMessage("§cUsage: /clone <begin x y z> <end x y z> <destination: x y z> [maskMode] [cloneMode] [tileName]");
- }
- private function doFill(CommandSender $sender, array $args){
- // Check for permission
- if(!$sender->hasPermission("fill")) return;
- // Make sure command is formatted right
- if ( $this->areValidCoords( $sender, $args, 6 ) && isset($args[6])){
- $this->printRegion($sender, true, $args);
- return;
- }
- // Code will get here only if command is not formated right
- $sender->sendMessage("§cUsage: /fill <from: x y z> <to: x y z> <tileName: string> [tileData:int] [oldBlockHandling]");
- }
- private function doSetblock(CommandSender $sender, array $args){
- // Check for permission
- if(!$sender->hasPermission("setblock")) return;
- // Make sure command is formatted right
- if ( $this->areValidCoords( $sender , $args, 3 ) && isset($args[3]) ) {
- $this->printBlock($sender, $args[0], $args[1], $args[2], $args[3], isset($args[4]) ? $args[4] : 0, isset($args[5]) ? $args[5] : 'r');
- return;
- }
- // Code will get here only if command is not formated right
- $sender->sendMessage("§cUsage: /setblock <position: x y z> <tileName: string> [tileData: int] [oldBlockHandling]");
- }
- /* ___ _ _
- | _ \_ _(_)_ _| |_
- | _/ '_| | ' \ _|
- |_| |_| |_|_||_\__| - Print
- --------------------------------------------------------------------------------------------------------------- */
- // Main function to print out more than one block at a time
- private function printRegion(CommandSender $sender, $fill, array $args){
- // Clone
- if (!$fill){
- // First Check for clone
- // Safety check to make sure origin and destination dont overlap - cloneMode override here too
- if ($this->regionsCollide($args)){
- if (!isset($args[10]) || ( $args[10] != "force" && $args[10] != "f" )) {
- $sender->sendMessage("§cCopy region and Clone region collide. Command ignored.");
- $sender->sendMessage("§cUsage: /clone <begin x y z> <end x y z> <destination: x y z> [maskMode] [cloneMode] [tileName]");
- return;
- }
- }
- }
- $specialObjects = [];
- // Loop through region
- for ($printZ = $args[2]; $printZ <= $args[5]; $printZ++){
- for ($printY = $args[1]; $printY <= $args[4]; $printY++){
- for ($printX = $args[0]; $printX <= $args[3]; $printX++){
- $allow = true;
- // Fill
- if ( $fill ) {
- // Set the OldBlockHandle
- $printOldBlockHandle = isset($args[8]) ? $args[8] : 'r';
- // Checking OldBlockHandle :Outline - if it is not part of edge do not print
- if ( $printOldBlockHandle == "outline" || $printOldBlockHandle == "o" && !$this->isOuterEdge($args,$printX,$printY,$printZ) ) $allow = false;
- // Checking OldBlockHandle :Hollow - if it is not part of edge send air block
- elseif ( $printOldBlockHandle == "hollow" || $printOldBlockHandle == "h" && !$this->isOuterEdge($args,$printX,$printY,$printZ) ) $printBlock = 'air';
- // if part of edge print normal
- else $printBlock = $args[6];
- // Update meta
- $printMeta = isset($args[7]) ? $args[7] : '';
- }
- // Clone
- else {
- // Set the maskMode to replace if it isnt set - default
- $printOldBlockHandle = isset($args[9]) ? $args[9] : 'r';
- // Saving Old block to send it out to new spot
- $oldBlockobj = $sender->getLevel()->getBlock(new Vector3($printX, $printY, $printZ));
- $printBlock = $oldBlockobj->getId();
- // Update meta with old block damage
- $printMeta = $oldBlockobj->getDamage();
- /* ------------------------------ Clone Mode ------------------------------ */
- // Checking cloneMode : Move
- if (isset($args[10])) {
- if ( $args[10] == "move" || $args[10] == "m" ){
- // is it air block
- if ($printBlock != 0 ){
- // Delete old block if it isnt air
- $this->printBlock($sender, $printX, $printY, $printZ, 0, 0, 'r');
- }
- }
- }
- /* ------------------------------ Mask Mode ------------------------------ */
- // Checking maskMode : Masked
- if ( $printOldBlockHandle == "masked" || $printOldBlockHandle == "m" ) $allow = $printBlock != 0;
- // Checking maskMode : Filtered
- elseif ( $printOldBlockHandle == "filtered" || $printOldBlockHandle == "f" ){
- // Check to see that tiledata was sent
- if ( isset($args[11])){
- // Grab the block to compare for filter
- $filterMeta = isset($args[12]) ? $args[12] : 0;
- $filterBlock = is_numeric($args[11]) ? Block::get((int)$args[11], $filterMeta) : Block::get((Item::fromString($args[11])->getBlock())->getId(), $filterMeta) ;
- $filterBlockId = $filterBlock->getId();
- // Only pass along blocks that match filter
- $allow = (int)$printBlock === (int)$filterBlockId;
- }
- }
- // Special block check here - Chests and Signs so far
- if (($allow) && ($printBlock == 63 || $printBlock == 68 || $printBlock == 54 )){
- $specialObjects[] = array(
- 'x'=> $args[6] + ($printX - $args[0]),
- 'y' => $args[7] + ($printY - $args[1]),
- 'z' => $args[8] + ($printZ - $args[2]),
- 'id' => $printBlock,
- 'meta' => $printBlock,
- 'obh' => $printOldBlockHandle,
- 'tile' => $sender->getLevel()->getTile(new Vector3($printX,$printY,$printZ)) );
- // Don't print tile now- hold it for later
- $allow = false;
- }
- }
- // print block
- if ($allow) $this->printBlock($sender, $fill ? $printX : $args[6] + ($printX - $args[0]) , $fill ? $printY : $args[7] + ($printY - $args[1]) , $fill ? $printZ : $args[8] + ($printZ - $args[2]) , $printBlock, $printMeta, $printOldBlockHandle);
- }
- }
- }
- // Print special tiles here - My guess is if the tile is attached to another block , block should be placed first
- if (is_array($specialObjects)){
- foreach($specialObjects as $b) {
- $sender->getLevel()->setBlock(new Vector3($b['x'], $b['y'], $b['z']), Block::get($b['id']));
- // Signs
- if ($b['id'] == 63 || $b['id'] == 68 || $b['id'] == 54 ){
- // Setup nbt tag
- $nbt = new CompoundTag("", [
- new ListTag("Items", [] ),
- new StringTag("id", $b['id'] == 54 ? Tile::CHEST :Tile::SIGN),
- new IntTag("x", $b['x']),
- new IntTag("y", $b['y']),
- new IntTag("z", $b['z']) ]);
- $nbt->Items->setTagType(NBT::TAG_Compound);
- // Create tile
- $tile = Tile::createTile( $b['id'] == 54 ? Tile::CHEST :Tile::SIGN, $sender->getLevel(), $nbt);
- if (isset($tile)){
- // Copy over text from old sign
- if( $b['id'] == 63 || $b['id'] == 68 ){
- $tile->setText($b['tile']->getText()[0], $b['tile']->getText()[1], $b['tile']->getText()[2], $b['tile']->getText()[3]);
- }
- // Put copied items into new chest
- elseif ( $b['id'] == 54 ){
- $tile->getInventory()->setContents($b['tile']->getInventory()->getContents());
- }
- }
- else $sender->sendMessage("§cTile not picked up!");
- }
- }
- }
- }
- // Single block print
- private function printBlock(CommandSender $sender, $x, $y, $z, $block, $meta, $oldBlockHandle){
- // set meta
- $meta = isset($meta) ? $meta : 0;
- // Set as: old block = air block - it only gets changed if "Keep" is selected
- // for old block handeling
- $isAir = true;
- // How to handle the Old Block being replaced
- if (isset($oldBlockHandle)) {
- if ( $oldBlockHandle == "destroy" || $oldBlockHandle == "d" ){
- $item = null;
- $sender->getLevel()->useBreakOn(new Vector3($x, $y, $z), $item, $sender, true);
- }
- elseif ( $oldBlockHandle == "keep" || $oldBlockHandle == "k" ){
- // Add air block check
- $isAir = $sender->getLevel()->getBlock(new Vector3($x, $y, $z))->getId() == 0;
- }
- }
- if ($isAir){
- // Grab block obj
- $blockobj = is_numeric($block) ? Block::get((int)$block, $meta) : Block::get((Item::fromString($block)->getBlock())->getId(), $meta);
- // send the command
- $sender->getLevel()->setBlock(new Vector3($x, $y, $z), $blockobj, true, true);
- }
- }
- /* __ __ _ ___
- | \/ (_)___ __ | __| _ _ _ __
- | |\/| | (_-</ _| | _| || | ' \/ _|
- |_| |_|_/__/\__| |_| \_,_|_||_\__| - Misc Functions
- --------------------------------------------------------------------------------------------------------------- */
- // Function to make sure that our copyto region doesnt overlap on the cloneregion
- private function regionsCollide(array $args){
- // Region to clone: Clone region
- $x = $args[0]; $y = $args[1]; $z = $args[2];
- $x2 = $args[3]; $y2 = $args[4]; $z2 = $args[5];
- // Region to copy to: CopyTo Region
- $newX = $args[6]; $newY = $args[7]; $newZ = $args[8];
- $newX2 = $newX + ( $x2 - $x ); $newY2 = $newY + ( $y2 - $y ); $newZ2 = $newZ + ( $z2 - $z );
- if ( $newX > $x2 || $newX2 < $x ) { return false; }
- else if ( $newY > $y2 || $newY2 < $y ) { return false; }
- else if ( $newZ > $z2 || $newZ2 < $z ) { return false; }
- return true;
- }
- private function isOuterEdge( array $args, $newX, $newY, $newZ ){
- $x = $args[0]; $y = $args[1]; $z = $args[2];
- $x2 = $args[3]; $y2 = $args[4]; $z2 = $args[5];
- if ( $newX == $x || $newX == $x2 || $newY == $y || $newY == $y2 || $newZ == $z || $newZ == $z2 ) return true;
- return false;
- }
- // Checking coords and rearranging to find lowest point
- private function areValidCoords( CommandSender $sender, array &$args, $count){
- $tempSet = array('');
- $count--;
- for ($l = 0; $l <= $count ; $l++){
- // First check- Make sure it is set
- if (!isset($args[$l])) return false;
- if (is_numeric($args[$l])){
- $args[$l] = (int)$args[$l];
- $tempSet[$l] = $args[$l];
- }// Handle ~ entries
- elseif($args[$l] == "~" ){
- // x coord
- if ( $l == 0 || $l == 3 || $l == 6 ){
- $args[$l] = (int)$sender->getX();
- $tempSet[$l] = $args[$l];
- }// y coord
- elseif ( $l == 1 || $l == 4 || $l == 7 ){
- $args[$l] = (int)$sender->getY();
- $tempSet[$l] = $args[$l];
- }// z coord
- elseif ( $l == 2 || $l == 5 || $l == 8 ){
- $args[$l] = (int)$sender->getZ();
- $tempSet[$l] = $args[$l];
- }
- }
- else{
- return false;
- }
- }
- if ( $count >= 5 ){
- // Rearrange coords so we start at lower most part of region
- $args[0] = $tempSet[0] < $tempSet[3] ? $tempSet[0] : $tempSet[3] ;
- $args[1] = $tempSet[1] < $tempSet[4] ? $tempSet[1] : $tempSet[4] ;
- $args[2] = $tempSet[2] < $tempSet[5] ? $tempSet[2] : $tempSet[5] ;
- $args[3] = $tempSet[0] < $tempSet[3] ? $tempSet[3] : $tempSet[0] ;
- $args[4] = $tempSet[1] < $tempSet[4] ? $tempSet[4] : $tempSet[1] ;
- $args[5] = $tempSet[2] < $tempSet[5] ? $tempSet[5] : $tempSet[2] ;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement