Implemented food and hunger

This commit is contained in:
PEMapModder
2016-02-10 22:49:48 +08:00
parent 1082e32fd7
commit 329a525ea1
47 changed files with 724 additions and 323 deletions

View File

@ -76,11 +76,9 @@ use pocketmine\inventory\PlayerInventory;
use pocketmine\inventory\ShapedRecipe;
use pocketmine\inventory\ShapelessRecipe;
use pocketmine\inventory\SimpleTransactionGroup;
use pocketmine\item\Item;
use pocketmine\level\ChunkLoader;
use pocketmine\level\format\FullChunk;
use pocketmine\level\format\LevelProvider;
use pocketmine\level\Level;
use pocketmine\level\Location;
use pocketmine\level\Position;
@ -93,13 +91,12 @@ use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\ByteTag;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\DoubleTag;
use pocketmine\nbt\tag\ListTag;
use pocketmine\nbt\tag\FloatTag;
use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\ListTag;
use pocketmine\nbt\tag\LongTag;
use pocketmine\nbt\tag\ShortTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\network\Network;
use pocketmine\network\protocol\AdventureSettingsPacket;
use pocketmine\network\protocol\AnimatePacket;
use pocketmine\network\protocol\BatchPacket;
@ -110,20 +107,20 @@ use pocketmine\network\protocol\DisconnectPacket;
use pocketmine\network\protocol\EntityEventPacket;
use pocketmine\network\protocol\FullChunkDataPacket;
use pocketmine\network\protocol\Info as ProtocolInfo;
use pocketmine\network\protocol\MovePlayerPacket;
use pocketmine\network\protocol\PlayerActionPacket;
use pocketmine\network\protocol\PlayStatusPacket;
use pocketmine\network\protocol\RespawnPacket;
use pocketmine\network\protocol\SetPlayerGameTypePacket;
use pocketmine\network\protocol\TextPacket;
use pocketmine\network\protocol\MovePlayerPacket;
use pocketmine\network\protocol\SetDifficultyPacket;
use pocketmine\network\protocol\SetEntityMotionPacket;
use pocketmine\network\protocol\SetHealthPacket;
use pocketmine\network\protocol\SetPlayerGameTypePacket;
use pocketmine\network\protocol\SetSpawnPositionPacket;
use pocketmine\network\protocol\SetTimePacket;
use pocketmine\network\protocol\StartGamePacket;
use pocketmine\network\protocol\TakeItemEntityPacket;
use pocketmine\network\protocol\TextPacket;
use pocketmine\network\protocol\UpdateAttributesPacket;
use pocketmine\network\protocol\UpdateBlockPacket;
use pocketmine\network\SourceInterface;
use pocketmine\permission\PermissibleBase;
@ -133,8 +130,6 @@ use pocketmine\tile\Sign;
use pocketmine\tile\Spawnable;
use pocketmine\tile\Tile;
use pocketmine\utils\TextFormat;
use raklib\Binary;
/**
@ -221,7 +216,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
protected $viewDistance;
protected $chunksPerTick;
protected $spawnThreshold;
protected $spawnThreshold;
/** @var null|Position */
private $spawnPosition = null;
@ -247,7 +242,8 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
/**
* This might disappear in the future.
* Please use getUniqueId() instead (IP + clientId + name combo, in the future it'll change to real UUID for online auth)
* Please use getUniqueId() instead (IP + clientId + name combo, in the future it'll change to real UUID for online
* auth)
*
* @deprecated
*
@ -486,7 +482,6 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
return $this->perm->getEffectivePermissions();
}
/**
* @param SourceInterface $interface
* @param null $clientID
@ -505,7 +500,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
$this->clientID = $clientID;
$this->loaderId = Level::generateChunkLoaderId($this);
$this->chunksPerTick = (int) $this->server->getProperty("chunk-sending.per-tick", 4);
$this->spawnThreshold = (int) $this->server->getProperty("chunk-sending.spawn-threshold", 56);
$this->spawnThreshold = (int) $this->server->getProperty("chunk-sending.spawn-threshold", 56);
$this->spawnPosition = null;
$this->gamemode = $this->server->getGamemode();
$this->setLevel($this->server->getDefaultLevel());
@ -559,9 +554,9 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
return $this->displayName;
}
/**
* @param string $name
*/
/**
* @param string $name
*/
public function setDisplayName($name){
$this->displayName = $name;
if($this->spawned){
@ -853,7 +848,6 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
$this->loadQueue = $newOrder;
Timings::$playerChunkOrderTimer->stopTiming();
return true;
@ -984,12 +978,12 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
$this->level->sleepTicks = 60;
return true;
}
/**
* Sets the spawnpoint of the player (and the compass direction) to a Vector3, or set it on another world with a Position object
* Sets the spawnpoint of the player (and the compass direction) to a Vector3, or set it on another world with a
* Position object
*
* @param Vector3|Position $pos
*/
@ -1015,7 +1009,6 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
$this->setDataProperty(self::DATA_PLAYER_BED_POSITION, self::DATA_TYPE_POS, [0, 0, 0]);
$this->setDataFlag(self::DATA_PLAYER_FLAGS, self::DATA_PLAYER_FLAG_SLEEP, false);
$this->level->sleepTicks = 0;
$pk = new AnimatePacket();
@ -1023,7 +1016,6 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
$pk->action = 3; //Wake up
$this->dataPacket($pk);
}
}
/**
@ -1211,6 +1203,17 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
return false;
}
public function entityBaseTick($tickDiff = 1){
parent::entityBaseTick($tickDiff);
$entries = $this->attributeMap->needSend();
if(count($entries) > 0){
$pk = new UpdateAttributesPacket();
$pk->entityId = 0;
$pk->entries = $entries;
$this->dataPacket($pk);
}
}
protected function checkGroundState($movX, $movY, $movZ, $dx, $dy, $dz){
if(!$this->onGround or $movY != 0){
$bb = clone $this->boundingBox;
@ -1446,7 +1449,6 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
}
protected function updateMovement(){
}
public function onUpdate($currentTick){
@ -1529,7 +1531,6 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
$this->server->batchPackets([$this], $this->batchedPackets, false);
$this->batchedPackets = [];
}
}
public function canInteract(Vector3 $pos, $maxDistance, $maxDiff = 0.5){
@ -1610,7 +1611,6 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
$this->allowFlight = $this->isCreative();
if(($level = $this->server->getLevelByName($nbt["Level"])) === null){
$this->setLevel($this->server->getDefaultLevel());
$nbt["Level"] = $this->level->getName();
@ -1750,7 +1750,6 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
return;
}
$timings = Timings::getReceiveDataPacketTimings($packet);
$timings->startTiming();
@ -1906,13 +1905,13 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
break;
}
}else{
if($packet->selectedSlot >= 0 and $packet->selectedSlot < 9){
$this->inventory->setHeldItemIndex($packet->selectedSlot);
$this->inventory->setHeldItemSlot($packet->slot);
}else{
$this->inventory->sendContents($this);
break;
}
if($packet->selectedSlot >= 0 and $packet->selectedSlot < 9){
$this->inventory->setHeldItemIndex($packet->selectedSlot);
$this->inventory->setHeldItemSlot($packet->slot);
}else{
$this->inventory->sendContents($this);
break;
}
}
}elseif($item === null or $slot === -1 or !$item->deepEquals($packet->item)){ // packet error or not implemented
$this->inventory->sendContents($this);
@ -1922,13 +1921,13 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
$this->inventory->setItem($packet->selectedSlot, $item);
$this->inventory->setHeldItemSlot($packet->selectedSlot);
}else{
if($packet->selectedSlot >= 0 and $packet->selectedSlot < $this->inventory->getHotbarSize()){
$this->inventory->setHeldItemIndex($packet->selectedSlot);
$this->inventory->setHeldItemSlot($slot);
}else{
$this->inventory->sendContents($this);
break;
}
if($packet->selectedSlot >= 0 and $packet->selectedSlot < $this->inventory->getHotbarSize()){
$this->inventory->setHeldItemIndex($packet->selectedSlot);
$this->inventory->setHeldItemSlot($slot);
}else{
$this->inventory->sendContents($this);
break;
}
}
$this->inventory->sendHeldItem($this->hasSpawned);
@ -1948,7 +1947,6 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_ACTION, false);
if(!$this->canInteract($blockVector->add(0.5, 0.5, 0.5), 13) or $this->isSpectator()){
}elseif($this->isCreative()){
$item = $this->inventory->getItemInHand();
if($this->level->useItemOn($blockVector, $item, $packet->face, $packet->fx, $packet->fy, $packet->fz, $this) === true){
@ -2069,14 +2067,13 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
break;
case PlayerActionPacket::ACTION_RELEASE_ITEM:
if($this->startAction > -1 and $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_ACTION)){
if($this->inventory->getItemInHand()->getId() === Item::BOW) {
if($this->inventory->getItemInHand()->getId() === Item::BOW){
$bow = $this->inventory->getItemInHand();
if ($this->isSurvival() and !$this->inventory->contains(Item::get(Item::ARROW, 0, 1))) {
if($this->isSurvival() and !$this->inventory->contains(Item::get(Item::ARROW, 0, 1))){
$this->inventory->sendContents($this);
break;
}
$nbt = new CompoundTag("", [
"Pos" => new ListTag("Pos", [
new DoubleTag("", $this->x),
@ -2100,35 +2097,35 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
$f = min((($p ** 2) + $p * 2) / 3, 1) * 2;
$ev = new EntityShootBowEvent($this, $bow, Entity::createEntity("Arrow", $this->chunk, $nbt, $this, $f == 2 ? true : false), $f);
if ($f < 0.1 or $diff < 5) {
if($f < 0.1 or $diff < 5){
$ev->setCancelled();
}
$this->server->getPluginManager()->callEvent($ev);
if ($ev->isCancelled()) {
if($ev->isCancelled()){
$ev->getProjectile()->kill();
$this->inventory->sendContents($this);
} else {
}else{
$ev->getProjectile()->setMotion($ev->getProjectile()->getMotion()->multiply($ev->getForce()));
if($this->isSurvival()){
$this->inventory->removeItem(Item::get(Item::ARROW, 0, 1));
$bow->setDamage($bow->getDamage() + 1);
if ($bow->getDamage() >= 385) {
if($bow->getDamage() >= 385){
$this->inventory->setItemInHand(Item::get(Item::AIR, 0, 0));
} else {
}else{
$this->inventory->setItemInHand($bow);
}
}
if ($ev->getProjectile() instanceof Projectile) {
if($ev->getProjectile() instanceof Projectile){
$this->server->getPluginManager()->callEvent($projectileEv = new ProjectileLaunchEvent($ev->getProjectile()));
if ($projectileEv->isCancelled()) {
if($projectileEv->isCancelled()){
$ev->getProjectile()->kill();
} else {
}else{
$ev->getProjectile()->spawnToAll();
$this->level->addSound(new LaunchSound($this), $this->getViewers());
}
} else {
}else{
$ev->getProjectile()->spawnToAll();
}
}
@ -2146,7 +2143,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
$this->dataPacket($pk);
Server::broadcastPacket($this->getViewers(), $pk);
if ($this->isSurvival()) {
if($this->isSurvival()){
$slot = $this->inventory->getItemInHand();
--$slot->count;
$this->inventory->setItemInHand($slot);
@ -2249,7 +2246,6 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
$vector = new Vector3($packet->x, $packet->y, $packet->z);
if($this->isCreative()){
$item = $this->inventory->getItemInHand();
}else{
@ -2274,7 +2270,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
$this->level->sendBlocks([$this], [$target], UpdateBlockPacket::FLAG_ALL_PRIORITY);
$this->inventory->sendHeldItem($this);
$this->inventory->sendHeldItem($this);
if($tile instanceof Spawnable){
$tile->spawnTo($this);
@ -2405,7 +2401,6 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
}
}
break;
case ProtocolInfo::ANIMATE_PACKET:
if($this->spawned === false or !$this->isAlive()){
@ -2434,7 +2429,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
switch($packet->event){
case 9: //Eating
$items = [ //TODO: move this to item classes
$items = [ //TODO: change to hunger; add RabbitStew and RawRabbit and RottenFlesh
Item::APPLE => 4,
Item::MUSHROOM_STEW => 10,
Item::BEETROOT_SOUP => 10,
@ -2481,7 +2476,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
if(is_array($amount)){
$amount = isset($amount[$slot->getDamage()]) ? $amount[$slot->getDamage()] : 0;
}
$ev = new EntityRegainHealthEvent($this, $amount, EntityRegainHealthEvent::CAUSE_EATING);
$ev = new EntityRegainHealthEvent($this, $amount, EntityRegainHealthEvent::CAUSE_EATING);
$this->heal($ev->getAmount(), $ev);
--$slot->count;
@ -2593,7 +2588,6 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
$canCraft = true;
if($recipe instanceof ShapedRecipe){
for($x = 0; $x < 3 and $canCraft; ++$x){
for($y = 0; $y < 3; ++$y){
@ -2645,7 +2639,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
$result = $packet->output[0];
if(!$canCraft or !$recipe->getResult()->deepEquals($result)){
$this->server->getLogger()->debug("Unmatched recipe ". $recipe->getId() ." from player ". $this->getName() .": expected " . $recipe->getResult() . ", got ". $result .", using: " . implode(", ", $ingredients));
$this->server->getLogger()->debug("Unmatched recipe " . $recipe->getId() . " from player " . $this->getName() . ": expected " . $recipe->getResult() . ", got " . $result . ", using: " . implode(", ", $ingredients));
$this->inventory->sendContents($this);
break;
}
@ -2669,7 +2663,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
}
if(!$canCraft){
$this->server->getLogger()->debug("Unmatched recipe ". $recipe->getId() ." from player ". $this->getName() .": client does not have enough items, using: " . implode(", ", $ingredients));
$this->server->getLogger()->debug("Unmatched recipe " . $recipe->getId() . " from player " . $this->getName() . ": client does not have enough items, using: " . implode(", ", $ingredients));
$this->inventory->sendContents($this);
break;
}
@ -2781,7 +2775,6 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
break;
}
if($this->currentTransaction === null or $this->currentTransaction->getCreationTime() < (microtime(true) - 8)){
if($this->currentTransaction !== null){
foreach($this->currentTransaction->getInventories() as $inventory){
@ -2883,11 +2876,11 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
$this->server->getPluginManager()->callEvent($ev = new PlayerKickEvent($this, $reason, $this->getLeaveMessage()));
if(!$ev->isCancelled()){
if($isAdmin){
if(!$this->isBanned()) {
$message = "Kicked by admin." . ($reason !== "" ? " Reason: " . $reason : "");
}else{
$message = $reason;
}
if(!$this->isBanned()){
$message = "Kicked by admin." . ($reason !== "" ? " Reason: " . $reason : "");
}else{
$message = $reason;
}
}else{
if($reason === ""){
$message = "disconnectionScreen.noReason";
@ -2910,12 +2903,11 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
*/
public function sendMessage($message){
if($message instanceof TextContainer){
if ($message instanceof TranslationContainer) {
$this->sendTranslation($message->getText(), $message->getParameters());
return;
}
$message = $message->getText();
if($message instanceof TranslationContainer){
$this->sendTranslation($message->getText(), $message->getParameters());
return;
}
$message = $message->getText();
}
$mes = explode("\n", $this->server->getLanguage()->translateString($message));
@ -2966,7 +2958,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
*
* @param string $message Message to be broadcasted
* @param string $reason Reason showed in console
* @param bool $notify
* @param bool $notify
*/
public final function close($message = "", $reason = "generic reason", $notify = true){
@ -3206,7 +3198,6 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
break;
default:
}
Entity::kill();
@ -3227,7 +3218,6 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
$this->server->broadcast($ev->getDeathMessage(), Server::BROADCAST_CHANNEL_USERS);
}
$pk = new RespawnPacket();
$pos = $this->getSpawn();
$pk->x = $pos->x;
@ -3250,13 +3240,13 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
return;
}
if($this->isCreative()
and $source->getCause() !== EntityDamageEvent::CAUSE_MAGIC
and $source->getCause() !== EntityDamageEvent::CAUSE_SUICIDE
and $source->getCause() !== EntityDamageEvent::CAUSE_VOID
){
$source->setCancelled();
}elseif($this->allowFlight and $source->getCause() === EntityDamageEvent::CAUSE_FALL){
if($this->isCreative()
and $source->getCause() !== EntityDamageEvent::CAUSE_MAGIC
and $source->getCause() !== EntityDamageEvent::CAUSE_SUICIDE
and $source->getCause() !== EntityDamageEvent::CAUSE_VOID
){
$source->setCancelled();
}elseif($this->allowFlight and $source->getCause() === EntityDamageEvent::CAUSE_FALL){
$source->setCancelled();
}
@ -3355,11 +3345,11 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
/**
* @param Vector3|Position|Location $pos
* @param float $yaw
* @param float $pitch
* @param float $yaw
* @param float $pitch
*
* @return bool
*/
*/
public function teleport(Vector3 $pos, $yaw = null, $pitch = null){
if(!$this->isOnline()){
return false;
@ -3412,7 +3402,6 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
$this->forceMovement = new Vector3($this->x, $this->y, $this->z);
$this->sendPosition($this, $this->yaw, $this->pitch, 1);
$this->resetFallDistance();
$this->orderChunks();
$this->nextChunkOrderRun = 0;
@ -3420,7 +3409,6 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
}
}
/**
* @param Inventory $inventory
*
@ -3488,25 +3476,20 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
$this->server->getPlayerMetadata()->removeMetadata($this, $metadataKey, $plugin);
}
public function onChunkChanged(FullChunk $chunk){
$this->loadQueue[Level::chunkHash($chunk->getX(), $chunk->getZ())] = abs(($this->x >> 4) - $chunk->getX()) + abs(($this->z >> 4) - $chunk->getZ());
}
public function onChunkLoaded(FullChunk $chunk){
}
public function onChunkPopulated(FullChunk $chunk){
}
public function onChunkUnloaded(FullChunk $chunk){
}
public function onBlockChanged(Vector3 $block){
}
public function getLoaderId(){