mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-21 00:07:30 +00:00
Implemented Lectern (#4708)
Co-authored-by: Covered123 <58715544+JavierLeon9966@users.noreply.github.com> Co-authored-by: Dylan K. Taylor <dktapps@pmmp.io>
This commit is contained in:
parent
ee6548aa50
commit
1366c49f1f
@ -43,6 +43,7 @@ use pocketmine\block\tile\FlowerPot as TileFlowerPot;
|
||||
use pocketmine\block\tile\Hopper as TileHopper;
|
||||
use pocketmine\block\tile\ItemFrame as TileItemFrame;
|
||||
use pocketmine\block\tile\Jukebox as TileJukebox;
|
||||
use pocketmine\block\tile\Lectern as TileLectern;
|
||||
use pocketmine\block\tile\MonsterSpawner as TileMonsterSpawner;
|
||||
use pocketmine\block\tile\NormalFurnace as TileNormalFurnace;
|
||||
use pocketmine\block\tile\Note as TileNote;
|
||||
@ -253,6 +254,7 @@ class BlockFactory{
|
||||
$this->registerAllMeta(new Opaque(new BID(Ids::LAPIS_BLOCK, 0), "Lapis Lazuli Block", new BlockBreakInfo(3.0, BlockToolType::PICKAXE, ToolTier::STONE()->getHarvestLevel())));
|
||||
$this->registerAllMeta(new LapisOre(new BID(Ids::LAPIS_ORE, 0), "Lapis Lazuli Ore", new BlockBreakInfo(3.0, BlockToolType::PICKAXE, ToolTier::STONE()->getHarvestLevel())));
|
||||
$this->registerAllMeta(new Lava(new BIDFlattened(Ids::FLOWING_LAVA, [Ids::STILL_LAVA], 0), "Lava", BlockBreakInfo::indestructible(500.0)));
|
||||
$this->registerAllMeta(new Lectern(new BID(Ids::LECTERN, 0, ItemIds::LECTERN, TileLectern::class), "Lectern", new BlockBreakInfo(2.0, BlockToolType::AXE)));
|
||||
$this->registerAllMeta(new Lever(new BID(Ids::LEVER, 0), "Lever", new BlockBreakInfo(0.5)));
|
||||
$this->registerAllMeta(new Loom(new BID(Ids::LOOM, 0), "Loom", new BlockBreakInfo(2.5, BlockToolType::AXE)));
|
||||
$this->registerAllMeta(new Magma(new BID(Ids::MAGMA, 0), "Magma Block", new BlockBreakInfo(0.5, BlockToolType::PICKAXE, ToolTier::WOOD()->getHarvestLevel())));
|
||||
@ -612,7 +614,6 @@ class BlockFactory{
|
||||
//TODO: minecraft:jigsaw
|
||||
//TODO: minecraft:kelp
|
||||
//TODO: minecraft:lava_cauldron
|
||||
//TODO: minecraft:lectern
|
||||
//TODO: minecraft:movingBlock
|
||||
//TODO: minecraft:observer
|
||||
//TODO: minecraft:piston
|
||||
|
127
src/block/Lectern.php
Normal file
127
src/block/Lectern.php
Normal file
@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\block;
|
||||
|
||||
use pocketmine\block\tile\Lectern as TileLectern;
|
||||
use pocketmine\block\utils\BlockDataSerializer;
|
||||
use pocketmine\block\utils\FacesOppositePlacingPlayerTrait;
|
||||
use pocketmine\block\utils\HorizontalFacingTrait;
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\item\WritableBookBase;
|
||||
use pocketmine\math\AxisAlignedBB;
|
||||
use pocketmine\math\Facing;
|
||||
use pocketmine\math\Vector3;
|
||||
use pocketmine\player\Player;
|
||||
|
||||
class Lectern extends Transparent{
|
||||
use FacesOppositePlacingPlayerTrait;
|
||||
use HorizontalFacingTrait;
|
||||
|
||||
protected int $viewedPage = 0;
|
||||
protected ?WritableBookBase $book = null;
|
||||
|
||||
public function readStateFromData(int $id, int $stateMeta) : void{
|
||||
$this->facing = BlockDataSerializer::readLegacyHorizontalFacing($stateMeta);
|
||||
}
|
||||
|
||||
public function writeStateToMeta() : int{
|
||||
return BlockDataSerializer::writeLegacyHorizontalFacing($this->facing);
|
||||
}
|
||||
|
||||
public function readStateFromWorld() : void{
|
||||
parent::readStateFromWorld();
|
||||
$tile = $this->position->getWorld()->getTile($this->position);
|
||||
if($tile instanceof TileLectern){
|
||||
$this->viewedPage = $tile->getViewedPage();
|
||||
$this->book = $tile->getBook();
|
||||
}
|
||||
}
|
||||
|
||||
public function writeStateToWorld() : void{
|
||||
parent::writeStateToWorld();
|
||||
$tile = $this->position->getWorld()->getTile($this->position);
|
||||
if($tile instanceof TileLectern){
|
||||
$tile->setViewedPage($this->viewedPage);
|
||||
$tile->setBook($this->book);
|
||||
}
|
||||
}
|
||||
|
||||
public function getStateBitmask() : int{
|
||||
return 0b11;
|
||||
}
|
||||
|
||||
public function getFlammability() : int{
|
||||
return 30;
|
||||
}
|
||||
|
||||
public function getDrops(Item $item) : array{
|
||||
$drops = parent::getDrops($item);
|
||||
if($this->book !== null){
|
||||
$drops[] = clone $this->book;
|
||||
}
|
||||
|
||||
return $drops;
|
||||
}
|
||||
|
||||
protected function recalculateCollisionBoxes() : array{
|
||||
return [AxisAlignedBB::one()->trim(Facing::UP, 0.1)];
|
||||
}
|
||||
|
||||
public function getViewedPage() : int{
|
||||
return $this->viewedPage;
|
||||
}
|
||||
|
||||
/** @return $this */
|
||||
public function setViewedPage(int $viewedPage) : self{
|
||||
$this->viewedPage = $viewedPage;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getBook() : ?WritableBookBase{
|
||||
return $this->book !== null ? clone $this->book : null;
|
||||
}
|
||||
|
||||
/** @return $this */
|
||||
public function setBook(?WritableBookBase $book) : self{
|
||||
$this->book = $book !== null && !$book->isNull() ? (clone $book)->setCount(1) : null;
|
||||
$this->viewedPage = 0;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
|
||||
if($this->book === null && $item instanceof WritableBookBase){
|
||||
$this->position->getWorld()->setBlock($this->position, $this->setBook($item));
|
||||
$item->pop();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function onAttack(Item $item, int $face, ?Player $player = null) : bool{
|
||||
if($this->book !== null){
|
||||
$this->position->getWorld()->dropItem($this->position->up(), $this->book);
|
||||
$this->position->getWorld()->setBlock($this->position, $this->setBook(null));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
@ -363,6 +363,7 @@ use pocketmine\utils\CloningRegistryTrait;
|
||||
* @method static LapisOre LAPIS_LAZULI_ORE()
|
||||
* @method static DoubleTallGrass LARGE_FERN()
|
||||
* @method static Lava LAVA()
|
||||
* @method static Lectern LECTERN()
|
||||
* @method static Opaque LEGACY_STONECUTTER()
|
||||
* @method static Lever LEVER()
|
||||
* @method static GlazedTerracotta LIGHT_BLUE_GLAZED_TERRACOTTA()
|
||||
@ -924,6 +925,7 @@ final class VanillaBlocks{
|
||||
self::register("lapis_lazuli_ore", $factory->get(21, 0));
|
||||
self::register("large_fern", $factory->get(175, 3));
|
||||
self::register("lava", $factory->get(10, 0));
|
||||
self::register("lectern", $factory->get(449, 0));
|
||||
self::register("legacy_stonecutter", $factory->get(245, 0));
|
||||
self::register("lever", $factory->get(69, 0));
|
||||
self::register("light_blue_glazed_terracotta", $factory->get(223, 2));
|
||||
|
87
src/block/tile/Lectern.php
Normal file
87
src/block/tile/Lectern.php
Normal file
@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\block\tile;
|
||||
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\item\WritableBookBase;
|
||||
use pocketmine\nbt\tag\CompoundTag;
|
||||
use function count;
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
* @see \pocketmine\block\Lectern
|
||||
*/
|
||||
class Lectern extends Spawnable{
|
||||
public const TAG_HAS_BOOK = "hasBook";
|
||||
public const TAG_PAGE = "page";
|
||||
public const TAG_TOTAL_PAGES = "totalPages";
|
||||
public const TAG_BOOK = "book";
|
||||
|
||||
private int $viewedPage = 0;
|
||||
private ?WritableBookBase $book = null;
|
||||
|
||||
public function readSaveData(CompoundTag $nbt) : void{
|
||||
$this->viewedPage = $nbt->getInt(self::TAG_PAGE, 0);
|
||||
if(($itemTag = $nbt->getCompoundTag(self::TAG_BOOK)) !== null){
|
||||
$book = Item::nbtDeserialize($itemTag);
|
||||
if($book instanceof WritableBookBase && !$book->isNull()){
|
||||
$this->book = $book;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function writeSaveData(CompoundTag $nbt) : void{
|
||||
$nbt->setByte(self::TAG_HAS_BOOK, $this->book !== null ? 1 : 0);
|
||||
$nbt->setInt(self::TAG_PAGE, $this->viewedPage);
|
||||
if($this->book !== null){
|
||||
$nbt->setTag(self::TAG_BOOK, $this->book->nbtSerialize());
|
||||
$nbt->setInt(self::TAG_TOTAL_PAGES, count($this->book->getPages()));
|
||||
}
|
||||
}
|
||||
|
||||
public function getViewedPage() : int{
|
||||
return $this->viewedPage;
|
||||
}
|
||||
|
||||
public function setViewedPage(int $viewedPage) : void{
|
||||
$this->viewedPage = $viewedPage;
|
||||
}
|
||||
|
||||
public function getBook() : ?WritableBookBase{
|
||||
return $this->book !== null ? clone $this->book : null;
|
||||
}
|
||||
|
||||
public function setBook(?WritableBookBase $book) : void{
|
||||
$this->book = $book !== null && !$book->isNull() ? clone $book : null;
|
||||
}
|
||||
|
||||
protected function addAdditionalSpawnData(CompoundTag $nbt) : void{
|
||||
$nbt->setByte(self::TAG_HAS_BOOK, $this->book !== null ? 1 : 0);
|
||||
$nbt->setInt(self::TAG_PAGE, $this->viewedPage);
|
||||
if($this->book !== null){
|
||||
$nbt->setTag(self::TAG_BOOK, $this->book->nbtSerialize());
|
||||
$nbt->setInt(self::TAG_TOTAL_PAGES, count($this->book->getPages()));
|
||||
}
|
||||
}
|
||||
}
|
@ -67,6 +67,7 @@ final class TileFactory{
|
||||
$this->register(Hopper::class, ["Hopper", "minecraft:hopper"]);
|
||||
$this->register(ItemFrame::class, ["ItemFrame"]); //this is an entity in PC
|
||||
$this->register(Jukebox::class, ["Jukebox", "RecordPlayer", "minecraft:jukebox"]);
|
||||
$this->register(Lectern::class, ["Lectern", "minecraft:lectern"]);
|
||||
$this->register(MonsterSpawner::class, ["MobSpawner", "minecraft:mob_spawner"]);
|
||||
$this->register(Note::class, ["Music", "minecraft:noteblock"]);
|
||||
$this->register(ShulkerBox::class, ["ShulkerBox", "minecraft:shulker_box"]);
|
||||
@ -85,7 +86,6 @@ final class TileFactory{
|
||||
//TODO: EndGateway
|
||||
//TODO: EndPortal
|
||||
//TODO: JigsawBlock
|
||||
//TODO: Lectern
|
||||
//TODO: MovingBlock
|
||||
//TODO: NetherReactor
|
||||
//TODO: PistonArm
|
||||
|
@ -628,6 +628,7 @@ final class StringToItemParser extends StringToTParser{
|
||||
$result->registerBlock("leave2", fn() => VanillaBlocks::ACACIA_LEAVES());
|
||||
$result->registerBlock("leaves", fn() => VanillaBlocks::OAK_LEAVES());
|
||||
$result->registerBlock("leaves2", fn() => VanillaBlocks::ACACIA_LEAVES());
|
||||
$result->registerBlock("lectern", fn() => VanillaBlocks::LECTERN());
|
||||
$result->registerBlock("legacy_stonecutter", fn() => VanillaBlocks::LEGACY_STONECUTTER());
|
||||
$result->registerBlock("lever", fn() => VanillaBlocks::LEVER());
|
||||
$result->registerBlock("light_blue_glazed_terracotta", fn() => VanillaBlocks::LIGHT_BLUE_GLAZED_TERRACOTTA());
|
||||
|
@ -25,6 +25,7 @@ namespace pocketmine\network\mcpe\handler;
|
||||
|
||||
use pocketmine\block\BaseSign;
|
||||
use pocketmine\block\ItemFrame;
|
||||
use pocketmine\block\Lectern;
|
||||
use pocketmine\block\utils\SignText;
|
||||
use pocketmine\entity\animation\ConsumingItemAnimation;
|
||||
use pocketmine\entity\Attribute;
|
||||
@ -65,6 +66,7 @@ use pocketmine\network\mcpe\protocol\InteractPacket;
|
||||
use pocketmine\network\mcpe\protocol\InventoryTransactionPacket;
|
||||
use pocketmine\network\mcpe\protocol\ItemFrameDropItemPacket;
|
||||
use pocketmine\network\mcpe\protocol\LabTablePacket;
|
||||
use pocketmine\network\mcpe\protocol\LecternUpdatePacket;
|
||||
use pocketmine\network\mcpe\protocol\LevelSoundEventPacket;
|
||||
use pocketmine\network\mcpe\protocol\LevelSoundEventPacketV1;
|
||||
use pocketmine\network\mcpe\protocol\MapInfoRequestPacket;
|
||||
@ -104,6 +106,7 @@ use pocketmine\player\Player;
|
||||
use pocketmine\utils\AssumptionFailedError;
|
||||
use pocketmine\utils\Limits;
|
||||
use pocketmine\utils\TextFormat;
|
||||
use pocketmine\world\format\Chunk;
|
||||
use function array_push;
|
||||
use function base64_encode;
|
||||
use function count;
|
||||
@ -926,6 +929,35 @@ class InGamePacketHandler extends PacketHandler{
|
||||
return false; //TODO
|
||||
}
|
||||
|
||||
public function handleLecternUpdate(LecternUpdatePacket $packet) : bool{
|
||||
if($packet->dropBook){
|
||||
//Drop book is handled with an interact event on use item transaction
|
||||
return true;
|
||||
}
|
||||
|
||||
$pos = $packet->blockPosition;
|
||||
$chunkX = $pos->getX() >> Chunk::COORD_BIT_SIZE;
|
||||
$chunkZ = $pos->getZ() >> Chunk::COORD_BIT_SIZE;
|
||||
$world = $this->player->getWorld();
|
||||
if(!$world->isChunkLoaded($chunkX, $chunkZ) || $world->isChunkLocked($chunkX, $chunkZ)){
|
||||
return false;
|
||||
}
|
||||
|
||||
$lectern = $world->getBlockAt($pos->getX(), $pos->getY(), $pos->getZ());
|
||||
if($lectern instanceof Lectern && $this->player->canInteract($lectern->getPosition(), 15)){
|
||||
if($lectern->getViewedPage() === $packet->page){
|
||||
return true;
|
||||
}
|
||||
$book = $lectern->getBook();
|
||||
if($book !== null && count($book->getPages()) === $packet->totalPages && $packet->page >= 0 && $packet->page < $packet->totalPages){
|
||||
$world->setBlock($lectern->getPosition(), $lectern->setViewedPage($packet->page));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function handleNetworkStackLatency(NetworkStackLatencyPacket $packet) : bool{
|
||||
return true; //TODO: implement this properly - this is here to silence debug spam from MCPE dev builds
|
||||
}
|
||||
|
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user