Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .poggit.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
--- # Poggit-CI Manifest. Open the CI at https://poggit.pmmp.io/ci/thebigsmileXD/libstructure
branches:
- master
projects:
libstructure:
path: ""
Expand Down
75 changes: 75 additions & 0 deletions src/xenialdan/dump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# NBT format output of exported.mcstructure
```
TAG_Compound({
'structure_world_origin': TAG_List<TAG_Int>({
1,
4,
0,
}),
'format_version': TAG_Int(1),
'size': TAG_List<TAG_Int>({
1,
1,
14,
}),
'structure': TAG_Compound({
'block_indices': TAG_List<TAG_List<TAG_Int>>({
{
0,
1,
0,
1,
0,
1,
0,
1,
1,
0,
1,
0,
1,
0,
},
{
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
},
}),
'entities': TAG_List<nil>({
}),
'palette': TAG_Compound({
'default': TAG_Compound({
'block_palette': TAG_List<TAG_Compound>({
{
'name': TAG_String(minecraft:air),
'states': TAG_Compound({
}),
'version': TAG_Int(17760256),
},
{
'name': TAG_String(minecraft:stained_glass),
'states': TAG_Compound({
'color': TAG_String(white),
}),
'version': TAG_Int(17760256),
},
}),
'block_position_data': TAG_Compound({
}),
}),
}),
}),
})
```
131 changes: 81 additions & 50 deletions src/xenialdan/libstructure/PacketListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,100 @@

namespace xenialdan\libstructure;

use InvalidArgumentException;
use pocketmine\block\BlockFactory;
use pocketmine\block\BlockIdentifier;
use pocketmine\block\BlockLegacyIds;
use pocketmine\block\tile\TileFactory;
use pocketmine\event\Listener;
use pocketmine\event\server\DataPacketReceiveEvent;
use pocketmine\network\mcpe\protocol\PacketPool;
use pocketmine\network\mcpe\protocol\StructureTemplateDataExportRequestPacket;
use pocketmine\network\mcpe\protocol\StructureTemplateDataExportResponsePacket;
use pocketmine\network\mcpe\protocol\StructureBlockUpdatePacket;
use pocketmine\network\mcpe\protocol\StructureTemplateDataRequestPacket;
use pocketmine\network\mcpe\protocol\StructureTemplateDataResponsePacket;
use pocketmine\plugin\Plugin;
use pocketmine\Server;
use xenialdan\libstructure\packet\StructureBlockUpdatePacket;
use pocketmine\plugin\PluginException;
use RuntimeException;
use xenialdan\libstructure\block\StructureBlock;
use xenialdan\libstructure\tile\StructureBlockTags;
use xenialdan\libstructure\tile\StructureBlockTile;
use xenialdan\libstructure\window\StructureBlockInventory;

class PacketListener implements Listener
{
/** @var Plugin|null */
private static $registrant;
/** @var Plugin|null */
private static ?Plugin $registrant = null;

public static function isRegistered(): bool
{
return self::$registrant instanceof Plugin;
}
public static function isRegistered(): bool
{
return self::$registrant instanceof Plugin;
}

public static function getRegistrant(): Plugin
{
return self::$registrant;
}
public static function getRegistrant(): Plugin
{
return self::$registrant;
}

public static function unregister(): void
{
self::$registrant = null;
}
public static function unregister(): void
{
self::$registrant = null;
}

/**
* @param Plugin $plugin
*/
public static function register(Plugin $plugin): void
{
if (self::isRegistered()) {
return;//silent return
}
/**
* @param Plugin $plugin
* @throws PluginException|RuntimeException
*/
public static function register(Plugin $plugin): void
{
if (self::isRegistered()) {
return;//silent return
}

self::$registrant = $plugin;
$plugin->getServer()->getPluginManager()->registerEvents(new self, $plugin);
PacketPool::registerPacket(new StructureBlockUpdatePacket());
}
self::$registrant = $plugin;
try {
TileFactory::getInstance()->register(StructureBlockTile::class, [StructureBlockTags::TAG_ID, "minecraft:structure_block"]);
BlockFactory::getInstance()->register(new StructureBlock(new BlockIdentifier(BlockLegacyIds::STRUCTURE_BLOCK,0, null, StructureBlockTile::class), "Structure Block"), true);
} catch (InvalidArgumentException) {
}
$plugin->getServer()->getPluginManager()->registerEvents(new self, $plugin);
}

public function onDataPacketReceiveEvent(DataPacketReceiveEvent $e)
{
if ($e->getPacket() instanceof StructureBlockUpdatePacket) $this->onStructureBlockUpdatePacket($e);
if ($e->getPacket() instanceof StructureTemplateDataExportRequestPacket) $this->onStructureTemplateDataExportRequestPacket($e);
if ($e->getPacket() instanceof StructureTemplateDataExportResponsePacket) $this->onStructureTemplateDataExportResponsePacket($e);
}
public function onDataPacketReceiveEvent(DataPacketReceiveEvent $e)
{
if ($e->getPacket() instanceof StructureBlockUpdatePacket) $this->onStructureBlockUpdatePacket($e);
//if ($e->getPacket() instanceof StructureTemplateDataRequestPacket) $this->onStructureTemplateDataExportRequestPacket($e);
//if ($e->getPacket() instanceof StructureTemplateDataResponsePacket) $this->onStructureTemplateDataExportResponsePacket($e);
}

private function onStructureBlockUpdatePacket(DataPacketReceiveEvent $e)
{
if (!($pk = $e->getPacket()) instanceof StructureBlockUpdatePacket) throw new \InvalidArgumentException(get_class($pk) . " is not a " . StructureBlockUpdatePacket::class);
/** @var StructureBlockUpdatePacket $pk */
var_dump($e->getPacket());
}
private function onStructureBlockUpdatePacket(DataPacketReceiveEvent $e)
{
if (!$e->getPacket() instanceof StructureBlockUpdatePacket) return;
//** @var StructureBlockUpdatePacket $pk */
#var_dump($e->getPacket());//TODO remove
$session = $e->getOrigin();
$window = $session->getInvManager()->getWindow($session->getInvManager()->getCurrentWindowId());
//Hack to close the inventory (client does not send inventory close packet for structure blocks)
if($window instanceof StructureBlockInventory){
$session->getPlayer()->removeCurrentWindow();
}
}

private function onStructureTemplateDataExportRequestPacket(DataPacketReceiveEvent $e)
{
}

private function onStructureTemplateDataExportResponsePacket(DataPacketReceiveEvent $e)
{
}
private function onStructureTemplateDataExportRequestPacket(DataPacketReceiveEvent $e)
{
/** @var StructureTemplateDataRequestPacket $pk */
$pk = $e->getPacket();
#$player = $e->getOrigin()->getPlayer();
if ($pk instanceof StructureTemplateDataRequestPacket) {
var_dump($pk);//TODO remove
}
}

private function onStructureTemplateDataExportResponsePacket(DataPacketReceiveEvent $e)
{
/** @var StructureTemplateDataResponsePacket $pk */
$pk = $e->getPacket();
#$player = $e->getOrigin()->getPlayer();
if ($pk instanceof StructureTemplateDataResponsePacket) {
var_dump($pk);//TODO remove
}
}
}
Loading