mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-07 02:08:21 +00:00
Added events
This commit is contained in:
@ -39,7 +39,7 @@ class BeetrootSoup extends Food{
|
||||
return 7.2;
|
||||
}
|
||||
|
||||
public function getResidue() : Item{
|
||||
public function getResidue(){
|
||||
return Item::get(Item::BOWL);
|
||||
}
|
||||
}
|
@ -67,7 +67,7 @@ class Fish extends Food{
|
||||
return 0;
|
||||
}
|
||||
|
||||
public function getAdditionEffects() : array{
|
||||
public function getAdditionalEffects() : array{
|
||||
return $this->meta === self::FISH_PUFFERFISH ? [
|
||||
Effect::getEffect(Effect::HUNGER)->setDuration(300)->setAmplifier(2),
|
||||
Effect::getEffect(Effect::NAUSEA)->setDuration(300)->setAmplifier(1),
|
||||
|
@ -21,18 +21,23 @@
|
||||
|
||||
namespace pocketmine\item;
|
||||
|
||||
use pocketmine\entity\Effect;
|
||||
use pocketmine\entity\Entity;
|
||||
use pocketmine\entity\Human;
|
||||
use pocketmine\event\entity\EntityEatItemEvent;
|
||||
use pocketmine\network\protocol\EntityEventPacket;
|
||||
use pocketmine\Player;
|
||||
use pocketmine\Server;
|
||||
|
||||
abstract class Food extends Item{
|
||||
public abstract function getFoodRestore() : int;
|
||||
abstract class Food extends Item implements FoodSource{
|
||||
public function canBeConsumed() : bool{
|
||||
return true;
|
||||
}
|
||||
|
||||
public abstract function getSaturationRestore() : float;
|
||||
public function canBeConsumedBy(Entity $entity){
|
||||
return $entity instanceof Human and $entity->getFood() < $entity->getMaxFood();
|
||||
}
|
||||
|
||||
public function getResidue() : Item{
|
||||
public function getResidue(){
|
||||
if($this->getCount() === 1){
|
||||
return Item::get(0);
|
||||
}else{
|
||||
@ -42,14 +47,11 @@ abstract class Food extends Item{
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Effect[]
|
||||
*/
|
||||
public function getAdditionEffects() : array{
|
||||
public function getAdditionalEffects() : array{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function onEat(Human $human){
|
||||
public function onConsume(Entity $human){
|
||||
$pk = new EntityEventPacket();
|
||||
$pk->eid = $human->getId();
|
||||
$pk->event = EntityEventPacket::USE_ITEM;
|
||||
@ -57,12 +59,15 @@ abstract class Food extends Item{
|
||||
$human->dataPacket($pk);
|
||||
}
|
||||
Server::broadcastPacket($human->getViewers(), $pk);
|
||||
|
||||
$human->addSaturation($this->getSaturationRestore());
|
||||
$human->addFood($this->getFoodRestore());
|
||||
foreach($this->getAdditionEffects() as $effect){
|
||||
|
||||
$ev = new EntityEatItemEvent($human, $this);
|
||||
|
||||
$human->addSaturation($ev->getSaturationRestore());
|
||||
$human->addFood($ev->getFoodRestore());
|
||||
foreach($ev->getAdditionalEffects() as $effect){
|
||||
$human->addEffect($effect);
|
||||
}
|
||||
$human->getInventory()->setItemInHand($this->getResidue());
|
||||
|
||||
$human->getInventory()->setItemInHand($ev->getResidue());
|
||||
}
|
||||
}
|
||||
|
37
src/pocketmine/item/FoodSource.php
Normal file
37
src/pocketmine/item/FoodSource.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?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\item;
|
||||
|
||||
use pocketmine\entity\Effect;
|
||||
|
||||
interface FoodSource{
|
||||
public function getFoodRestore() : int;
|
||||
|
||||
public function getSaturationRestore() : float;
|
||||
|
||||
public function getResidue();
|
||||
|
||||
/**
|
||||
* @return Effect[]
|
||||
*/
|
||||
public function getAdditionalEffects() : array;
|
||||
}
|
@ -36,7 +36,7 @@ class GoldenApple extends Food{
|
||||
return 9.6;
|
||||
}
|
||||
|
||||
public function getAdditionEffects() : array{
|
||||
public function getAdditionalEffects() : array{
|
||||
return $this->meta === 1 ? [
|
||||
Effect::getEffect(Effect::REGENERATION)->setDuration(600)->setAmplifier(4),
|
||||
Effect::getEffect(Effect::ABSORPTION)->setDuration(2400),
|
||||
|
@ -1042,6 +1042,17 @@ class Item{
|
||||
return $this->block !== null and $this->block->canBePlaced();
|
||||
}
|
||||
|
||||
public function canBeConsumed() : bool{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function canBeConsumedBy(Entity $entity) : bool{
|
||||
return $this->canBeConsumed();
|
||||
}
|
||||
|
||||
public function onConsume(Entity $entity){
|
||||
}
|
||||
|
||||
public function getBlock() : Block{
|
||||
if($this->block instanceof Block){
|
||||
return clone $this->block;
|
||||
|
@ -38,7 +38,7 @@ class MushroomStew extends Food{
|
||||
return 7.2;
|
||||
}
|
||||
|
||||
public function getResidue() : Item{
|
||||
public function getResidue(){
|
||||
return Item::get(Item::BOWL);
|
||||
}
|
||||
}
|
||||
|
@ -21,8 +21,18 @@
|
||||
|
||||
namespace pocketmine\item;
|
||||
|
||||
use pocketmine\entity\Entity;
|
||||
|
||||
class Potion extends Item{
|
||||
public function __construct($meta = 0, $count = 1){
|
||||
parent::__construct(self::POTION, $meta, $count, "Potion");
|
||||
}
|
||||
|
||||
public function canBeConsumed() : bool{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function onConsume(Entity $entity){
|
||||
// TODO: Implement potions
|
||||
}
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ class RawChicken extends Food{
|
||||
return 1.2;
|
||||
}
|
||||
|
||||
public function getAdditionEffects() : array{
|
||||
public function getAdditionalEffects() : array{
|
||||
if(mt_rand(0, 9) < 3){
|
||||
return Effect::getEffect(Effect::HUNGER)->setDuration(600);
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ class SpiderEye extends Food{
|
||||
return 3.2;
|
||||
}
|
||||
|
||||
public function getAdditionEffects() : array{
|
||||
public function getAdditionalEffects() : array{
|
||||
return [Effect::getEffect(Effect::POISON)->setDuration(80)];
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user