From 52d28795fab67862393c4b4e638a5bbaec5fb3e6 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Sat, 16 Aug 2014 14:25:58 +0200 Subject: [PATCH] Added Villager spawn egg --- src/pocketmine/block/Block.php | 3 +- src/pocketmine/block/TallGrass.php | 24 +----- src/pocketmine/entity/Entity.php | 4 +- src/pocketmine/entity/Villager.php | 75 +++++++++++++++++-- src/pocketmine/entity/Zombie.php | 24 ++++++ src/pocketmine/item/SpawnEgg.php | 7 +- .../level/format/mcregion/McRegion.php | 3 + 7 files changed, 110 insertions(+), 30 deletions(-) diff --git a/src/pocketmine/block/Block.php b/src/pocketmine/block/Block.php index 77b1ff017..c49771dc2 100644 --- a/src/pocketmine/block/Block.php +++ b/src/pocketmine/block/Block.php @@ -24,6 +24,7 @@ */ namespace pocketmine\block; +use pocketmine\entity\Villager; use pocketmine\entity\Zombie; use pocketmine\item\Item; use pocketmine\level\Level; @@ -449,7 +450,7 @@ abstract class Block extends Position implements Metadatable{ [Item::CLOCK, 0], [Item::COMPASS, 0], [Item::MINECART, 0], - //TODO: Villager + [Item::SPAWN_EGG, Villager::NETWORK_ID], //[Item::SPAWN_EGG, 10], //Chicken //[Item::SPAWN_EGG, 11], //Cow //[Item::SPAWN_EGG, 12], //Pig diff --git a/src/pocketmine/block/TallGrass.php b/src/pocketmine/block/TallGrass.php index 2ac85d9cc..4e4ccbf38 100644 --- a/src/pocketmine/block/TallGrass.php +++ b/src/pocketmine/block/TallGrass.php @@ -50,28 +50,10 @@ class TallGrass extends Flowable{ } public function getDrops(Item $item){ - $drops = []; - $possibleDrops = array( - array(Item::WHEAT_SEEDS, 0, 1), - array(Item::CARROT, 0, 1), - array(Item::POTATO, 0, 1), - array(Item::BEETROOT_SEEDS, 0, 1), - array(Item::MELON_SEEDS, 0, 1), - array(Item::PUMPKIN_SEEDS, 0, 1), - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - ); - if(($item = $possibleDrops[mt_rand(0, count($possibleDrops) - 1)]) !== 0){ - $drops[] = $item; + if(mt_rand(0, 15) === 0){ + return [Item::WHEAT_SEEDS, 0, 1]; } - - return $drops; + return []; } } \ No newline at end of file diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index bc202163e..7648d475d 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -158,7 +158,7 @@ abstract class Entity extends Position implements Metadatable{ $this->justCreated = true; $this->namedtag = $nbt; $this->chunk = $chunk; - $this->setLevel($chunk->getProvider()->getLevel(), true); //Create a hard reference + $this->setLevel($chunk->getProvider()->getLevel()); //Create a hard reference $this->server = $chunk->getProvider()->getLevel()->getServer(); $this->boundingBox = new AxisAlignedBB(0, 0, 0, 0, 0, 0); @@ -667,7 +667,7 @@ abstract class Entity extends Position implements Metadatable{ $this->getLevel()->freeAllChunks($this); } } - $this->setLevel($targetLevel, true); //Hard reference + $this->setLevel($targetLevel, $this instanceof Player ? true : false); //Hard reference $this->getLevel()->addEntity($this); if($this instanceof Player){ $this->usedChunks = []; diff --git a/src/pocketmine/entity/Villager.php b/src/pocketmine/entity/Villager.php index 27d324de8..1d026747a 100644 --- a/src/pocketmine/entity/Villager.php +++ b/src/pocketmine/entity/Villager.php @@ -22,12 +22,73 @@ namespace pocketmine\entity; +use pocketmine\nbt\tag\Int; +use pocketmine\network\protocol\AddMobPacket; +use pocketmine\network\protocol\SetEntityMotionPacket; +use pocketmine\Player; + class Villager extends Creature implements NPC, Ageable{ - const FARMER = 0; - const LIBRARIAN = 1; - const PRIEST = 2; - const BLACKSMITH = 3; - const BUTCHER = 4; + const PROFESSION_FARMER = 0; + const PROFESSION_LIBRARIAN = 1; + const PROFESSION_PRIEST = 2; + const PROFESSION_BLACKSMITH = 3; + const PROFESSION_BUTCHER = 4; + const PROFESSION_GENERIC = 5; + + const NETWORK_ID = 15; + + public $width = 0.6; + public $length = 0.6; + public $height = 1.8; + + public function getName(){ + return "Villager"; + } + + protected function initEntity(){ + parent::initEntity(); + if(!isset($this->namedtag->Profession)){ + $this->setProfession(self::PROFESSION_GENERIC); + } + } + + public function spawnTo(Player $player){ + if($player !== $this and !isset($this->hasSpawned[$player->getID()])){ + $this->hasSpawned[$player->getID()] = $player; + + $pk = new AddMobPacket(); + $pk->eid = $this->getID(); + $pk->type = Villager::NETWORK_ID; + $pk->x = $this->x; + $pk->y = $this->y; + $pk->z = $this->z; + $pk->yaw = $this->yaw; + $pk->pitch = $this->pitch; + $pk->metadata = $this->getData(); + $player->dataPacket($pk); + + $pk = new SetEntityMotionPacket(); + $pk->entities = [ + [$this->getID(), $this->motionX, $this->motionY, $this->motionZ] + ]; + $player->dataPacket($pk); + } + } + + public function getData(){ //TODO + $flags = 0; + $flags |= $this->fireTicks > 0 ? 1 : 0; + //$flags |= ($this->crouched === true ? 0b10:0) << 1; + //$flags |= ($this->inAction === true ? 0b10000:0); + $d = array( + 0 => array("type" => 0, "value" => $flags), + 1 => array("type" => 1, "value" => $this->airTicks), + 16 => array("type" => 0, "value" => 0), + 17 => array("type" => 6, "value" => array(0, 0, 0)), + ); + + return $d; + } /** * Sets the villager profession @@ -35,6 +96,10 @@ class Villager extends Creature implements NPC, Ageable{ * @param $profession */ public function setProfession($profession){ + $this->namedtag->Profession = new Int("Profession", $profession); + } + public function getProfession(){ + return $this->namedtag["Profession"]; } } \ No newline at end of file diff --git a/src/pocketmine/entity/Zombie.php b/src/pocketmine/entity/Zombie.php index 38b769f50..67e0ebbfe 100644 --- a/src/pocketmine/entity/Zombie.php +++ b/src/pocketmine/entity/Zombie.php @@ -22,6 +22,8 @@ namespace pocketmine\entity; +use pocketmine\event\entity\EntityDamageByEntityEvent; +use pocketmine\item\Item; use pocketmine\network\protocol\AddMobPacket; use pocketmine\network\protocol\SetEntityMotionPacket; use pocketmine\Player; @@ -74,4 +76,26 @@ class Zombie extends Monster{ return $d; } + + public function getDrops(){ + $drops = [ + Item::get(Item::FEATHER, 0, 1) + ]; + if($this->lastDamageCause instanceof EntityDamageByEntityEvent and $this->lastDamageCause->getEntity() instanceof Player){ + if(mt_rand(0, 199) < 5){ + switch(mt_rand(0, 2)){ + case 0: + $drops[] = Item::get(Item::IRON_INGOT, 0, 1); + break; + case 1: + $drops[] = Item::get(Item::CARROT, 0, 1); + break; + case 2: + $drops[] = Item::get(Item::POTATO, 0, 1); + break; + } + } + } + return $drops; + } } \ No newline at end of file diff --git a/src/pocketmine/item/SpawnEgg.php b/src/pocketmine/item/SpawnEgg.php index 1c640e2fe..56a602d1b 100644 --- a/src/pocketmine/item/SpawnEgg.php +++ b/src/pocketmine/item/SpawnEgg.php @@ -23,6 +23,7 @@ namespace pocketmine\item; use pocketmine\block\Block; use pocketmine\entity\Entity; +use pocketmine\entity\Villager; use pocketmine\entity\Zombie; use pocketmine\level\format\FullChunk; use pocketmine\level\Level; @@ -51,7 +52,7 @@ class SpawnEgg extends Item{ $nbt = new Compound("", [ "Pos" => new Enum("Pos", [ new Double("", $block->getX()), - new Double("", $block->getY() + 1), + new Double("", $block->getY()), new Double("", $block->getZ()) ]), //TODO: add random motion with physics @@ -67,6 +68,10 @@ class SpawnEgg extends Item{ ]); switch($this->meta){ + case Villager::NETWORK_ID: + $nbt->Health = new Short("Health", 20); + $entity = new Villager($chunk, $nbt); + break; case Zombie::NETWORK_ID: $nbt->Health = new Short("Health", 20); $entity = new Zombie($chunk, $nbt); diff --git a/src/pocketmine/level/format/mcregion/McRegion.php b/src/pocketmine/level/format/mcregion/McRegion.php index 1be3e6727..e4649ced2 100644 --- a/src/pocketmine/level/format/mcregion/McRegion.php +++ b/src/pocketmine/level/format/mcregion/McRegion.php @@ -144,6 +144,9 @@ class McRegion extends BaseLevelProvider{ } public function unloadChunks(){ + foreach($this->chunks as $chunk){ + $this->unloadChunk($chunk->getX(), $chunk->getZ(), false); + } $this->chunks = []; }