From 3a1f5a28b16d70f8ae9a4f4c4339f8028b0aa950 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 28 Nov 2023 03:55:08 +0300 Subject: [PATCH] packets will send every tick, min_possible_speed is 1/8000 --- src/Player.php | 7 ++----- src/entity/Entity.php | 16 ++++++++++------ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/Player.php b/src/Player.php index 8234ac6f..6d32549e 100755 --- a/src/Player.php +++ b/src/Player.php @@ -97,7 +97,7 @@ class Player{ $this->packetStats = [0, 0]; $this->buffer = new RakNetPacket(RakNetInfo::DATA_PACKET_0); $this->buffer->data = []; - $this->server->schedule(2, [$this, "handlePacketQueues"], [], true); + $this->server->schedule(1, [$this, "handlePacketQueues"], [], true); $this->server->schedule(20 * 60, [$this, "clearQueue"], [], true); $this->evid[] = $this->server->event("server.close", [$this, "close"]); console("[DEBUG] New Session started with " . $ip . ":" . $port . ". MTU " . $this->MTU . ", Client ID " . $this->clientID, true, true, 2); @@ -334,7 +334,7 @@ class Player{ $this->bufferLen = 0; $this->buffer = new RakNetPacket(RakNetInfo::DATA_PACKET_0); $this->buffer->data = []; - $this->nextBuffer = microtime(true) + 0.1; + $this->nextBuffer = microtime(true) + 0.05; } /** @@ -781,9 +781,6 @@ class Player{ $pk = new SetEntityMotionPacket; //TODO one packet for all entities if possible $pk->entities = [[$data->eid, $data->speedX, $data->speedY, $data->speedZ]]; $this->dataPacket($pk); - $data->lastSpeedZ = $data->speedZ; - $data->lastSpeedY = $data->speedY; - $data->lastSpeedX = $data->speedX; break; case "entity.animate": diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 973e913b..1eff1113 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -5,6 +5,7 @@ class Entity extends Position const TYPE = - 1; const CLASS_TYPE = - 1; + const MIN_POSSIBLE_SPEED = 1/8000; //anything below will send 0 to player public $counter = 0; public $invincible, $status, $health, $fire, $crouched, $position, $meta, $stack; @@ -207,11 +208,11 @@ class Entity extends Position } public function isMovingHorizontally() { - return ($this->speedX > 0.01 || $this->speedX < - 0.01) || ($this->speedZ > 0.01 || $this->speedZ < - 0.01); + return ($this->speedX >= self::MIN_POSSIBLE_SPEED || $this->speedX <= -self::MIN_POSSIBLE_SPEED) || ($this->speedZ >= self::MIN_POSSIBLE_SPEED || $this->speedZ <= -self::MIN_POSSIBLE_SPEED); } public function isMoving() { - return $this->isMovingHorizontally() || ($this->speedY > 0.007 || $this->speedY < - 0.007); + return $this->isMovingHorizontally() || ($this->speedY > self::MIN_POSSIBLE_SPEED || $this->speedY < -self::MIN_POSSIBLE_SPEED); } public function setVelocity($vX, $vY = 0, $vZ = 0) @@ -571,13 +572,13 @@ class Entity extends Position } if(!$this->isPlayer()){ $update = false; - if(Utils::in_range($this->speedX, -0.01, 0.01)){ + if($this->speedX > -self::MIN_POSSIBLE_SPEED && $this->speedX < self::MIN_POSSIBLE_SPEED){ $this->speedX = 0; } - if(Utils::in_range($this->speedZ, -0.01, 0.01)){ + if($this->speedZ > -self::MIN_POSSIBLE_SPEED && $this->speedZ < self::MIN_POSSIBLE_SPEED){ $this->speedZ = 0; } - if(Utils::in_range($this->speedY, -0.007, 0.007)){ + if($this->speedY > -self::MIN_POSSIBLE_SPEED && $this->speedY < self::MIN_POSSIBLE_SPEED){ $this->speedY = 0; } @@ -672,7 +673,7 @@ class Entity extends Position $this->onGround = $support; if($this->hasGravity){ - $this->speedY -= $this->inWater ? 0.02 : $this->gravity; // TODO: replace with $gravity + $this->speedY -= $this->inWater ? 0.02 : $this->gravity; // TODO: fix packet spam $update = true; } elseif($this->lastX != $this->x || $this->lastZ != $this->z || $this->lastY != $this->z){ // $this->speedX = 0; @@ -688,6 +689,9 @@ class Entity extends Position $hasUpdate = true; if(($this->server->ticks % 4 === 0 && $this->class === ENTITY_ITEM) || $this->class != ENTITY_ITEM){ //update item speed every 4 ticks $this->server->api->handle("entity.motion", $this); + $this->lastSpeedZ = $this->speedZ; + $this->lastSpeedY = $this->speedY; + $this->lastSpeedX = $this->speedX; } } } elseif($this->player instanceof Player){