From 4449af22136b5e92bd1684dacc3b28d50964bdb1 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Tue, 4 Mar 2014 19:40:30 +0100 Subject: [PATCH] Implemented InventorySourceEntity into HumanEntity --- src/Entity.php | 3 +- src/entity/HumanEntity.php | 116 ++++++++++++++++++++++++++- src/entity/InventorySourceEntity.php | 49 +++++++++++ src/material/Item.php | 12 +++ src/math/AxisAlignedBB.php | 2 +- 5 files changed, 177 insertions(+), 5 deletions(-) create mode 100644 src/entity/InventorySourceEntity.php diff --git a/src/Entity.php b/src/Entity.php index d91a01a4f..f94db7349 100644 --- a/src/Entity.php +++ b/src/Entity.php @@ -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"); diff --git a/src/entity/HumanEntity.php b/src/entity/HumanEntity.php index 15d3f7f69..29882ddf2 100644 --- a/src/entity/HumanEntity.php +++ b/src/entity/HumanEntity.php @@ -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); + } } \ No newline at end of file diff --git a/src/entity/InventorySourceEntity.php b/src/entity/InventorySourceEntity.php new file mode 100644 index 000000000..c30d47f32 --- /dev/null +++ b/src/entity/InventorySourceEntity.php @@ -0,0 +1,49 @@ +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()); + } + } diff --git a/src/math/AxisAlignedBB.php b/src/math/AxisAlignedBB.php index b1d0b4409..c71670394 100644 --- a/src/math/AxisAlignedBB.php +++ b/src/math/AxisAlignedBB.php @@ -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),