From 6109505786b8cf46b93e4ee787590b03521fbd35 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Sun, 24 Aug 2014 20:34:06 +0200 Subject: [PATCH] Implemented correct time offsets and speed --- src/pocketmine/block/Bed.php | 20 ++++++--- .../command/defaults/TimeCommand.php | 3 +- src/pocketmine/entity/Arrow.php | 2 +- src/pocketmine/entity/Entity.php | 25 +++++++++++ src/pocketmine/level/Level.php | 43 +++++++++++-------- .../network/protocol/SetTimePacket.php | 4 +- 6 files changed, 69 insertions(+), 28 deletions(-) diff --git a/src/pocketmine/block/Bed.php b/src/pocketmine/block/Bed.php index 26be10a25..a1723bf77 100644 --- a/src/pocketmine/block/Bed.php +++ b/src/pocketmine/block/Bed.php @@ -22,6 +22,7 @@ namespace pocketmine\block; use pocketmine\item\Item; +use pocketmine\level\Level; use pocketmine\network\protocol\ChatPacket; use pocketmine\Player; @@ -34,13 +35,18 @@ class Bed extends Transparent{ } public function onActivate(Item $item, Player $player = null){ - /*if($player instanceof Player and Server::getInstance()->api->time->getPhase($this->getLevel()) !== "night"){ + + $time = $this->getLevel()->getTime() % Level::TIME_FULL; + + $isNight = ($time >= Level::TIME_NIGHT and $time < Level::TIME_SUNRISE); + + if($player instanceof Player and !$isNight){ $pk = new ChatPacket; $pk->message = "You can only sleep at night"; $player->dataPacket($pk); return true; - }*/ + } $blockNorth = $this->getSide(2); //Gets the blocks around them $blockSouth = $this->getSide(3); @@ -57,10 +63,12 @@ class Bed extends Transparent{ $b = $blockEast; }elseif($blockWest->getID() === $this->id and ($blockWest->meta & 0x08) === 0x08){ $b = $blockWest; - }elseif($player instanceof Player){ - $pk = new ChatPacket; - $pk->message = "This bed is incomplete"; - $player->dataPacket($pk); + }else{ + if($player instanceof Player){ + $pk = new ChatPacket; + $pk->message = "This bed is incomplete"; + $player->dataPacket($pk); + } return true; } diff --git a/src/pocketmine/command/defaults/TimeCommand.php b/src/pocketmine/command/defaults/TimeCommand.php index 3532984e2..346bfb4b8 100644 --- a/src/pocketmine/command/defaults/TimeCommand.php +++ b/src/pocketmine/command/defaults/TimeCommand.php @@ -23,6 +23,7 @@ namespace pocketmine\command\defaults; use pocketmine\command\Command; use pocketmine\command\CommandSender; +use pocketmine\level\Level; use pocketmine\utils\TextFormat; class TimeCommand extends VanillaCommand{ @@ -53,7 +54,7 @@ class TimeCommand extends VanillaCommand{ if($args[1] === "day"){ $value = 0; }elseif($args[1] === "night"){ - $value = 12500; + $value = Level::TIME_NIGHT; }else{ $value = $this->getInteger($sender, $args[1], 0); } diff --git a/src/pocketmine/entity/Arrow.php b/src/pocketmine/entity/Arrow.php index cf0288e1b..ccfee599a 100644 --- a/src/pocketmine/entity/Arrow.php +++ b/src/pocketmine/entity/Arrow.php @@ -50,7 +50,7 @@ class Arrow extends Projectile{ public function onUpdate(){ $this->entityBaseTick(); - + if($this->closed !== false){ return false; } diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index 38b5debc6..1de20b288 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -51,6 +51,7 @@ use pocketmine\Network; use pocketmine\network\protocol\MoveEntityPacket; use pocketmine\network\protocol\MovePlayerPacket; use pocketmine\network\protocol\RemoveEntityPacket; +use pocketmine\network\protocol\SetEntityDataPacket; use pocketmine\network\protocol\SetEntityMotionPacket; use pocketmine\network\protocol\SetTimePacket; use pocketmine\Player; @@ -254,6 +255,30 @@ abstract class Entity extends Position implements Metadatable{ } } + /** + * @param Player[]|Player $player + */ + public function sendMetadata($player){ + if($player instanceof Player){ + $player = [$player]; + } + + $pk = new SetEntityDataPacket(); + $pk->eid = $this->id; + $pk->metadata = $this->getData(); + foreach($player as $p){ + if($p === $this){ + /** @var Player $p */ + $pk = new SetEntityDataPacket(); + $pk->eid = 0; + $pk->metadata = $this->getData(); + $p->dataPacket($pk); + }else{ + $p->dataPacket($pk); + } + } + } + /** * @param Player $player */ diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index c03527a67..77f34ddf4 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -84,6 +84,13 @@ class Level implements ChunkManager, Metadatable{ const BLOCK_UPDATE_WEAK = 4; const BLOCK_UPDATE_TOUCH = 5; + const TIME_DAY = 0; + const TIME_SUNSET = 12000; + const TIME_NIGHT = 14000; + const TIME_SUNRISE = 23000; + + const TIME_FULL = 24000; + /** @var Tile[] */ protected $tiles = []; @@ -112,8 +119,6 @@ class Level implements ChunkManager, Metadatable{ protected $time; public $stopTime; - private $startCheck; - private $startTime; private $folderName; @@ -160,7 +165,7 @@ class Level implements ChunkManager, Metadatable{ Block::PUMPKIN_STEM => true, Block::MELON_STEM => true, //Block::VINE => true, - //Block::MYCELIUM => true, + Block::MYCELIUM => true, //Block::COCOA_BLOCK => true, Block::CARROT_BLOCK => true, Block::POTATO_BLOCK => true, @@ -222,8 +227,7 @@ class Level implements ChunkManager, Metadatable{ $this->folderName = $name; $this->updateQueue = new ReversePriorityQueue(); $this->updateQueue->setExtractFlags(\SplPriorityQueue::EXTR_BOTH); - $this->startTime = $this->time = (int) $this->provider->getTime(); - $this->nextSave = $this->startCheck = microtime(true); + $this->time = (int) $this->provider->getTime(); $this->nextSave = microtime(true) + 90; $this->chunkTickRadius = min($this->server->getViewDistance(), max(1, (int) $this->server->getProperty("chunk-ticking.tick-radius", 3))); @@ -382,22 +386,24 @@ class Level implements ChunkManager, Metadatable{ * Changes to this function won't be recorded on the version. */ public function checkTime(){ - $now = microtime(true); if($this->stopTime == true){ return; }else{ - $time = $this->startTime + ($now - $this->startCheck) * 20; + $this->time += 2.5; } + } - $this->time = $time; + /** + * WARNING: Do not use this, it's only for internal use. + * Changes to this function won't be recorded on the version. + */ + public function sendTime(){ $pk = new SetTimePacket; $pk->time = (int) $this->time; $pk->started = $this->stopTime == false; foreach($this->players as $player){ - $player->dataPacket($pk); + $player->directDataPacket($pk); } - - return; } /** @@ -412,8 +418,10 @@ class Level implements ChunkManager, Metadatable{ $this->timings->doTick->startTiming(); + $this->checkTime(); + if(($currentTick % 200) === 0){ - $this->checkTime(); + $this->sendTime(); } if(count($this->changedCount) > 0){ @@ -1730,9 +1738,8 @@ class Level implements ChunkManager, Metadatable{ * @param int $time */ public function setTime($time){ - $this->startTime = $this->time = (int) $time; - $this->startCheck = microtime(true); - $this->checkTime(); + $this->time = (int) $time; + $this->sendTime(); } /** @@ -1740,8 +1747,7 @@ class Level implements ChunkManager, Metadatable{ */ public function stopTime(){ $this->stopTime = true; - $this->startCheck = 0; - $this->checkTime(); + $this->sendTime(); } /** @@ -1749,8 +1755,7 @@ class Level implements ChunkManager, Metadatable{ */ public function startTime(){ $this->stopTime = false; - $this->startCheck = microtime(true); - $this->checkTime(); + $this->sendTime(); } /** diff --git a/src/pocketmine/network/protocol/SetTimePacket.php b/src/pocketmine/network/protocol/SetTimePacket.php index 2f013bd34..bfa51a5da 100644 --- a/src/pocketmine/network/protocol/SetTimePacket.php +++ b/src/pocketmine/network/protocol/SetTimePacket.php @@ -22,6 +22,8 @@ namespace pocketmine\network\protocol; +use pocketmine\level\Level; + class SetTimePacket extends DataPacket{ public $time; public $started = true; @@ -36,7 +38,7 @@ class SetTimePacket extends DataPacket{ public function encode(){ $this->reset(); - $this->putInt($this->time); + $this->putInt((int) (($this->time / Level::TIME_FULL) * 19200)); $this->putByte($this->started == true ? 0x80 : 0x00); }