Improved Item property handling

This commit is contained in:
Shoghi Cervantes 2014-12-07 15:51:00 +01:00
parent a98da3bab1
commit 7506f01302
17 changed files with 103 additions and 31 deletions

View File

@ -27,5 +27,7 @@ use pocketmine\entity\Entity;
abstract class Armor extends Item{
public $maxStackSize = 1;
public function getMaxStackSize(){
return 1;
}
}

View File

@ -27,6 +27,9 @@ class Bed extends Item{
public function __construct($meta = 0, $count = 1){
$this->block = Block::get(Item::BED_BLOCK);
parent::__construct(self::BED, 0, $count, "Bed");
$this->maxStackSize = 1;
}
public function getMaxStackSize(){
return 1;
}
}

View File

@ -25,7 +25,9 @@ namespace pocketmine\item;
class BeetrootSoup extends Item{
public function __construct($meta = 0, $count = 1){
parent::__construct(self::BEETROOT_SOUP, 0, $count, "Beetroot Soup");
$this->maxStackSize = 1;
}
public function getMaxStackSize(){
return 1;
}
}

View File

@ -31,10 +31,16 @@ use pocketmine\Player;
class Bucket extends Item{
public function __construct($meta = 0, $count = 1){
parent::__construct(self::BUCKET, $meta, $count, "Bucket");
$this->isActivable = true;
$this->maxStackSize = 1;
}
public function getMaxStackSize(){
return 1;
}
public function canBeActivated(){
return true;
}
public function onActivate(Level $level, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){
$targetBlock = Block::get($this->meta);

View File

@ -26,7 +26,10 @@ use pocketmine\block\Block;
class Cake extends Item{
public function __construct($meta = 0, $count = 1){
$this->block = Block::get(Item::CAKE_BLOCK);
$this->maxStackSize = 1;
parent::__construct(self::CAKE, 0, $count, "Cake");
}
public function getMaxStackSize(){
return 1;
}
}

View File

@ -30,9 +30,12 @@ use pocketmine\Player;
class FlintSteel extends Tool{
public function __construct($meta = 0, $count = 1){
parent::__construct(self::FLINT_STEEL, $meta, $count, "Flint and Steel");
$this->isActivable = true;
}
public function canBeActivated(){
return true;
}
public function onActivate(Level $level, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){
if(($player->gamemode & 0x01) === 0 and $this->useOn($block) and $this->getDamage() >= $this->getMaxDurability()){
$player->getInventory()->setItemInHand(new Item(Item::AIR, 0, 0));

View File

@ -27,6 +27,9 @@ class IronDoor extends Item{
public function __construct($meta = 0, $count = 1){
$this->block = Block::get(Item::IRON_DOOR_BLOCK);
parent::__construct(self::IRON_DOOR, 0, $count, "Iron Door");
$this->maxStackSize = 1;
}
public function getMaxStackSize(){
return 1;
}
}

View File

@ -373,7 +373,7 @@ class Item{
const CARROT = 391;
const CARROTS = 391;
const POTATO = 392;
const POTATOES = 392; //@shoghicp Why the heck do we need plural redundant Item ID here????
const POTATOES = 392;
const BAKED_POTATO = 393;
const BAKED_POTATOES = 393;
@ -396,10 +396,12 @@ class Item{
protected $id;
protected $meta;
public $count;
protected $maxStackSize = 64;
protected $durability = 0;
protected $name;
public $isActivable = false;
public function canBeActivated(){
return false;
}
public static function init(){
if(self::$list === null){
@ -425,6 +427,7 @@ class Item{
self::$list[self::SPAWN_EGG] = SpawnEgg::class;
self::$list[self::DIAMOND] = Diamond::class;
self::$list[self::STICK] = Stick::class;
self::$list[self::SNOWBALL] = Snowball::class;
self::$list[self::BOWL] = Bowl::class;
self::$list[self::FEATHER] = Feather::class;
self::$list[self::BRICK] = Brick::class;
@ -514,16 +517,16 @@ class Item{
if(!isset($b[1])){
$meta = 0;
}else{
$meta = ((int) $b[1]) & 0xFFFF;
$meta = $b[1] & 0xFFFF;
}
if(defined(Item::class . "::" . strtoupper($b[0]))){
$item = self::get(constant(Item::class . "::" . strtoupper($b[0])), $meta);
if($item->getId() === self::AIR and strtoupper($b[0]) !== "AIR"){
$item = self::get(((int) $b[0]) & 0xFFFF, $meta);
$item = self::get($b[0] & 0xFFFF, $meta);
}
}else{
$item = self::get(((int) $b[0]) & 0xFFFF, $meta);
$item = self::get($b[0] & 0xFFFF, $meta);
}
return $item;
@ -539,9 +542,6 @@ class Item{
$this->block = Block::get($this->id, $this->meta);
$this->name = $this->block->getName();
}
if($this->isTool() !== false){
$this->maxStackSize = 1;
}
}
@ -581,8 +581,8 @@ class Item{
$this->meta = $meta !== null ? $meta & 0xFFFF : null;
}
final public function getMaxStackSize(){
return $this->maxStackSize;
public function getMaxStackSize(){
return 64;
}
final public function getFuelTime(){

View File

@ -28,7 +28,7 @@ use pocketmine\block\Block;
*/
class ItemBlock extends Item{
public function __construct(Block $block, $meta = 0, $count = 1){
$this->block = clone $block;
$this->block = $block;
parent::__construct($block->getId(), $block->getDamage(), $count, $block->getName());
}

View File

@ -25,7 +25,9 @@ namespace pocketmine\item;
class MushroomStew extends Item{
public function __construct($meta = 0, $count = 1){
parent::__construct(self::MUSHROOM_STEW, 0, $count, "Mushroom Stew");
$this->maxStackSize = 1;
}
public function getMaxStackSize(){
return 1;
}
}

View File

@ -29,9 +29,12 @@ use pocketmine\Player;
class Painting extends Item{
public function __construct($meta = 0, $count = 1){
parent::__construct(self::PAINTING, 0, $count, "Painting");
$this->isActivable = true;
}
public function canBeActivated(){
return true;
}
public function onActivate(Level $level, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){
if($target->isTransparent() === false and $face > 1 and $block->isSolid() === false){
$faces = [

View File

@ -26,7 +26,10 @@ use pocketmine\block\Block;
class Sign extends Item{
public function __construct($meta = 0, $count = 1){
$this->block = Block::get(Item::SIGN_POST);
$this->maxStackSize = 16;
parent::__construct(self::SIGN, 0, $count, "Sign");
}
public function getMaxStackSize(){
return 16;
}
}

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/
*
*
*/
namespace pocketmine\item;
class Snowball extends Item{
public function __construct($meta = 0, $count = 1){
parent::__construct(self::SNOWBALL, 0, $count, "Snowball");
}
public function getMaxStackSize(){
return 16;
}
}

View File

@ -33,11 +33,13 @@ use pocketmine\Player;
class SpawnEgg extends Item{
public function __construct($meta = 0, $count = 1){
parent::__construct(self::SPAWN_EGG, 0, $count, "Spawn Egg");
$this->meta = $meta;
$this->isActivable = true;
parent::__construct(self::SPAWN_EGG, $meta, $count, "Spawn Egg");
}
public function canBeActivated(){
return true;
}
public function onActivate(Level $level, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){
$entity = null;
$chunk = $level->getChunk($block->getX() >> 4, $block->getZ() >> 4);

View File

@ -34,9 +34,12 @@ abstract class Tool extends Item{
public function __construct($id, $meta = 0, $count = 1, $name = "Unknown"){
parent::__construct($id, $meta, $count, $name);
$this->maxStackSize = 1;
}
public function getMaxStackSize(){
return 1;
}
/**
* TODO: Move this to each item
*

View File

@ -27,6 +27,9 @@ class WoodenDoor extends Item{
public function __construct($meta = 0, $count = 1){
$this->block = Block::get(Item::WOODEN_DOOR_BLOCK);
parent::__construct(self::WOODEN_DOOR, 0, $count, "Wooden Door");
$this->maxStackSize = 1;
}
public function getMaxStackSize(){
return 1;
}
}

View File

@ -1266,11 +1266,11 @@ class Level implements ChunkManager, Metadatable{
$this->server->getPluginManager()->callEvent($ev);
if(!$ev->isCancelled()){
$target->onUpdate(self::BLOCK_UPDATE_TOUCH);
if($target->isActivable === true and $target->onActivate($item, $player) === true){
if($target->canBeActivated() === true and $target->onActivate($item, $player) === true){
return true;
}
if($item->isActivable and $item->onActivate($this, $player, $block, $target, $face, $fx, $fy, $fz)){
if($item->canBeActivated() and $item->onActivate($this, $player, $block, $target, $face, $fx, $fy, $fz)){
if($item->getCount() <= 0){
$item = Item::get(Item::AIR, 0, 0);
@ -1278,7 +1278,7 @@ class Level implements ChunkManager, Metadatable{
}
}
}
}elseif($target->isActivable === true and $target->onActivate($item, $player) === true){
}elseif($target->canBeActivated() === true and $target->onActivate($item, $player) === true){
return true;
}