Implemented correct time offsets and speed

This commit is contained in:
Shoghi Cervantes 2014-08-24 20:34:06 +02:00
parent 764937dda4
commit 6109505786
6 changed files with 69 additions and 28 deletions

View File

@ -22,6 +22,7 @@
namespace pocketmine\block; namespace pocketmine\block;
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\level\Level;
use pocketmine\network\protocol\ChatPacket; use pocketmine\network\protocol\ChatPacket;
use pocketmine\Player; use pocketmine\Player;
@ -34,13 +35,18 @@ class Bed extends Transparent{
} }
public function onActivate(Item $item, Player $player = null){ 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 = new ChatPacket;
$pk->message = "You can only sleep at night"; $pk->message = "You can only sleep at night";
$player->dataPacket($pk); $player->dataPacket($pk);
return true; return true;
}*/ }
$blockNorth = $this->getSide(2); //Gets the blocks around them $blockNorth = $this->getSide(2); //Gets the blocks around them
$blockSouth = $this->getSide(3); $blockSouth = $this->getSide(3);
@ -57,10 +63,12 @@ class Bed extends Transparent{
$b = $blockEast; $b = $blockEast;
}elseif($blockWest->getID() === $this->id and ($blockWest->meta & 0x08) === 0x08){ }elseif($blockWest->getID() === $this->id and ($blockWest->meta & 0x08) === 0x08){
$b = $blockWest; $b = $blockWest;
}elseif($player instanceof Player){ }else{
$pk = new ChatPacket; if($player instanceof Player){
$pk->message = "This bed is incomplete"; $pk = new ChatPacket;
$player->dataPacket($pk); $pk->message = "This bed is incomplete";
$player->dataPacket($pk);
}
return true; return true;
} }

View File

