Fixed the half-done hunger implementation, fixed lots of bugs related to hunger

- Fixed starvation doesn't deal any damage at all (Human->getFood() returns a float, not an int, === 0 won't work so great)
- Added exhaustion for sprinting, walking, jumping and sprint-jumping as per MCPE (these use MCPE values, and yes MCPE does walking exhaustion!)
- Fixed attributes don't get reset after player death
- Added food and hunger regeneration in peaceful difficulty
- Added API methods Living->jump() (motion isn't updated yet, so this won't actually do much if plugins try to use it) and Living->getJumpVelocity()

TODO: implement exhaustion for swimming
This commit is contained in:
Dylan K. Taylor
2017-04-03 13:12:33 +01:00
parent 00a226921c
commit 2204942338
6 changed files with 101 additions and 31 deletions

View File

@ -43,6 +43,8 @@ abstract class Living extends Entity implements Damageable{
protected $invisible = false;
protected $jumpVelocity = 0.42;
protected function initEntity(){
parent::initEntity();
@ -115,6 +117,23 @@ abstract class Living extends Entity implements Damageable{
$this->attackTime = 0;
}
/**
* Returns the initial upwards velocity of a jumping entity in blocks/tick, including additional velocity due to effects.
* @return float
*/
public function getJumpVelocity() : float{
return $this->jumpVelocity + ($this->hasEffect(Effect::JUMP) ? (($this->getEffect(Effect::JUMP)->getAmplifier() + 1) / 10) : 0);
}
/**
* Called when the entity jumps from the ground. This method adds upwards velocity to the entity.
*/
public function jump(){
if($this->onGround){
$this->motionY = $this->getJumpVelocity(); //Y motion should already be 0 if we're jumping from the ground.
}
}
public function attack($damage, EntityDamageEvent $source){
if($this->attackTime > 0 or $this->noDamageTicks > 0){
$lastCause = $this->getLastDamageCause();
@ -183,6 +202,10 @@ abstract class Living extends Entity implements Damageable{
return;
}
parent::kill();
$this->callDeathEvent();
}
protected function callDeathEvent(){
$this->server->getPluginManager()->callEvent($ev = new EntityDeathEvent($this, $this->getDrops()));
foreach($ev->getDrops() as $item){
$this->getLevel()->dropItem($this, $item);