Skip to content

Commit

Permalink
Implemented health, events, damage, motion push
Browse files Browse the repository at this point in the history
  • Loading branch information
shoghicp committed Jul 6, 2014
1 parent 25ecdcf commit 6552397
Show file tree
Hide file tree
Showing 15 changed files with 702 additions and 123 deletions.
285 changes: 189 additions & 96 deletions src/pocketmine/Player.php

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions src/pocketmine/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -1379,11 +1379,17 @@ public function __construct(\SplClassLoader $autoloader, \ThreadedLogger $logger
$version = new VersionString($this->getPocketMineVersion());
$this->logger->info("Starting Minecraft: PE server version " . TextFormat::AQUA . $this->getVersion());

$this->logger->info("Loading properties...");
$this->logger->info("Loading pocketmine.yml...");
if(!file_exists($this->dataPath . "pocketmine.yml")){
@file_put_contents($this->dataPath . "pocketmine.yml", file_get_contents($this->filePath . "src/pocketmine/resources/pocketmine.yml"));
$content = file_get_contents($this->filePath . "src/pocketmine/resources/pocketmine.yml");
if($version->isDev()){
$content = str_replace("preferred-channel: stable", "preferred-channel: beta", $content);
}
@file_put_contents($this->dataPath . "pocketmine.yml", $content);
}
$this->config = new Config($this->dataPath . "pocketmine.yml", Config::YAML, []);

$this->logger->info("Loading server properties...");
$this->properties = new Config($this->dataPath . "server.properties", Config::PROPERTIES, array(
"motd" => "Minecraft: PE Server",
"server-port" => 19132,
Expand Down Expand Up @@ -1555,7 +1561,7 @@ public function broadcast($message, $permissions){
* @param Player[] $players
* @param DataPacket $packet
*/
public function broadcastPacket(array $players, DataPacket $packet){
public static function broadcastPacket(array $players, DataPacket $packet){
foreach($players as $player){
$player->dataPacket(clone $packet);
}
Expand Down
4 changes: 2 additions & 2 deletions src/pocketmine/block/Door.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function onActivate(Item $item, Player $player = null){
$pk->z = $this->z;
$pk->evid = 1003;
$pk->data = 0;
Server::getInstance()->broadcastPacket($players, $pk);
Server::broadcastPacket($players, $pk);

return true;
}
Expand All @@ -131,7 +131,7 @@ public function onActivate(Item $item, Player $player = null){
$pk->z = $this->z;
$pk->evid = 1003;
$pk->data = 0;
Server::getInstance()->broadcastPacket($players, $pk);
Server::broadcastPacket($players, $pk);
}

return true;
Expand Down
11 changes: 9 additions & 2 deletions src/pocketmine/command/defaults/KillCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
namespace pocketmine\command\defaults;

use pocketmine\command\CommandSender;
use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\Player;
use pocketmine\Server;
use pocketmine\utils\TextFormat;

class KillCommand extends VanillaCommand{
Expand All @@ -45,9 +47,14 @@ public function execute(CommandSender $sender, $currentAlias, array $args){
if($sender instanceof Player){
//TODO: EntityDamageEvent

//$ev->setLastDamageCause()
Server::getInstance()->getPluginManager()->callEvent($ev = new EntityDamageEvent($sender, EntityDamageEvent::CAUSE_SUICIDE, 1000));

if($ev->isCancelled()){
return true;
}

$sender->setLastDamageCause($ev);
$sender->setHealth(0);
//TODO: set update
$sender->sendMessage("Ouch. That look like it hurt.");
}else{
$sender->sendMessage(TextFormat::RED . "You can only perform this command as a player");
Expand Down
5 changes: 3 additions & 2 deletions src/pocketmine/entity/DroppedItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

namespace pocketmine\entity;

use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\item\Item;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\Byte;
Expand Down Expand Up @@ -106,11 +107,11 @@ public function onUpdate(){
return true;
}

public function attack($damage, $source = "generic"){
public function attack($damage, $source = EntityDamageEvent::CAUSE_MAGIC){

}

public function heal($amount, $source = "generic"){
public function heal($amount){

}

Expand Down
54 changes: 47 additions & 7 deletions src/pocketmine/entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
namespace pocketmine\entity;

use pocketmine\block\Block;
use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\event\entity\EntityDeathEvent;
use pocketmine\event\entity\EntityDespawnEvent;
use pocketmine\event\entity\EntityLevelChangeEvent;
use pocketmine\event\entity\EntityMotionEvent;
Expand Down Expand Up @@ -79,6 +81,8 @@ abstract class Entity extends Position implements Metadatable{
/** @var Chunk */
public $chunk;

protected $lastDamageCause = null;

public $lastX;
public $lastY;
public $lastZ;
Expand Down Expand Up @@ -242,9 +246,15 @@ public function despawnFrom(Player $player){
}
}

abstract function attack($damage, $source = "generic");
/**
* @param float $damage
* @param int|EntityDamageEvent $source
*
* @return mixed
*/
abstract function attack($damage, $source = EntityDamageEvent::CAUSE_MAGIC);

abstract function heal($amount, $source = "generic");
abstract function heal($amount);

/**
* @return int
Expand All @@ -259,16 +269,36 @@ public function getHealth(){
* @param int $amount
*/
public function setHealth($amount){
if($amount < 0){
if($amount === $this->health){
return;
}

if($amount <= 0){
$this->health = 0;
$this->dead = true;
if($this->dead !== true){
$this->kill();
}
}elseif($amount > $this->getMaxHealth()){
$this->health = $this->getMaxHealth();
}else{
$this->health = (int) $amount;
}
}

/**
* @param int|EntityDamageEvent $type
*/
public function setLastDamageCause($type){
$this->lastDamageCause = $type;
}

/**
* @return int|EntityDamageEvent|null
*/
public function getLastDamageCause(){
return $this->lastDamageCause;
}

/**
* @return int
*/
Expand Down Expand Up @@ -379,6 +409,8 @@ public function entityBaseTick(){
$this->close();

return false;
}elseif($this->dead === true){
$this->despawnFromAll();
}

$hasUpdate = false;
Expand Down Expand Up @@ -447,7 +479,7 @@ public function updateMovement(){
[$this->id, $this->x, $this->y, $this->z, $this->yaw, $this->pitch]
];
}
$this->server->broadcastPacket($this->hasSpawned, $pk);
Server::broadcastPacket($this->hasSpawned, $pk);
}

if(!($this instanceof Player) and ($this->lastMotionX != $this->motionX or $this->lastMotionY != $this->motionY or $this->lastMotionZ != $this->motionZ)){
Expand All @@ -459,7 +491,7 @@ public function updateMovement(){
$pk->entities = [
[$this->getID(), $this->motionX, $this->motionY, $this->motionZ]
];
$this->server->broadcastPacket($this->hasSpawned, $pk);
Server::broadcastPacket($this->hasSpawned, $pk);
}
}

Expand Down Expand Up @@ -909,7 +941,14 @@ public function setMotion(Vector3 $motion){
$this->motionY = $motion->y;
$this->motionZ = $motion->z;
if(!$this->justCreated){
$this->scheduleUpdate();
if($this instanceof Player){
$pk = new SetEntityMotionPacket;
$pk->entities = [
[0, $this->motionX, $this->motionY, $this->motionZ]
];
$this->dataPacket($pk);
}
$this->updateMovement();
}
}

Expand All @@ -918,6 +957,7 @@ public function isOnGround(){
}

public function kill(){
$this->setHealth(0);
$this->dead = true;
$this->scheduleUpdate();
}
Expand Down
12 changes: 4 additions & 8 deletions src/pocketmine/entity/Human.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ protected function initEntity(){
parent::initEntity();
}

public function getName(){
return $this->nameTag;
}

public function saveNBT(){
parent::saveNBT();
$this->namedtag->Inventory = new Enum("Inventory", []);
Expand Down Expand Up @@ -204,12 +208,4 @@ public function getData(){ //TODO
return $d;
}

public function attack($damage, $source = "generic"){

}

public function heal($amount, $source = "generic"){

}

}
52 changes: 52 additions & 0 deletions src/pocketmine/entity/Living.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
namespace pocketmine\entity;


use pocketmine\event\entity\EntityDamageByEntityEvent;
use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\event\entity\EntityDeathEvent;
use pocketmine\event\entity\EntityRegainHealthEvent;
use pocketmine\math\Vector3;
use pocketmine\network\protocol\EntityEventPacket;
use pocketmine\Server;
use pocketmine\item\Item;

abstract class Living extends Entity implements Damageable{

protected $gravity = 0.08;
Expand All @@ -30,4 +39,47 @@ abstract class Living extends Entity implements Damageable{
protected function initEntity(){

}

public abstract function getName();

public function attack($damage, $source = EntityDamageEvent::CAUSE_MAGIC){
//TODO: attack tick limit
$pk = new EntityEventPacket();
$pk->eid = $this->getID();
$pk->event = 2; //Ouch!
Server::broadcastPacket($this->hasSpawned, $pk);
$this->setLastDamageCause($source);
$motion = new Vector3(0, 0.25, 0);
if($source instanceof EntityDamageByEntityEvent){
$e = $source->getDamager();
$motion->x = -cos(deg2rad($e->pitch)) * sin(deg2rad($e->yaw)) * 0.5;
$motion->z = cos(deg2rad($e->pitch)) * sin(deg2rad($e->yaw)) * 0.5;
}
$this->setMotion($motion);
$this->setHealth($this->getHealth() - $damage);

}

public function heal($amount){
$this->server->getPluginManager()->callEvent($ev = new EntityRegainHealthEvent($this, $amount));
if($ev->isCancelled()){
return;
}
$this->setHealth($this->getHealth() + $amount);
}

public function kill(){
parent::kill();
$this->server->getPluginManager()->callEvent($ev = new EntityDeathEvent($this, $this->getDrops()));
foreach($ev->getDrops() as $item){
$this->getLevel()->dropItem($this, $item);
}
}

/**
* @return Item[]
*/
public function getDrops(){
return [];
}
}
53 changes: 53 additions & 0 deletions src/pocketmine/event/entity/EntityDamageByEntityEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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\event\entity;

use pocketmine\entity\Entity;
use pocketmine\event\Cancellable;

class EntityDamageByEntityEvent extends EntityDamageEvent{
public static $handlerList = null;

/** @var Entity */
private $damager;


/**
* @param Entity $damager
* @param Entity $entity
* @param int $cause
* @param int|int[] $damage
*/
public function __construct(Entity $damager, Entity $entity, $cause, $damage){
$this->damager = $damager;
parent::__construct($entity, $cause, $damage);
}

/**
* @return Entity
*/
public function getDamager(){
return $this->damager;
}


}
Loading

0 comments on commit 6552397

Please sign in to comment.