Skip to content

Commit

Permalink
feat: make the cooking campfire work
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholass003 committed Mar 31, 2024
1 parent 008a836 commit 0293869
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 19 deletions.
5 changes: 2 additions & 3 deletions src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
use nicholass003\campfire\utils\CampfireRegistry;
use pocketmine\plugin\PluginBase;
use pocketmine\scheduler\AsyncTask;
use pocketmine\Server;
use pocketmine\utils\SingletonTrait;

class Loader extends PluginBase{
Expand All @@ -40,12 +39,12 @@ protected function onLoad() : void{
protected function onEnable() : void{
self::setInstance($this);

/*$this->getServer()->getAsyncPool()->addWorkerStartHook(function(int $worker) : void{
$this->getServer()->getAsyncPool()->addWorkerStartHook(function(int $worker) : void{
$this->getServer()->getAsyncPool()->submitTaskToWorker(new class extends AsyncTask{
public function onRun() : void{
CampfireRegistry::register();
}
}, $worker);
});*/
});
}
}
24 changes: 16 additions & 8 deletions src/block/Campfire.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use pocketmine\block\BlockTypeInfo;
use pocketmine\block\Transparent;
use pocketmine\block\utils\FacesOppositePlacingPlayerTrait;
use pocketmine\block\utils\HorizontalFacingTrait;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\item\Item;
use pocketmine\math\Vector3;
Expand All @@ -40,6 +41,7 @@

