From e388ac9c8b133fbf0382ef56f6ab75f04249a670 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sat, 20 Jul 2019 17:48:09 +0100 Subject: [PATCH] implemented lantern --- src/pocketmine/block/BlockFactory.php | 2 +- src/pocketmine/block/BlockLegacyMetadata.php | 2 + src/pocketmine/block/Lantern.php | 80 ++++++++++++++++++++ src/pocketmine/block/VanillaBlocks.php | 2 + 4 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 src/pocketmine/block/Lantern.php diff --git a/src/pocketmine/block/BlockFactory.php b/src/pocketmine/block/BlockFactory.php index e67b2b646..839e39345 100644 --- a/src/pocketmine/block/BlockFactory.php +++ b/src/pocketmine/block/BlockFactory.php @@ -224,6 +224,7 @@ class BlockFactory{ self::register(new Trapdoor(new BID(Ids::IRON_TRAPDOOR), "Iron Trapdoor", new BlockBreakInfo(5.0, BlockToolType::PICKAXE, ToolTier::WOOD()->getHarvestLevel(), 25.0))); self::register(new ItemFrame(new BID(Ids::FRAME_BLOCK, 0, ItemIds::FRAME, TileItemFrame::class), "Item Frame")); self::register(new Ladder(new BID(Ids::LADDER), "Ladder")); + self::register(new Lantern(new BID(Ids::LANTERN), "Lantern", new BlockBreakInfo(5.0, BlockToolType::PICKAXE, ToolTier::WOOD()->getHarvestLevel()))); self::register(new Solid(new BID(Ids::LAPIS_BLOCK), "Lapis Lazuli Block", new BlockBreakInfo(3.0, BlockToolType::PICKAXE, ToolTier::STONE()->getHarvestLevel()))); self::register(new LapisOre(new BID(Ids::LAPIS_ORE), "Lapis Lazuli Ore")); self::register(new Lava(new BIDFlattened(Ids::FLOWING_LAVA, Ids::STILL_LAVA), "Lava")); @@ -599,7 +600,6 @@ class BlockFactory{ //TODO: minecraft:jigsaw //TODO: minecraft:jukebox //TODO: minecraft:kelp - //TODO: minecraft:lantern //TODO: minecraft:lava_cauldron //TODO: minecraft:lectern //TODO: minecraft:lit_blast_furnace diff --git a/src/pocketmine/block/BlockLegacyMetadata.php b/src/pocketmine/block/BlockLegacyMetadata.php index 899e93554..60a336c4d 100644 --- a/src/pocketmine/block/BlockLegacyMetadata.php +++ b/src/pocketmine/block/BlockLegacyMetadata.php @@ -95,6 +95,8 @@ interface BlockLegacyMetadata{ public const ITEM_FRAME_FLAG_HAS_MAP = 0x04; + public const LANTERN_FLAG_HANGING = 0x01; + public const LEAVES_FLAG_NO_DECAY = 0x04; public const LEAVES_FLAG_CHECK_DECAY = 0x08; diff --git a/src/pocketmine/block/Lantern.php b/src/pocketmine/block/Lantern.php new file mode 100644 index 000000000..98ca830ee --- /dev/null +++ b/src/pocketmine/block/Lantern.php @@ -0,0 +1,80 @@ +hanging = ($stateMeta & BlockLegacyMetadata::LANTERN_FLAG_HANGING) !== 0; + } + + protected function writeStateToMeta() : int{ + return $this->hanging ? BlockLegacyMetadata::LANTERN_FLAG_HANGING : 0; + } + + public function getStateBitmask() : int{ + return 0b1; + } + + public function getLightLevel() : int{ + return 15; + } + + protected function recalculateBoundingBox() : ?AxisAlignedBB{ + return AxisAlignedBB::one() + ->trim(Facing::UP, $this->hanging ? 6 / 16 : 8 / 16) + ->trim(Facing::DOWN, $this->hanging ? 2 / 16 : 0) + ->squash(Facing::AXIS_X, 5 / 16) + ->squash(Facing::AXIS_Z, 5 / 16); + } + + protected function canAttachTo(Block $b) : bool{ + return !$b->isTransparent(); + } + + public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ + if(!$this->canAttachTo($this->world->getBlock($blockReplace->up())) and !$this->canAttachTo($this->world->getBlock($blockReplace->down()))){ + return false; + } + + $this->hanging = ($face === Facing::DOWN or !$this->canAttachTo($this->world->getBlock($blockReplace->down()))); + return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player); + } + + public function onNearbyBlockChange() : void{ + if(!$this->canAttachTo($this->world->getBlock($this->hanging ? $this->up() : $this->down()))){ + $this->world->useBreakOn($this); + } + } +} diff --git a/src/pocketmine/block/VanillaBlocks.php b/src/pocketmine/block/VanillaBlocks.php index 5423d458c..d14c05149 100644 --- a/src/pocketmine/block/VanillaBlocks.php +++ b/src/pocketmine/block/VanillaBlocks.php @@ -410,6 +410,7 @@ use function assert; * @method static WoodenTrapdoor JUNGLE_TRAPDOOR() * @method static Wood JUNGLE_WOOD() * @method static Ladder LADDER() + * @method static Lantern LANTERN() * @method static Solid LAPIS_LAZULI() * @method static LapisOre LAPIS_LAZULI_ORE() * @method static DoubleTallGrass LARGE_FERN() @@ -1071,6 +1072,7 @@ final class VanillaBlocks{ self::register("jungle_trapdoor", BlockFactory::get(403)); self::register("jungle_wood", BlockFactory::get(467, 3)); self::register("ladder", BlockFactory::get(65, 2)); + self::register("lantern", BlockFactory::get(463)); self::register("lapis_lazuli", BlockFactory::get(22)); self::register("lapis_lazuli_ore", BlockFactory::get(21)); self::register("large_fern", BlockFactory::get(175, 3));