Implement ender chest (#1462)

This commit is contained in:
JackNoordhuis 2017-11-23 01:25:21 +11:00 committed by Dylan K. Taylor
parent 8d59843020
commit 686e1c4470
7 changed files with 260 additions and 1 deletions

View File

@ -774,6 +774,7 @@ class Server{
//new IntTag("SpawnZ", (int) $spawn->z),
//new ByteTag("SpawnForced", 1), //TODO
new ListTag("Inventory", [], NBT::TAG_Compound),
new ListTag("EnderChestInventory", [], NBT::TAG_Compound),
new CompoundTag("Achievements", []),
new IntTag("playerGameType", $this->getGamemode()),
new ListTag("Motion", [

View File

@ -199,7 +199,7 @@ class BlockFactory{
self::registerBlock(new CocoaBlock());
self::registerBlock(new SandstoneStairs());
self::registerBlock(new EmeraldOre());
//TODO: ENDER_CHEST
self::registerBlock(new EnderChest());
self::registerBlock(new TripwireHook());
self::registerBlock(new Tripwire());
self::registerBlock(new Emerald());

View File

@ -0,0 +1,113 @@
<?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\item\Item;
use pocketmine\item\ItemFactory;
use pocketmine\item\Tool;
use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Vector3;
use pocketmine\Player;
use pocketmine\tile\EnderChest as TileEnderChest;
use pocketmine\tile\Tile;
class EnderChest extends Chest{
protected $id = self::ENDER_CHEST;
public function getHardness() : float{
return 22.5;
}
public function getBlastResistance() : float{
return 3000;
}
public function getLightLevel() : int{
return 7;
}
public function getName() : string{
return "Ender Chest";
}
public function getToolType() : int{
return Tool::TYPE_PICKAXE;
}
public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $facePos, Player $player = null) : bool{
$faces = [
0 => 4,
1 => 2,
2 => 5,
3 => 3
];
$this->meta = $faces[$player instanceof Player ? $player->getDirection() : 0];
$this->getLevel()->setBlock($blockReplace, $this, true, true);
Tile::createTile(Tile::ENDER_CHEST, $this->getLevel(), TileEnderChest::createNBT($this, $face, $item, $player));
return true;
}
public function onBreak(Item $item, Player $player = null) : bool{
return Block::onBreak($item, $player);
}
public function onActivate(Item $item, Player $player = null) : bool{
if($player instanceof Player){
$t = $this->getLevel()->getTile($this);
$enderChest = null;
if($t instanceof TileEnderChest){
$enderChest = $t;
}else{
$enderChest = Tile::createTile(Tile::ENDER_CHEST, $this->getLevel(), TileEnderChest::createNBT($this));
}
if(!$this->getSide(Vector3::SIDE_UP)->isTransparent()){
return true;
}
$player->getEnderChestInventory()->setHolderPosition($enderChest);
$player->addWindow($player->getEnderChestInventory());
}
return true;
}
public function getDrops(Item $item) : array{
if($item->isPickaxe() >= Tool::TIER_WOODEN){
return [ItemFactory::get(Item::OBSIDIAN, 0, 8)];
}
return [];
}
public function getFuelTime() : int{
return 0;
}
}

View File

@ -27,6 +27,7 @@ use pocketmine\entity\projectile\ProjectileSource;
use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\event\entity\EntityRegainHealthEvent;
use pocketmine\event\player\PlayerExhaustEvent;
use pocketmine\inventory\EnderChestInventory;
use pocketmine\inventory\InventoryHolder;
use pocketmine\inventory\PlayerInventory;
use pocketmine\item\enchantment\Enchantment;
@ -54,6 +55,9 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
/** @var PlayerInventory */
protected $inventory;
/** @var EnderChestInventory */
protected $enderChestInventory;
/** @var UUID */
protected $uuid;
protected $rawUUID;
@ -309,6 +313,10 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
return $this->inventory;
}
public function getEnderChestInventory(){
return $this->enderChestInventory;
}
/**
* For Human entities which are not players, sets their properties such as nametag, skin and UUID from NBT.
*/
@ -334,6 +342,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
$this->setDataProperty(self::DATA_PLAYER_BED_POSITION, self::DATA_TYPE_POS, [0, 0, 0], false);
$this->inventory = new PlayerInventory($this);
$this->enderChestInventory = new EnderChestInventory($this);
$this->initHumanData();
$inventoryTag = $this->namedtag->getListTag("Inventory");
@ -352,6 +361,14 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
}
}
$enderChestInventoryTag = $this->namedtag->getListTag("EnderChestInventory");
if($enderChestInventoryTag !== null){
/** @var CompoundTag $item */
foreach($enderChestInventoryTag as $i => $item){
$this->enderChestInventory->setItem($item->getByte("Slot"), ItemItem::nbtDeserialize($item));
}
}
$this->inventory->setHeldItemIndex($this->namedtag->getInt("SelectedInventorySlot", 0), false);
parent::initEntity();
@ -483,6 +500,21 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
$this->namedtag->setInt("SelectedInventorySlot", $this->inventory->getHeldItemIndex());
}
if($this->enderChestInventory !== null){
/** @var CompoundTag[] $items */
$items = [];
$slotCount = $this->enderChestInventory->getSize();
for($slot = 0; $slot < $slotCount; ++$slot){
$item = $this->enderChestInventory->getItem($slot);
if(!$item->isNull()){
$items[] = $item->nbtSerialize($slot);
}
}
$this->namedtag->setTag(new ListTag("EnderChestInventory", $items, NBT::TAG_Compound));
}
if($this->skin !== null){
$this->namedtag->setTag(new CompoundTag("Skin", [
//TODO: save cape & geometry
@ -528,6 +560,10 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
$this->inventory->removeAllViewers(true);
$this->inventory = null;
}
if($this->enderChestInventory !== null){
$this->enderChestInventory->removeAllViewers(true);
$this->enderChestInventory = null;
}
parent::close();
}
}

View File

@ -0,0 +1,73 @@
<?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\entity\Human;
use pocketmine\level\Level;
use pocketmine\network\mcpe\protocol\BlockEventPacket;
use pocketmine\network\mcpe\protocol\LevelSoundEventPacket;
use pocketmine\network\mcpe\protocol\types\WindowTypes;
use pocketmine\Player;
use pocketmine\tile\EnderChest;
class EnderChestInventory extends ChestInventory{
/** @var FakeBlockMenu */
protected $holder;
public function __construct(Human $owner){
ContainerInventory::__construct(new FakeBlockMenu($this, $owner->getPosition()));
}
public function getNetworkType() : int{
return WindowTypes::CONTAINER;
}
public function getName() : string{
return "EnderChest";
}
public function getDefaultSize() : int{
return 27;
}
/**
* Set the holder's position to that of a tile
*
* @param EnderChest $enderChest
*/
public function setHolderPosition(EnderChest $enderChest){
$this->holder->setComponents($enderChest->getX(), $enderChest->getY(), $enderChest->getZ());
$this->holder->setLevel($enderChest->getLevel());
}
/**
* This override is here for documentation and code completion purposes only.
* @return FakeBlockMenu
*/
public function getHolder(){
return $this->holder;
}
}

View File

@ -0,0 +1,34 @@
<?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\tile;
use pocketmine\nbt\tag\CompoundTag;
class EnderChest extends Spawnable{
public function addAdditionalSpawnData(CompoundTag $nbt) : void{
}
}

View File

@ -54,6 +54,7 @@ abstract class Tile extends Position{
public const BREWING_STAND = "BrewingStand";
public const CHEST = "Chest";
public const ENCHANT_TABLE = "EnchantTable";
public const ENDER_CHEST = "EnderChest";
public const FLOWER_POT = "FlowerPot";
public const FURNACE = "Furnace";
public const ITEM_FRAME = "ItemFrame";
@ -89,6 +90,7 @@ abstract class Tile extends Position{
self::registerTile(Bed::class);
self::registerTile(Chest::class);
self::registerTile(EnchantTable::class);
self::registerTile(EnderChest::class);
self::registerTile(FlowerPot::class);
self::registerTile(Furnace::class);
self::registerTile(ItemFrame::class);