Working item frames

This commit is contained in:
Dylan K. Taylor
2016-12-14 18:30:40 +00:00
parent c4d4277a6c
commit 8c772fe671
6 changed files with 211 additions and 13 deletions

View File

@ -2,11 +2,11 @@
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* 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
@ -15,7 +15,7 @@
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*
*/
@ -236,6 +236,8 @@ class Block extends Position implements BlockIds, Metadatable{
self::$list[self::FENCE_GATE_DARK_OAK] = FenceGateDarkOak::class;
self::$list[self::FENCE_GATE_ACACIA] = FenceGateAcacia::class;
self::$list[self::ITEM_FRAME_BLOCK] = ItemFrame::class;
self::$list[self::GRASS_PATH] = GrassPath::class;
self::$list[self::PODZOL] = Podzol::class;

View File

@ -0,0 +1,150 @@
<?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/
*
*
*/
namespace pocketmine\block;
use pocketmine\item\Item;
use pocketmine\level\Level;
use pocketmine\nbt\tag\{
ByteTag,
CompoundTag,
FloatTag,
IntTag,
StringTag
};
use pocketmine\Player;
use pocketmine\tile\Tile;
use pocketmine\tile\ItemFrame as TileItemFrame;
class ItemFrame extends Flowable{
protected $id = Block::ITEM_FRAME_BLOCK;
public function __construct($meta = 0){
$this->meta = $meta;
}
public function getName(){
return "Item Frame";
}
public function canBeActivated(){
return true;
}
public function onActivate(Item $item, Player $player = null){
if(!(($tile = $this->level->getTile($this)) instanceof TileItemFrame)){
$nbt = new CompoundTag("", [
new StringTag("id", Tile::ITEM_FRAME),
new IntTag("x", $block->x),
new IntTag("y", $block->y),
new IntTag("z", $block->z),
new FloatTag("ItemDropChance", 1.0),
new ByteTag("ItemRotation", 0)
]);
$tile = Tile::createTile(Tile::ITEM_FRAME, $this->level->getChunk($this->x >> 4, $this->z >> 4), $nbt);
}
if($tile->hasItem()){
$tile->setItemRotation(($tile->getItemRotation() + 1) % 8);
}else{
if($item->getCount() > 0){
$frameItem = clone $item;
$frameItem->setCount(1);
$item->setCount($item->getCount() - 1);
$tile->setItem($frameItem);
if($player instanceof Player and $player->isSurvival()){
$player->getInventory()->setItemInHand($item->getCount() <= 0 ? Item::get(Item::AIR) : $item);
}
}
}
return true;
}
public function onBreak(Item $item){
if(($tile = $this->level->getTile($this)) instanceof TileItemFrame){
//TODO: add events
if(lcg_value() <= $tile->getItemDropChance() and $tile->getItem()->getId() !== Item::AIR){
$this->level->dropItem($tile->getBlock(), $tile->getItem());
}
}
return parent::onBreak($item);
}
public function onUpdate($type){
if($type === Level::BLOCK_UPDATE_NORMAL){
$sides = [
0 => 4,
1 => 5,
2 => 2,
3 => 3
];
if(!$this->getSide($sides[$this->meta])->isSolid()){
$this->level->useBreakOn($this);
return Level::BLOCK_UPDATE_NORMAL;
}
}
return false;
}
public function place(Item $item, Block $block, Block $target, $face, $fx, $fy, $fz, Player $player = null){
if($face === 0 or $face === 1){
return false;
}
$faces = [
2 => 3,
3 => 2,
4 => 1,
5 => 0
];
$this->meta = $faces[$face];
$this->level->setBlock($block, $this, true, true);
$nbt = new CompoundTag("", [
new StringTag("id", Tile::ITEM_FRAME),
new IntTag("x", $block->x),
new IntTag("y", $block->y),
new IntTag("z", $block->z),
new FloatTag("ItemDropChance", 1.0),
new ByteTag("ItemRotation", 0)
]);
if($item->hasCustomBlockData()){
foreach($item->getCustomBlockData() as $key => $v){
$nbt->{$key} = $v;
}
}
Tile::createTile(Tile::ITEM_FRAME, $this->level->getChunk($this->x >> 4, $this->z >> 4), $nbt);
return true;
}
public function getDrops(Item $item){
return [
[Item::ITEM_FRAME, 0, 1]
];
}
}