Added Villager spawn egg

This commit is contained in:
Shoghi Cervantes 2014-08-16 14:25:58 +02:00
parent c675605014
commit 52d28795fa
7 changed files with 110 additions and 30 deletions

View File

@ -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

View File

@ -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 [];
}
}

View File

@ -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 = [];

View File

@ -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"];
}
}

View File

@ -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;
}
}

View File

@ -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);

View File

@ -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 = [];
}