partial implementation of hopper (just enough to load/save all data)

This commit is contained in:
Dylan K. Taylor 2019-05-31 19:32:19 +01:00
parent e091cfe1de
commit ad79e6cd8e
6 changed files with 215 additions and 1 deletions

View File

@ -36,6 +36,7 @@ use pocketmine\block\tile\EnchantTable as TileEnchantingTable;
use pocketmine\block\tile\EnderChest as TileEnderChest;
use pocketmine\block\tile\FlowerPot as TileFlowerPot;
use pocketmine\block\tile\Furnace as TileFurnace;
use pocketmine\block\tile\Hopper as TileHopper;
use pocketmine\block\tile\ItemFrame as TileItemFrame;
use pocketmine\block\tile\MonsterSpawner as TileMonsterSpawner;
use pocketmine\block\tile\Note as TileNote;
@ -178,6 +179,7 @@ class BlockFactory{
self::register(new HardenedGlass(new BID(Ids::HARD_GLASS), "Hardened Glass"));
self::register(new HardenedGlassPane(new BID(Ids::HARD_GLASS_PANE), "Hardened Glass Pane"));
self::register(new HayBale(new BID(Ids::HAY_BALE), "Hay Bale"));
self::register(new Hopper(new BID(Ids::HOPPER_BLOCK, 0, ItemIds::HOPPER, TileHopper::class), "Hopper", new BlockBreakInfo(3.0, BlockToolType::TYPE_PICKAXE, TieredTool::TIER_WOODEN, 15.0)));
self::register(new Ice(new BID(Ids::ICE), "Ice"));
self::register(new class(new BID(Ids::MONSTER_EGG, Meta::INFESTED_STONE), "Infested Stone") extends InfestedStone{
public function getSilkTouchDrops(Item $item) : array{

View File

@ -84,6 +84,8 @@ interface BlockLegacyMetadata{
public const FLOWER_POT_FLAG_OCCUPIED = 0x01;
public const HOPPER_FLAG_POWERED = 0x08;
public const INFESTED_STONE = 0;
public const INFESTED_COBBLESTONE = 1;
public const INFESTED_STONE_BRICK = 2;

View File

@ -0,0 +1,88 @@
<?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\Hopper as TileHopper;
use pocketmine\block\utils\BlockDataValidator;
use pocketmine\block\utils\InvalidBlockStateException;
use pocketmine\item\Item;
use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Facing;
use pocketmine\math\Vector3;
use pocketmine\Player;
class Hopper extends Transparent{
/** @var int */
private $facing = Facing::DOWN;
/** @var bool */
private $powered = false;
public function readStateFromData(int $id, int $stateMeta) : void{
$facing = BlockDataValidator::readFacing($stateMeta & 0x07);
if($facing === Facing::UP){
throw new InvalidBlockStateException("Hopper may not face upward");
}
$this->facing = $facing;
$this->powered = ($stateMeta & BlockLegacyMetadata::HOPPER_FLAG_POWERED) !== 0;
}
protected function writeStateToMeta() : int{
return $this->facing | ($this->powered ? BlockLegacyMetadata::HOPPER_FLAG_POWERED : 0);
}
public function getStateBitmask() : int{
return 0b1111;
}
protected function recalculateCollisionBoxes() : array{
$result = [
AxisAlignedBB::one()->trim(Facing::UP, 6 / 16) //the empty area around the bottom is currently considered solid
];
foreach(Facing::HORIZONTAL as $f){ //add the frame parts around the bowl
$result[] = AxisAlignedBB::one()->trim($f, 14 / 16);
}
return $result;
}
public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
$this->facing = $face === Facing::DOWN ? Facing::DOWN : Facing::opposite($face);
return parent::place($item, $blockReplace, $blockClicked, $face, $clickVector, $player);
}
public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
if($player !== null){
$tile = $this->world->getTile($this);
if($tile instanceof TileHopper){ //TODO: find a way to have inventories open on click without this boilerplate in every block
$player->addWindow($tile->getInventory());
}
return true;
}
return false;
}
//TODO: redstone logic, sucking logic
}

View File

@ -0,0 +1,84 @@
<?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\inventory\HopperInventory;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\world\World;
class Hopper extends Spawnable implements Container, Nameable{
use ContainerTrait;
use NameableTrait;
private const TAG_TRANSFER_COOLDOWN = "TransferCooldown";
/** @var HopperInventory */
private $inventory;
/** @var int */
private $transferCooldown = 0;
public function __construct(World $world, Vector3 $pos){
$this->inventory = new HopperInventory($this);
parent::__construct($world, $pos);
}
public function readSaveData(CompoundTag $nbt) : void{
$this->loadItems($nbt);
$this->loadName($nbt);
$this->transferCooldown = $nbt->getInt(self::TAG_TRANSFER_COOLDOWN, 0);
}
protected function writeSaveData(CompoundTag $nbt) : void{
$this->saveItems($nbt);
$this->saveName($nbt);
$nbt->setInt(self::TAG_TRANSFER_COOLDOWN, $this->transferCooldown);
}
public function getDefaultName() : string{
return "Hopper";
}
/**
* @return HopperInventory
*/
public function getInventory(){
return $this->inventory;
}
/**
* @return HopperInventory
*/
public function getRealInventory(){
return $this->inventory;
}
public function onUpdate() : bool{
return false; //TODO
}
}

View File

@ -55,6 +55,7 @@ final class TileFactory{
self::register(EnderChest::class, ["EnderChest", "minecraft:ender_chest"]);
self::register(FlowerPot::class, ["FlowerPot", "minecraft:flower_pot"]);
self::register(Furnace::class, ["Furnace", "minecraft:furnace"]);
self::register(Hopper::class, ["Hopper", "minecraft:hopper"]);
self::register(ItemFrame::class, ["ItemFrame"]); //this is an entity in PC
self::register(MonsterSpawner::class, ["MobSpawner", "minecraft:mob_spawner"]);
self::register(Note::class, ["Music", "minecraft:noteblock"]);
@ -76,7 +77,6 @@ final class TileFactory{
//TODO: Dropper
//TODO: EndGateway
//TODO: EndPortal
//TODO: Hopper
//TODO: JigsawBlock
//TODO: Jukebox
//TODO: Lectern

View File

@ -0,0 +1,38 @@
<?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\inventory;
use pocketmine\math\Vector3;
use pocketmine\network\mcpe\protocol\types\WindowTypes;
class HopperInventory extends ContainerInventory{
public function __construct(Vector3 $holder, int $size = 5, array $items = []){
parent::__construct($holder, $size, $items);
}
public function getNetworkType() : int{
return WindowTypes::HOPPER;
}
}