Skip to content

Instantly share code, notes, and snippets.

@jasonw4331
Last active August 6, 2019 03:55
Show Gist options
  • Save jasonw4331/d20f75a66b37d9be7e1d09add5ac8a39 to your computer and use it in GitHub Desktop.
Save jasonw4331/d20f75a66b37d9be7e1d09add5ac8a39 to your computer and use it in GitHub Desktop.
A plugin script which allows players to fix blocks when using java edition maps
<?php
declare(strict_types=1);
/**
* @name BlockTypeCorrector
* @main jasonwynn10\BlockTypeCorrector\Main
* @version 0.1.0
* @api 3.0.0
* @description A plugin script which allows players to fix blocks when converting from PC edition
* @author jasonwynn10
*/
namespace jasonwynn10\BlockTypeCorrector{
use pocketmine\block\Block;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\command\utils\CommandException;
use pocketmine\level\format\Chunk;
use pocketmine\level\Level;
use pocketmine\Player;
use pocketmine\plugin\PluginBase;
use pocketmine\scheduler\Task;
class Main extends PluginBase {
public function onEnable() {
$this->getServer()->getCommandMap()->register('BlockTypeCorrector', new class($this) extends Command {
private $plugin;
public function __construct(Main $plugin) {
$this->plugin = $plugin;
parent::__construct("fixchunk", "Fixes the block types of the current chunk", "/fc", ['fc']);
}
/**
* @param CommandSender $sender
* @param string $commandLabel
* @param string[] $args
*
* @return mixed
* @throws CommandException
*/
public function execute(CommandSender $sender, string $commandLabel, array $args) {
if(!$sender instanceof Player)
return true;
$sender->sendMessage("Starting Fix on ".count(array_filter($sender->usedChunks))." chunks");
foreach($sender->usedChunks as $chunkHash => $sent) {
if($sent) {
Level::getXZ($chunkHash, $cx, $cz);
$level = $sender->getLevel();
$chunk = $level->getChunk($cx, $cz);
$this->plugin->getScheduler()->scheduleTask(new class($this->plugin, $chunk, $level) extends Task {
private $plugin, $chunk, $level;
public function __construct(Main $plugin, Chunk $chunk, Level $level) {
$this->plugin = $plugin;
$this->chunk = $chunk;
$this->level = $level;
}
/**
* Actions to execute when run
*
* @param int $currentTick
*
* @return void
*/
public function onRun(int $currentTick) {
$list = [];
$chunk = $this->chunk;
$height = $chunk->getMaxY();
for($x = 0; $x < 16; $x++) {
for($z = 0; $z < 16; $z++) {
for($y = 0; $y <= $height; $y++) {
$id = $chunk->getBlockId($x, $y, $z);
$meta = $chunk->getBlockData($x, $y, $z);
$block = Block::get($id, $meta);
$block->setComponents(($chunk->getX() << 4) + $x, $y, ($chunk->getZ() << 4) + $z);
$block->setLevel($this->level);
$list[] = $block;
}
}
}
$this->plugin->fixBlockIds($list);
}
});
}
}
$sender->sendMessage("Detected blocks have been fixed.");
return true;
}
});
}
/**
* @param Block[] $list
*/
public function fixBlockIds(array &$list) : void {
foreach($list as $k => $block) {
if(!$block->isValid())
continue;
$replace = null;
switch($block->getId()) {
case 126:
$replace = Block::get(Block::WOODEN_SLAB, $block->getDamage());
break;
case 95:
$replace = Block::get(Block::GLASS);
break;
case 160:
$replace = Block::get(Block::GLASS_PANE);
break;
case 125:
$replace = Block::get(Block::DOUBLE_WOODEN_SLAB, $block->getDamage());
break;
case 188:
$replace = Block::get(Block::FENCE, 1);
break;
case 189:
$replace = Block::get(Block::FENCE, 2);
break;
case 190:
$replace = Block::get(Block::FENCE, 3);
break;
case 191:
$replace = Block::get(Block::FENCE, 5);
break;
case 192:
$replace = Block::get(Block::FENCE, 4);
break;
default:
break;
}
if($replace !== null) {
$block->getLevel()->setBlock($block, $replace, false, false);
$this->getLogger()->info("Replaced ".(string)$block." with ".(string)$replace." at ".(string)($block->asPosition()));
}else{
unset($list[$k]);
}
}
}
}
}
@benda95280
Copy link

How to use it ?

@jasonw4331
Copy link
Author

Put in plugins folder. Run /fc.

@benda95280
Copy link

benda95280 commented Aug 6, 2019

Wasn't knowing it was possible like that, thanks you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment