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;
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;
}

View File

@ -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);
}

View File

@ -50,7 +50,7 @@ class Arrow extends Projectile{
public function onUpdate(){
$this->entityBaseTick();
if($this->closed !== false){
return false;
}

View File

@ -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
*/

View File

@ -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();
}
/**

View File

@ -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);
}