A virion for PocketMine-MP that lets plugins store arbitrary block data
A PocketMine-MP virion that lets plugins set arbitrary data to blocks.
BlockDataWorld::getBlockDataAt()
is called. So the block data isn't automatically cached when a chunk is loaded.Storing data in tiles is not a problem as long as the number of loaded tiles can be kept moderated. When a chunk loads, the server reads all tiles from the database and caches them. If you were to store block data for each block in a chunk using tiles, that's 65536 tile instances in the RAM! To avoid this, BlockData only loads data of blocks when explicitly requested to.
BlockDataWorld::getBlockDataAt()
would read the database synchronously.LevelDB::get()
hardly takes a millisecond anyway.World::setBlockAt()
, World::setChunk()
or even when the plugin using this virion is disabled and the plugin will never know that the block was deleted, so the BlockData in such cases will exist well after the block has been deleted.Install with the Virion 3 standard:
$ composer require cosmicpe/blockdata
The first thing your plugin will have to do to gain access to this virion's API is request a BlockDataWorldManager
instance.
BlockDataWorldManager
maps BlockDataWorld
s to pocketmine's worlds. BlockDataWorld
provides an API to get and set BlockData
.
final class MyPlugin extends PluginBase{
/** @var BlockDataWorldManager */
private $manager;
protected function onEnable() : void{
$this->manager = BlockDataWorldManager::create($this);
}
}
Now lets create a BlockData class! BlockData is backed by nbt. (Honestly, might switch over to JSON but not sure if it's worth sacrificing binary-safe data storage).
class BlockHistoryData extends BlockData{ // stores when block was placed and by whom.
public static function nbtDeserialize(CompoundTag $nbt) : BlockData{
return new BlockHistoryData($nbt->getString("placer"), $nbt->getLong("timestamp"));
}
/** @var string */
private $placer;
/** @var int */
private $timestamp;
public function __construct(string $placer, ?int $timestamp = null){
$this->placer = $placer;
$this->timestamp = $timestamp ?? time();
}
public function getPlacer() : string{
return $this->placer;
}
public function getTimestamp() : int{
return $this->timestamp;
}
public function nbtSerialize() : CompoundTag{
return CompoundTag::create()
->setString("placer", $this->placer)
->setLong("timestamp", $this->timestamp);
}
}
And map it to a string identifier.
const BLOCK_HISTORY_DATA = "blockhistory";
BlockDataFactory::register(self::BLOCK_HISTORY_DATA, BlockHistoryData::class);
Wew, now all that's remaining is event handling!
public function onBlockPlace(BlockPlaceEvent $event) : void{
$block = $event->getBlock();
$pos = $block->getPos();
$data = new BlockHistoryData($event->getPlayer()->getName());
$this->manager->getWorld($pos->getWorld())->setBlockDataAt($pos->x, $pos->y, $pos->z, $data);
}
public function onPlayerInteract(PlayerInteractEvent $event) : void{
if($event->getAction() === PlayerInteractEvent::RIGHT_CLICK_BLOCK && $event->getItem()->getId() === ItemIds::STICK){
$block = $event->getBlock();
$pos = $block->getPos();
$data = $this->manager->get($pos->getWorld())->getBlockDataAt($pos->x, $pos->y, $pos->z);
if($data instanceof BlockHistoryData){
$event->getPlayer()->sendMessage(TextFormat::LIGHT_PURPLE . "This block was placed by " . TextFormat::WHITE . $data->getPlacer() . TextFormat::LIGHT_PURPLE . " on " . TextFormat::WHITE . gmdate("d-m-Y H:i:s", $data->getTimestamp()));
}
}
}
Also check out BlockData-Example-Plugin.