Skip to content

Commit

Permalink
VanillaHopper: Our hopper block is now correctly used in crafting rec…
Browse files Browse the repository at this point in the history
…ipes and the creative inventory
  • Loading branch information
ColinHDev committed Jul 2, 2023
1 parent c3b9bac commit 8650fcf
Showing 1 changed file with 49 additions and 4 deletions.
53 changes: 49 additions & 4 deletions src/ColinHDev/VanillaHopper/VanillaHopper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace ColinHDev\VanillaHopper;

use Closure;
use ColinHDev\VanillaHopper\blocks\Hopper;
use ColinHDev\VanillaHopper\blocks\tiles\Hopper as TileHopper;
use ColinHDev\VanillaHopper\entities\ItemEntity;
Expand All @@ -22,14 +23,24 @@
use pocketmine\block\RuntimeBlockStateRegistry;
use pocketmine\block\tile\TileFactory;
use pocketmine\block\VanillaBlocks;
use pocketmine\crafting\CraftingManagerFromDataHelper;
use pocketmine\data\bedrock\item\ItemDeserializer;
use pocketmine\data\bedrock\item\ItemSerializer;
use pocketmine\data\bedrock\item\ItemTypeNames;
use pocketmine\data\bedrock\item\SavedItemData;
use pocketmine\data\SavedDataLoadingException;
use pocketmine\entity\EntityDataHelper;
use pocketmine\entity\EntityFactory;
use pocketmine\inventory\CreativeInventory;
use pocketmine\item\Item;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\plugin\PluginBase;
use pocketmine\Server;
use pocketmine\utils\SingletonTrait;
use pocketmine\world\format\io\GlobalItemDataHandlers;
use pocketmine\world\World;
use ReflectionClass;
use Symfony\Component\Filesystem\Path;
use function mb_strtoupper;

class VanillaHopper extends PluginBase {
Expand All @@ -45,14 +56,14 @@ public function onEnable() : void {
EntityFactory::getInstance()->register(
ItemEntity::class,
function (World $world, CompoundTag $nbt) : ItemEntity {
$itemTag = $nbt->getCompoundTag("Item");
$itemTag = $nbt->getCompoundTag(ItemEntity::TAG_ITEM);
if($itemTag === null){
throw new \UnexpectedValueException("Expected \"Item\" NBT tag not found");
throw new SavedDataLoadingException("Expected \"" . ItemEntity::TAG_ITEM . "\" NBT tag not found");
}

$item = Item::nbtDeserialize($itemTag);
if($item->isNull()){
throw new \UnexpectedValueException("Item is invalid");
throw new SavedDataLoadingException("Item is invalid");
}
return new ItemEntity(EntityDataHelper::parseLocation($nbt, $world), $item, $nbt);
},
Expand Down Expand Up @@ -93,9 +104,43 @@ private function registerHopperBlock() : void {
$reflection = new ReflectionClass(VanillaBlocks::class);
/** @var array<string, Block> $blocks */
$blocks = $reflection->getStaticPropertyValue("members");
$blocks[mb_strtoupper("hopper")] = $newHopper;
$blocks[mb_strtoupper("hopper")] = clone $newHopper;
$reflection->setStaticPropertyValue("members", $blocks);

/**
* Overwriting the entry in the ItemDeserializer and ItemSerializer by calling our custom version of their
* {@see ItemDeserializer::map()} and {@see ItemSerializer::map()} methods without prohibiting the
* overwriting of existing entries
*/
(function(string $id, Closure $deserializer) : void {
$this->deserializers[$id] = $deserializer;
})->call(
GlobalItemDataHandlers::getDeserializer(),
ItemTypeNames::HOPPER,
fn (SavedItemData $data) => $newHopper->asItem()
);
(function(Block $block, Closure $serializer) : void {
$this->blockItemSerializers[$block->getTypeId()] = $serializer;
})->call(
GlobalItemDataHandlers::getSerializer(),
$newHopper,
fn() => new SavedItemData(ItemTypeNames::HOPPER)
);

/**
* Recreating the creative inventory, so that it includes our custom hopper item instance.
*/
CreativeInventory::reset();
CreativeInventory::getInstance();

/**
* Overwriting the servers crafting manager, so that all recipes using the old hopper item are replaced and
* use the new hopper item instead.
*/
(function() : void {
$this->craftingManager = CraftingManagerFromDataHelper::make(Path::join(\pocketmine\BEDROCK_DATA_PATH, "recipes"));
})->call(Server::getInstance());

/*GlobalBlockStateHandlers::getDeserializer()->map(
BlockTypeNames::HOPPER,
function(BlockStateReader $in) use($newHopper) : Block {
Expand Down

0 comments on commit 8650fcf

Please sign in to comment.