Git Product home page Git Product logo

blockdata's Introduction

BlockData

A PocketMine-MP virion that lets plugins set arbitrary data to blocks.

What's so special about BlockData?

  1. Multiple plugins can store data in the same block without overriding each other's data.
  2. BlockData never reads the database unless BlockDataWorld::getBlockDataAt() is called. So the block data isn't automatically cached when a chunk is loaded.
  3. yeahh..

Why don't you just store data in tiles?

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.

Are there any drawbacks?

  1. Since the virion has a strict policy on caching, a fresh call to BlockDataWorld::getBlockDataAt() would read the database synchronously. If this worries you, BlockData is backed by LevelDB and uses the snappy compression. A call to LevelDB::get() hardly takes a millisecond anyway. BlockData instances once created by the virion are cached until the chunk unloads.
  2. There's an inconsistency in deleting block => 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. Blocks 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.
  3. Because BlockData doesn't autoload with chunks (unlike tiles), you can't efficiently write BlockData that's always ticking.

Developer Docs

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 BlockDataWorlds 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.

blockdata's People

Contributors

endermanbugzjfc avatar javierleon9966 avatar muqsit avatar poggit-bot avatar royalmcpe avatar unickorn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

blockdata's Issues

Chunks unloading without loading

I have a plugin using this virion where I generate new worlds by copy pasting a folder from somewhere else and changing the level name in the level.dat before actually loading it. I'm not sure if it's related, but my chunks appear to unload before loading, causing BlockData to crash the server like this:
image

Would adding an isset() check there be an awful move? Any idea what could be causing this?

Issues with unloading

I'm having more undefined offset errors, mostly related to world unloading. PocketMine seems to be saving the world with the unload which I believe is causing these. Following are pastes of crashes.

This one is a command to "delete the island world" which unloads it first. Crashes as soon as it unloads.
https://hasteb.in/bewevegu.php

This one is a command I made to unload the world manually, after trying to avoid the above error by adding yet another isset check like so.
https://hasteb.in/obevaqak.php

Chunks unloading before loading

I have a plugin using this virion where I generate new worlds by copy pasting a folder from somewhere else and changing the level name in the level.dat before actually loading it. I'm not sure if it's related, but my chunks appear to unload before loading, causing BlockData to crash the server like this:
image

Would adding an isset() check there be an awful move? Any idea what could be causing this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.