class Campfire extends Transparent{
use FacesOppositePlacingPlayerTrait;
use HorizontalFacingTrait;
use ExtinguishTrait;

public function __construct(BlockIdentifier $idInfo, string $name, BlockTypeInfo $typeInfo){
Expand All @@ -52,12 +54,13 @@ protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
}

public function getLightLevel() : int{
return $this->extinguished ? 15 : 0;
return $this->extinguished ? 0 : 15;
}

public function getDrops(Item $item) : array{
public function getDropsForCompatibleTool(Item $item) : array{
$tile = $this->position->getWorld()->getTile($this->position);
$drops = [];
$drops[] = ExtraVanillaBlocks::CAMPFIRE()->asItem();
if($tile instanceof TileCampfire){
foreach($tile->getItemCookQueue() as $slot => $id){
$item = CampfireFurnaceRecipe::matchItemDrop($id);
Expand All @@ -70,15 +73,19 @@ public function getDrops(Item $item) : array{
}

public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
$tile = $this->position->getWorld()->getTile($this->position);
$world = $this->position->getWorld();
$tile = $world->getTile($this->position);
if($tile instanceof TileCampfire){
if($tile->canCook($item)){
$cook = $tile->addItemCookQueue($item);
if($cook === false){
return false;
if($tile->addItemCookQueue($item)){
$item->pop();
$this->position->getWorld()->addSound($clickVector, new ItemFrameAddItemSound());
$world->scheduleDelayedBlockUpdate($this->position, 1);
$block = $world->getBlock($this->position);
if($block instanceof Campfire){
$world->setBlock($this->position, $block);
}
}
$item->pop();
$this->position->getWorld()->addSound($clickVector, new ItemFrameAddItemSound());
}
}
return true;
Expand All @@ -89,6 +96,7 @@ public function onScheduledUpdate() : void{
$tile = $world->getTile($this->position);
if($tile instanceof TileCampfire && $tile->onUpdate()){
$world->addSound($this->position, new CampfireSound());
$world->scheduleDelayedBlockUpdate($this->position, 1);
}
}
}
6 changes: 3 additions & 3 deletions src/block/ExtraVanillaBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
namespace nicholass003\campfire\block;

use nicholass003\campfire\block\tile\Campfire as TileCampfire;
use nicholass003\campfire\block\Campfire;
use pocketmine\block\Block;
use pocketmine\block\BlockBreakInfo;
use pocketmine\block\BlockIdentifier;
Expand All @@ -41,6 +40,7 @@
* @generate-registry-docblock
*
* @method static Campfire CAMPFIRE()
* @method static SoulCampfire SOUL_CAMPFIRE()
*/

final class ExtraVanillaBlocks{
Expand All @@ -62,7 +62,7 @@ public static function getAll() : array{
}

protected static function setup() : void{
$newId = BlockTypeIds::newId();
self::register("campfire", new Campfire(new BlockIdentifier($newId, TileCampfire::class), "Campfire", new BlockTypeInfo(BlockBreakInfo::axe(2.0, ToolTier::WOOD))));
self::register("campfire", new Campfire(new BlockIdentifier(BlockTypeIds::newId(), TileCampfire::class), "Campfire", new BlockTypeInfo(BlockBreakInfo::axe(2.0, ToolTier::WOOD))));
self::register("soul_campfire", new SoulCampfire(new BlockIdentifier(BlockTypeIds::newId(), TileCampfire::class), "Soul Campfire", new BlockTypeInfo(BlockBreakInfo::axe(2.0, ToolTier::WOOD))));
}
}
32 changes: 32 additions & 0 deletions src/block/SoulCampfire.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* Copyright (c) 2024 - present nicholass003
* _ _ _ ___ ___ ____
* (_) | | | | / _ \ / _ \___ \
* _ __ _ ___| |__ ___ | | __ _ ___ ___| | | | | | |__) |
* | '_ \| |/ __| '_ \ / _ \| |/ _` / __/ __| | | | | | |__ <
* | | | | | (__| | | | (_) | | (_| \__ \__ \ |_| | |_| |__) |
* |_| |_|_|\___|_| |_|\___/|_|\__,_|___/___/\___/ \___/____/
*
* 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 nicholass003
* @link https://github.com/nicholass003/
*
*
*/

declare(strict_types=1);

namespace nicholass003\campfire\block;

class SoulCampfire extends Campfire{

public function getLightLevel() : int{
return $this->extinguished ? 0 : 10;
}
}
9 changes: 9 additions & 0 deletions src/block/tile/Campfire.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
use nicholass003\campfire\utils\CampfireFurnaceRecipe;
use pocketmine\block\tile\Spawnable;
use pocketmine\item\Item;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\world\World;

class Campfire extends Spawnable{
use CampfireShelfTrait;
Expand All @@ -51,6 +53,11 @@ class Campfire extends Spawnable{

public const MAX_ITEMS = 4;

public function __construct(World $world, Vector3 $pos){
parent::__construct($world, $pos);
$world->scheduleDelayedBlockUpdate($pos, 1);
}

public function canCook(Item $item) : bool{
return in_array($item->getTypeId(), array_keys(CampfireFurnaceRecipe::RECIPES));
}
Expand All @@ -76,6 +83,8 @@ public function updateCookTime(int $index) : void{
$nbt = $this->saveNBT();
$nbt->removeTag(self::ITEM_SLOTS[$index]);
$nbt->removeTag(self::ITEM_TIMES[$index]);
$nbt->removeTag(self::TAG_ITEMS);
$nbt->removeTag(self::TAG_TIMES);
unset($this->items[$index]);
unset($this->times[$index]);
}
Expand Down
3 changes: 3 additions & 0 deletions src/block/tile/CampfireShelfTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ protected function saveItems(CompoundTag $tag) : void{
$items = [];
$times = [];
foreach($this->items as $index => $item){
if($item === null){
continue;
}
$items[] = $item->nbtSerialize($index);
$tag->setTag(Campfire::ITEM_SLOTS[$index], $item->nbtSerialize());
}
Expand Down
19 changes: 14 additions & 5 deletions src/utils/CampfireRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

use nicholass003\campfire\block\Campfire;
use nicholass003\campfire\block\ExtraVanillaBlocks;
use nicholass003\campfire\block\SoulCampfire;
use nicholass003\campfire\block\tile\Campfire as TileCampfire;
use pocketmine\block\Block;
use pocketmine\block\RuntimeBlockStateRegistry;
use pocketmine\block\tile\TileFactory;
use pocketmine\data\bedrock\block\BlockStateNames;
Expand All @@ -50,7 +50,8 @@ public static function register() : void{
}

private static function registerCampfire() : void{
self::registerBlock(ExtraVanillaBlocks::CAMPFIRE(), [BlockTypeNames::CAMPFIRE . "_block"]);
self::registerBlock(ExtraVanillaBlocks::CAMPFIRE(), [BlockTypeNames::CAMPFIRE]);
self::registerBlock(ExtraVanillaBlocks::SOUL_CAMPFIRE(), [BlockTypeNames::SOUL_CAMPFIRE]);
}

private static function mapBlockStateToObjectDeserializer() : void{
Expand All @@ -59,26 +60,34 @@ private static function mapBlockStateToObjectDeserializer() : void{
->setFacing($in->readCardinalHorizontalFacing())
->setExtinguished($in->readBool(BlockStateNames::EXTINGUISHED));
});
GlobalBlockStateHandlers::getDeserializer()->map(BlockTypeNames::SOUL_CAMPFIRE, function(BlockStateReader $in) : SoulCampfire{
return ExtraVanillaBlocks::SOUL_CAMPFIRE()
->setFacing($in->readCardinalHorizontalFacing())
->setExtinguished($in->readBool(BlockStateNames::EXTINGUISHED));
});
}

private static function mapBlockObjectToStateSerializer() : void{
GlobalBlockStateHandlers::getSerializer()->map(ExtraVanillaBlocks::CAMPFIRE(),
fn(Campfire $block) => self::encodeCampfire($block, BlockStateWriter::create(BlockTypeNames::CAMPFIRE))
);
GlobalBlockStateHandlers::getSerializer()->map(ExtraVanillaBlocks::SOUL_CAMPFIRE(),
fn(SoulCampfire $block) => self::encodeCampfire($block, BlockStateWriter::create(BlockTypeNames::SOUL_CAMPFIRE))
);
}

private static function encodeCampfire(Campfire $block, BlockStateWriter $out) : BlockStateWriter{
private static function encodeCampfire(Campfire|SoulCampfire $block, BlockStateWriter $out) : BlockStateWriter{
return $out
->writeCardinalHorizontalFacing($block->getFacing())
->writeBool(BlockStateNames::EXTINGUISHED, $block->isExtinguished());
}

private static function registerTile() : void{
$tileFactory = TileFactory::getInstance();
$tileFactory->register(TileCampfire::class, ["Campfire"]);
$tileFactory->register(TileCampfire::class, ["Campfire", "minecraft:campfire"]);
}

private static function registerBlock(Campfire $block, array $stringToItemParserNames) : void{
private static function registerBlock(Campfire|SoulCampfire $block, array $stringToItemParserNames) : void{
RuntimeBlockStateRegistry::getInstance()->register($block);
foreach($stringToItemParserNames as $name){
StringToItemParser::getInstance()->registerBlock($name, fn() => clone $block);
Expand Down

0 comments on commit 0293869

Please sign in to comment.