{"id":15143392,"url":"https://github.com/cosmoverse/blockdata","last_synced_at":"2025-10-23T20:30:28.991Z","repository":{"id":51340450,"uuid":"235358259","full_name":"Cosmoverse/BlockData","owner":"Cosmoverse","description":"A virion for PocketMine-MP that lets plugins store arbitrary block data","archived":false,"fork":false,"pushed_at":"2023-09-03T12:33:45.000Z","size":28,"stargazers_count":31,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-30T20:51:49.612Z","etag":null,"topics":["pmmp","pocketmine-mp","virion"],"latest_commit_sha":null,"homepage":"https://poggit.pmmp.io/ci/Cosmoverse/BlockData","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Cosmoverse.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-01-21T14:13:19.000Z","updated_at":"2024-11-11T22:49:57.000Z","dependencies_parsed_at":"2022-09-25T21:18:32.710Z","dependency_job_id":null,"html_url":"https://github.com/Cosmoverse/BlockData","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cosmoverse%2FBlockData","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cosmoverse%2FBlockData/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cosmoverse%2FBlockData/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cosmoverse%2FBlockData/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Cosmoverse","download_url":"https://codeload.github.com/Cosmoverse/BlockData/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237882172,"owners_count":19381176,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["pmmp","pocketmine-mp","virion"],"created_at":"2024-09-26T10:01:36.576Z","updated_at":"2025-10-23T20:30:28.657Z","avatar_url":"https://github.com/Cosmoverse.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# BlockData\nA PocketMine-MP virion that lets plugins set arbitrary data to blocks.\n\n### What's so special about BlockData?\n1. Multiple plugins can store data in the same block without overriding each other's data.\n2. BlockData never reads the database unless `BlockDataWorld::getBlockDataAt()` is called. So the block data isn't automatically cached when a chunk is loaded.\n3. yeahh..\n\n### Why don't you just store data in tiles?\nStoring 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.\nIf you were to store block data for each block in a chunk using tiles, that's 65536 tile instances in the RAM!\nTo avoid this, BlockData only loads data of blocks when explicitly requested to.\n\n### Are there any drawbacks?\n1. Since the virion has a strict policy on caching, a fresh call to `BlockDataWorld::getBlockDataAt()` would read the database synchronously.\nIf this worries you, BlockData is backed by LevelDB and uses the [snappy](http://google.github.io/snappy/) compression. A call to `LevelDB::get()` hardly takes a millisecond anyway.\nBlockData instances once created by the virion are cached until the chunk unloads.\n2. There's an inconsistency in deleting block =\u003e deleting data. This is due to the fact that blocks can be set by several different ways but there aren't the same number of ways to directly listen for block changes.\nBlocks can be changed using `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.\n3. Because BlockData doesn't autoload with chunks (unlike tiles), you can't efficiently write BlockData that's always ticking.\n\n### Developer Docs\nInstall with the Virion 3 standard:\n```bash\n$ composer require cosmicpe/blockdata\n```\n\nThe first thing your plugin will have to do to gain access to this virion's API is request a `BlockDataWorldManager` instance.\n`BlockDataWorldManager` maps `BlockDataWorld`s to pocketmine's worlds. `BlockDataWorld` provides an API to get and set `BlockData`.\n```php\nfinal class MyPlugin extends PluginBase{\n\n\t/** @var BlockDataWorldManager */\n\tprivate $manager;\n\t\n\tprotected function onEnable() : void{\n\t\t$this-\u003emanager = BlockDataWorldManager::create($this);\n\t}\n}\n```\n\nNow 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).\n```php\nclass BlockHistoryData extends BlockData{ // stores when block was placed and by whom.\n\n\tpublic static function nbtDeserialize(CompoundTag $nbt) : BlockData{\n\t\treturn new BlockHistoryData($nbt-\u003egetString(\"placer\"), $nbt-\u003egetLong(\"timestamp\"));\n\t}\n\t\n\t/** @var string */\n\tprivate $placer;\n\n\t/** @var int */\n\tprivate $timestamp;\n\t\n\tpublic function __construct(string $placer, ?int $timestamp = null){\n\t\t$this-\u003eplacer = $placer;\n\t\t$this-\u003etimestamp = $timestamp ?? time();\n\t}\n\t\n\tpublic function getPlacer() : string{\n\t\treturn $this-\u003eplacer;\n\t}\n\t\n\tpublic function getTimestamp() : int{\n\t\treturn $this-\u003etimestamp;\n\t}\n\t\n\tpublic function nbtSerialize() : CompoundTag{\n\t\treturn CompoundTag::create()\n\t\t\t-\u003esetString(\"placer\", $this-\u003eplacer)\n\t\t\t-\u003esetLong(\"timestamp\", $this-\u003etimestamp);\n\t}\n}\n```\n\nAnd map it to a string identifier.\n```php\nconst BLOCK_HISTORY_DATA = \"blockhistory\";\nBlockDataFactory::register(self::BLOCK_HISTORY_DATA, BlockHistoryData::class);\n```\n\nWew, now all that's remaining is event handling!\n```php\npublic function onBlockPlace(BlockPlaceEvent $event) : void{\n\t$block = $event-\u003egetBlock();\n\t$pos = $block-\u003egetPos();\n\t$data = new BlockHistoryData($event-\u003egetPlayer()-\u003egetName());\n\t$this-\u003emanager-\u003egetWorld($pos-\u003egetWorld())-\u003esetBlockDataAt($pos-\u003ex, $pos-\u003ey, $pos-\u003ez, $data);\n}\n\npublic function onPlayerInteract(PlayerInteractEvent $event) : void{\n\tif($event-\u003egetAction() === PlayerInteractEvent::RIGHT_CLICK_BLOCK \u0026\u0026 $event-\u003egetItem()-\u003egetId() === ItemIds::STICK){\n\t\t$block = $event-\u003egetBlock();\n\t\t$pos = $block-\u003egetPos();\n\t\t\n\t\t$data = $this-\u003emanager-\u003eget($pos-\u003egetWorld())-\u003egetBlockDataAt($pos-\u003ex, $pos-\u003ey, $pos-\u003ez);\n\t\tif($data instanceof BlockHistoryData){\n\t\t\t$event-\u003egetPlayer()-\u003esendMessage(TextFormat::LIGHT_PURPLE . \"This block was placed by \" . TextFormat::WHITE . $data-\u003egetPlacer() . TextFormat::LIGHT_PURPLE . \" on \" . TextFormat::WHITE . gmdate(\"d-m-Y H:i:s\", $data-\u003egetTimestamp()));\n\t\t}\n\t}\n}\n```\n\nAlso check out [BlockData-Example-Plugin](https://github.com/Cosmoverse/BlockData-Example-Plugin).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcosmoverse%2Fblockdata","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcosmoverse%2Fblockdata","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcosmoverse%2Fblockdata/lists"}