Implemented InventorySourceEntity into HumanEntity

This commit is contained in:
Shoghi Cervantes 2014-03-04 19:40:30 +01:00
parent f6589db064
commit 4449af2213
5 changed files with 177 additions and 5 deletions

View File

@ -154,7 +154,7 @@ abstract class Entity extends Position{
}
$timeNow = microtime(true);
$this->ticksLived += ($now - $this->lastUpdate) * 20;
$this->ticksLived += ($timeNow - $this->lastUpdate) * 20;
if($this->handleWaterMovement()){
$this->fallDistance = 0;
@ -490,6 +490,7 @@ abstract class Entity extends Position{
/***REM_START***/
require_once("entity/DamageableEntity.php");
require_once("entity/ProjectileSourceEntity.php");
require_once("entity/InventorySourceEntity.php");
require_once("entity/RideableEntity.php");
require_once("entity/TameableEntity.php");
require_once("entity/AttachableEntity.php");

View File

@ -19,9 +19,10 @@
*
*/
class HumanEntity extends CreatureEntity implements ProjectileSourceEntity{
class HumanEntity extends CreatureEntity implements ProjectileSourceEntity, InventorySourceEntity{
protected $nameTag;
protected $nameTag = "TESTIFICATE";
protected $inventory = array();
protected function initEntity(){
if(isset($this->namedtag->NameTag)){
@ -106,7 +107,7 @@ class HumanEntity extends CreatureEntity implements ProjectileSourceEntity{
}
}*/
return $d;
}
}
public function attack($damage, $source = "generic"){
@ -115,4 +116,113 @@ class HumanEntity extends CreatureEntity implements ProjectileSourceEntity{
public function heal($amount, $source = "generic"){
}
public function hasItem(Item $item, $checkDamage = true){
foreach($this->inventory as $s => $i){
if($i->equals($item, $checkDamage)){
return $i;
}
}
return false;
}
public function canAddItem(Item $item){
$inv = $this->inventory;
while($item->getCount() > 0){
$add = 0;
foreach($inv as $s => $i){
if($i->getID() === AIR){
$add = min($i->getMaxStackSize(), $item->getCount());
$inv[$s] = clone $item;
$inv[$s]->setCount($add);
break;
}elseif($i->equals($item)){
$add = min($i->getMaxStackSize() - $i->getCount(), $item->getCount());
if($add <= 0){
continue;
}
$inv[$s] = clone $item;
$inv[$s]->setCount($i->getCount() + $add);
break;
}
}
if($add <= 0){
return false;
}
$item->setCount($item->getCount() - $add);
}
return true;
}
public function addItem(Item $item){
while($item->getCount() > 0){
$add = 0;
foreach($this->inventory as $s => $i){
if($i->getID() === AIR){
$add = min($i->getMaxStackSize(), $item->getCount());
$i2 = clone $item;
$i2->setCount($add);
$this->setSlot($s, $i2);
break;
}elseif($i->equals($item)){
$add = min($i->getMaxStackSize() - $i->getCount(), $item->getCount());
if($add <= 0){
continue;
}
$i2 = clone $item;
$i2->setCount($i->getCount() + $add);
$this->setSlot($s, $i2);
break;
}
}
if($add <= 0){
return false;
}
$item->setCount($item->getCount() - $add);
}
return true;
}
public function canRemoveItem(Item $item, $checkDamage = true){
return $this->hasItem($item, $checkDamage);
}
public function removeItem(Item $item, $checkDamage = true){
while($item->getCount() > 0){
$remove = 0;
foreach($this->inventory as $s => $i){
if($i->equals($item, $checkDamage)){
$remove = min($item->getCount(), $i->getCount());
if($item->getCount() < $i->getCount()){
$i->setCount($i->getCount() - $item->getCount());
$this->setSlot($s, $i);
}else{
$this->setSlot($s, BlockAPI::getItem(AIR, 0, 0));
}
break;
}
}
if($remove <= 0){
return false;
}
$item->setCount($item->getCount() - $remove);
}
return true;
}
public function setSlot($slot, Item $item){
$this->inventory[(int) $slot] = $item;
}
public function getSlot($slot){
$this->inventory[(int) $slot] = $item;
}
public function getAllSlots(){
return $this->inventory;
}
public function getSlotCount(){
return count($this->inventory);
}
}

View File

@ -0,0 +1,49 @@
<?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/
*
*
*/
interface InventorySourceEntity{
public function hasItem(Item $item, $checkDamage = true);
public function canAddItem(Item $item);
/**
* @return boolean hasBeenAdded
*/
public function addItem(Item $item);
public function canRemoveItem(Item $item, $checkDamage = true);
/**
* @return boolean hasBeenRemoved
*/
public function removeItem(Item $item, $checkDamage = true);
public function getSlotCount();
public function getAllSlots();
public function getSlot($slot);
public function setSlot($slot, Item $item);
}

View File

@ -80,6 +80,14 @@ class Item{
}
}
public function getCount(){
return $this->count;
}
public function setCount($count){
$this->count = (int) $count;
}
final public function getName(){
return $this->name;
}
@ -282,4 +290,8 @@ class Item{
return false;
}
public final function equals(Item $item, $checkDamage = false){
return $this->id === $item->getID() and ($checkDamage === false or $this->getMetadata() === $item->getMetadata());
}
}

View File

@ -101,7 +101,7 @@ class AxisAlignedBB{
return $vec;
}
public function getMixedBoundingBox(AxisAlignedBB $bb){
public function setBB(AxisAlignedBB $bb){
return new AxisAlignedBB(
min($this->minX, $bb->minX),
min($this->minY, $bb->minY),