diff --git a/src/pocketmine/Server.php b/src/pocketmine/Server.php index 881cf0c18..63617af9a 100644 --- a/src/pocketmine/Server.php +++ b/src/pocketmine/Server.php @@ -2281,7 +2281,7 @@ class Server{ $this->nextTick = microtime(true); while($this->isRunning){ $this->tick(); - $next = $this->nextTick - 0.001; + $next = $this->nextTick - 0.0001; if($next > microtime(true)){ try{ time_sleep_until($next); @@ -2547,8 +2547,9 @@ class Server{ if(($this->nextTick - $tickTime) < -1){ $this->nextTick = $tickTime; + }else{ + $this->nextTick += 0.05; } - $this->nextTick += 0.05; return true; } diff --git a/src/pocketmine/command/CommandReader.php b/src/pocketmine/command/CommandReader.php index a2783c15a..02a6dddc2 100644 --- a/src/pocketmine/command/CommandReader.php +++ b/src/pocketmine/command/CommandReader.php @@ -57,9 +57,7 @@ class CommandReader extends Thread{ */ public function getLine(){ if($this->buffer->count() !== 0){ - return $this->buffer->synchronized(function (){ - return $this->buffer->shift(); - }); + return $this->buffer->shift(); } return null; @@ -76,9 +74,7 @@ class CommandReader extends Thread{ $lastLine = microtime(true); while(true){ if(($line = $this->readLine()) !== ""){ - $this->buffer->synchronized(function (\Threaded $buffer, $line){ - $buffer[] = preg_replace("#\\x1b\\x5b([^\\x1b]*\\x7e|[\\x40-\\x50])#", "", $line); - }, $this->buffer, $line); + $this->buffer[] = preg_replace("#\\x1b\\x5b([^\\x1b]*\\x7e|[\\x40-\\x50])#", "", $line); }elseif((microtime(true) - $lastLine) <= 0.1){ //Non blocking! Sleep to save CPU usleep(40000); } diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index 94430258f..1a193f40c 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -136,6 +136,8 @@ abstract class Entity extends Location implements Metadatable{ public $motionX; public $motionY; public $motionZ; + /** @var Vector3 */ + public $temporalVector; public $lastMotionX; public $lastMotionY; public $lastMotionZ; @@ -205,6 +207,8 @@ abstract class Entity extends Location implements Metadatable{ $this->timings = Timings::getEntityTimings($this); + $this->temporalVector = new Vector3(); + if($this->eyeHeight === null){ $this->eyeHeight = $this->height / 2 + 0.1; } diff --git a/src/pocketmine/entity/FallingSand.php b/src/pocketmine/entity/FallingSand.php index 5e72ca8e5..0bc6ba72f 100644 --- a/src/pocketmine/entity/FallingSand.php +++ b/src/pocketmine/entity/FallingSand.php @@ -135,7 +135,7 @@ class FallingSand extends Entity{ $this->updateMovement(); } - return $hasUpdate or !$this->onGround or $this->motionX != 0 or $this->motionY != 0 or $this->motionZ != 0; + return $hasUpdate or !$this->onGround or abs($this->motionX) > 0.00001 or abs($this->motionY) > 0.00001 or abs($this->motionZ) > 0.00001; } public function getBlock(){ diff --git a/src/pocketmine/entity/Item.php b/src/pocketmine/entity/Item.php index c8eac8cc4..bd58af67b 100644 --- a/src/pocketmine/entity/Item.php +++ b/src/pocketmine/entity/Item.php @@ -75,19 +75,16 @@ class Item extends Entity{ $this->server->getPluginManager()->callEvent(new ItemSpawnEvent($this)); } - - public function attack($damage, EntityDamageEvent $source){ - if($source->getCause() === EntityDamageEvent::CAUSE_FIRE_TICK){ - parent::attack($damage, $source); - } - } - public function onUpdate($currentTick){ if($this->closed){ return false; } - $tickDiff = max(1, $currentTick - $this->lastUpdate); + $tickDiff = $currentTick - $this->lastUpdate; + if($tickDiff <= 0){ + return false; + } + $this->lastUpdate = $currentTick; $this->timings->startTiming(); @@ -98,6 +95,9 @@ class Item extends Entity{ if($this->pickupDelay > 0 and $this->pickupDelay < 32767){ //Infinite delay $this->pickupDelay -= $tickDiff; + if($this->pickupDelay < 0){ + $this->pickupDelay = 0; + } } $this->motionY -= $this->gravity; @@ -108,8 +108,8 @@ class Item extends Entity{ $friction = 1 - $this->drag; - if($this->onGround and ($this->motionX != 0 or $this->motionZ != 0)){ - $friction = $this->getLevel()->getBlock(new Vector3($this->getFloorX(), $this->getFloorY() - 1, $this->getFloorZ()))->getFrictionFactor() * $friction; + if($this->onGround and (abs($this->motionX) > 0.00001 or abs($this->motionZ) > 0.00001)){ + $friction = $this->getLevel()->getBlock($this->temporalVector->setComponents((int) floor($this->x), (int) floor($this->y - 1), (int) floor($this->z) - 1))->getFrictionFactor() * $friction; } $this->motionX *= $friction; @@ -136,7 +136,7 @@ class Item extends Entity{ $this->timings->stopTiming(); - return $hasUpdate or !$this->onGround or $this->motionX != 0 or $this->motionY != 0 or $this->motionZ != 0; + return $hasUpdate or !$this->onGround or abs($this->motionX) > 0.00001 or abs($this->motionY) > 0.00001 or abs($this->motionZ) > 0.00001; } public function saveNBT(){ diff --git a/src/pocketmine/entity/PrimedTNT.php b/src/pocketmine/entity/PrimedTNT.php index 593f6ef1d..d25a52c00 100644 --- a/src/pocketmine/entity/PrimedTNT.php +++ b/src/pocketmine/entity/PrimedTNT.php @@ -113,7 +113,7 @@ class PrimedTNT extends Entity implements Explosive{ } - return $hasUpdate or $this->fuse >= 0 or $this->motionX != 0 or $this->motionY != 0 or $this->motionZ != 0; + return $hasUpdate or $this->fuse >= 0 or abs($this->motionX) > 0.00001 or abs($this->motionY) > 0.00001 or abs($this->motionZ) > 0.00001; } public function explode(){ diff --git a/src/pocketmine/entity/Projectile.php b/src/pocketmine/entity/Projectile.php index e6c37d8f0..a7c01dd6f 100644 --- a/src/pocketmine/entity/Projectile.php +++ b/src/pocketmine/entity/Projectile.php @@ -178,7 +178,7 @@ abstract class Projectile extends Entity{ $this->hadCollision = false; } - if(!$this->onGround or $this->motionX != 0 or $this->motionY != 0 or $this->motionZ != 0){ + if(!$this->onGround or abs($this->motionX) > 0.00001 or abs($this->motionY) > 0.00001 or abs($this->motionZ) > 0.00001){ $f = sqrt(($this->motionX ** 2) + ($this->motionZ ** 2)); $this->yaw = (atan2($this->motionX, $this->motionZ) * 180 / M_PI); $this->pitch = (atan2($this->motionY, $f) * 180 / M_PI); diff --git a/src/pocketmine/entity/Squid.php b/src/pocketmine/entity/Squid.php index 889a83bb7..d55bac496 100644 --- a/src/pocketmine/entity/Squid.php +++ b/src/pocketmine/entity/Squid.php @@ -143,7 +143,7 @@ class Squid extends WaterAnimal implements Ageable{ $this->timings->stopTiming(); - return $hasUpdate or !$this->onGround or $this->motionX != 0 or $this->motionY != 0 or $this->motionZ != 0; + return $hasUpdate or !$this->onGround or abs($this->motionX) > 0.00001 or abs($this->motionY) > 0.00001 or abs($this->motionZ) > 0.00001; } diff --git a/src/pocketmine/math/Vector3.php b/src/pocketmine/math/Vector3.php index c022463a4..d14efc802 100644 --- a/src/pocketmine/math/Vector3.php +++ b/src/pocketmine/math/Vector3.php @@ -53,15 +53,15 @@ class Vector3{ } public function getFloorX(){ - return (int) $this->x; + return (int) floor($this->x); } public function getFloorY(){ - return (int) $this->y; + return (int) floor($this->y); } public function getFloorZ(){ - return (int) $this->z; + return (int) floor($this->z); } public function getRight(){ diff --git a/src/pocketmine/utils/MainLogger.php b/src/pocketmine/utils/MainLogger.php index 10c505241..c7d549e71 100644 --- a/src/pocketmine/utils/MainLogger.php +++ b/src/pocketmine/utils/MainLogger.php @@ -46,7 +46,7 @@ class MainLogger extends \AttachableThreadedLogger{ touch($logFile); $this->logFile = $logFile; $this->logDebug = (bool) $logDebug; - $this->logStream = ""; + $this->logStream = \ThreadedFactory::create(); $this->start(); } @@ -190,11 +190,12 @@ class MainLogger extends \AttachableThreadedLogger{ $this->attachment->call($level, $message); } - $str = date("Y-m-d", $now) . " " . $cleanMessage . "\n"; - $this->synchronized(function($str){ - $this->logStream .= $str; - $this->notify(); - }, $str); + $this->logStream[] = date("Y-m-d", $now) . " " . $cleanMessage . "\n"; + if($this->logStream->count() === 1){ + $this->synchronized(function(){ + $this->notify(); + }); + } } public function run(){ @@ -206,13 +207,12 @@ class MainLogger extends \AttachableThreadedLogger{ while($this->shutdown === false){ $this->synchronized(function(){ - if(strlen($this->logStream) > 0){ - $chunk = $this->logStream; - $this->logStream = ""; + while($this->logStream->count() > 0){ + $chunk = $this->logStream->shift(); fwrite($this->logResource, $chunk); - }else{ - $this->wait(250000); } + + $this->wait(25000); }); }