@ -23,6 +23,7 @@ namespace pocketmine\command\defaults;
use pocketmine\command\Command; use pocketmine\command\Command;
use pocketmine\command\CommandSender; use pocketmine\command\CommandSender;
use pocketmine\level\Level;
use pocketmine\utils\TextFormat; use pocketmine\utils\TextFormat;
class TimeCommand extends VanillaCommand{ class TimeCommand extends VanillaCommand{
@ -53,7 +54,7 @@ class TimeCommand extends VanillaCommand{
if($args[1] === "day"){ if($args[1] === "day"){
$value = 0; $value = 0;
}elseif($args[1] === "night"){ }elseif($args[1] === "night"){
$value = 12500; $value = Level::TIME_NIGHT;
}else{ }else{
$value = $this->getInteger($sender, $args[1], 0); $value = $this->getInteger($sender, $args[1], 0);
} }

View File

@ -51,6 +51,7 @@ use pocketmine\Network;
use pocketmine\network\protocol\MoveEntityPacket; use pocketmine\network\protocol\MoveEntityPacket;
use pocketmine\network\protocol\MovePlayerPacket; use pocketmine\network\protocol\MovePlayerPacket;
use pocketmine\network\protocol\RemoveEntityPacket; use pocketmine\network\protocol\RemoveEntityPacket;
use pocketmine\network\protocol\SetEntityDataPacket;
use pocketmine\network\protocol\SetEntityMotionPacket; use pocketmine\network\protocol\SetEntityMotionPacket;
use pocketmine\network\protocol\SetTimePacket; use pocketmine\network\protocol\SetTimePacket;
use pocketmine\Player; 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 * @param Player $player
*/ */

View File

@ -84,6 +84,13 @@ class Level implements ChunkManager, Metadatable{
const BLOCK_UPDATE_WEAK = 4; const BLOCK_UPDATE_WEAK = 4;
const BLOCK_UPDATE_TOUCH = 5; 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[] */ /** @var Tile[] */
protected $tiles = []; protected $tiles = [];
@ -112,8 +119,6 @@ class Level implements ChunkManager, Metadatable{
protected $time; protected $time;
public $stopTime; public $stopTime;
private $startCheck;
private $startTime;
private $folderName; private $folderName;
@ -160,7 +165,7 @@ class Level implements ChunkManager, Metadatable{
Block::PUMPKIN_STEM => true, Block::PUMPKIN_STEM => true,
Block::MELON_STEM => true, Block::MELON_STEM => true,
//Block::VINE => true, //Block::VINE => true,
//Block::MYCELIUM => true, Block::MYCELIUM => true,
//Block::COCOA_BLOCK => true, //Block::COCOA_BLOCK => true,
Block::CARROT_BLOCK => true, Block::CARROT_BLOCK => true,
Block::POTATO_BLOCK => true, Block::POTATO_BLOCK => true,
@ -222,8 +227,7 @@ class Level implements ChunkManager, Metadatable{
$this->folderName = $name; $this->folderName = $name;
$this->updateQueue = new ReversePriorityQueue(); $this->updateQueue = new ReversePriorityQueue();
$this->updateQueue->setExtractFlags(\SplPriorityQueue::EXTR_BOTH); $this->updateQueue->setExtractFlags(\SplPriorityQueue::EXTR_BOTH);
$this->startTime = $this->time = (int) $this->provider->getTime(); $this->time = (int) $this->provider->getTime();
$this->nextSave = $this->startCheck = microtime(true);
$this->nextSave = microtime(true) + 90; $this->nextSave = microtime(true) + 90;
$this->chunkTickRadius = min($this->server->getViewDistance(), max(1, (int) $this->server->getProperty("chunk-ticking.tick-radius", 3))); $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. * Changes to this function won't be recorded on the version.
*/ */
public function checkTime(){ public function checkTime(){
$now = microtime(true);
if($this->stopTime == true){ if($this->stopTime == true){
return; return;
}else{ }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 = new SetTimePacket;
$pk->time = (int) $this->time; $pk->time = (int) $this->time;
$pk->started = $this->stopTime == false; $pk->started = $this->stopTime == false;
foreach($this->players as $player){ 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->timings->doTick->startTiming();
$this->checkTime();
if(($currentTick % 200) === 0){ if(($currentTick % 200) === 0){
$this->checkTime(); $this->sendTime();
} }
if(count($this->changedCount) > 0){ if(count($this->changedCount) > 0){
@ -1730,9 +1738,8 @@ class Level implements ChunkManager, Metadatable{
* @param int $time * @param int $time
*/ */
public function setTime($time){ public function setTime($time){
$this->startTime = $this->time = (int) $time; $this->time = (int) $time;
$this->startCheck = microtime(true); $this->sendTime();
$this->checkTime();
} }
/** /**
@ -1740,8 +1747,7 @@ class Level implements ChunkManager, Metadatable{
*/ */
public function stopTime(){ public function stopTime(){
$this->stopTime = true; $this->stopTime = true;
$this->startCheck = 0; $this->sendTime();
$this->checkTime();
} }
/** /**
@ -1749,8 +1755,7 @@ class Level implements ChunkManager, Metadatable{
*/ */
public function startTime(){ public function startTime(){
$this->stopTime = false; $this->stopTime = false;
$this->startCheck = microtime(true); $this->sendTime();
$this->checkTime();
} }
/** /**

View File

@ -22,6 +22,8 @@
namespace pocketmine\network\protocol; namespace pocketmine\network\protocol;
use pocketmine\level\Level;
class SetTimePacket extends DataPacket{ class SetTimePacket extends DataPacket{
public $time; public $time;
public $started = true; public $started = true;
@ -36,7 +38,7 @@ class SetTimePacket extends DataPacket{
public function encode(){ public function encode(){
$this->reset(); $this->reset();
$this->putInt($this->time); $this->putInt((int) (($this->time / Level::TIME_FULL) * 19200));
$this->putByte($this->started == true ? 0x80 : 0x00); $this->putByte($this->started == true ? 0x80 : 0x00);
} }