bonemeal on sugarcane tweaks, using block send queue to send UpdateBlockPackets

This commit is contained in:
GameHerobrine 2023-12-31 17:42:59 +03:00
parent 7ecfcacec6
commit 229a563ff5
8 changed files with 84 additions and 70 deletions

View file

@ -536,15 +536,13 @@ class BlockAPI{
if($block === false){
return false;
}
if($type === BLOCK_UPDATE_RANDOM) return false; //I HATE U NO
$level = $block->onUpdate($type);
if($level === BLOCK_UPDATE_NORMAL){
$this->blockUpdateAround($block, $level);
$this->server->api->entity->updateRadius($pos, 1);
}elseif($level === BLOCK_UPDATE_RANDOM){
//$this->nextRandomUpdate($pos); old version, change back if any issues will start to happen with this one(please note that old version makes a lot of lag)
//NO $this->scheduleBlockUpdate($pos, (mt_rand(25, 75) + Utils::getRandomUpdateTicks() * 0.05) / 0.05, BLOCK_UPDATE_RANDOM);
}
return $level;
}
public function blockUpdateAround(Position $pos, $type = BLOCK_UPDATE_NORMAL, $delay = false){

View file

@ -72,6 +72,10 @@ class Player{
public $entityMovementQueue;
public $entityMovementQueueLength = 0;
public $blockUpdateQueue;
public $blockUpdateQueueLength = 0;
/**
* @param integer $clientID
* @param string $ip
@ -96,10 +100,16 @@ class Player{
$this->slot = 0;
$this->hotbar = [0, -1, -1, -1, -1, -1, -1, -1, -1];
$this->packetStats = [0, 0];
$this->buffer = new RakNetPacket(RakNetInfo::DATA_PACKET_0);
$this->buffer->data = [];
$this->entityMovementQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0);
$this->entityMovementQueue->data = [];
$this->buffer->data = [];
$this->blockUpdateQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0);
$this->blockUpdateQueue->data = [];
$this->server->schedule(1, [$this, "handlePacketQueues"], [], true);
$this->server->schedule(20 * 60, [$this, "clearQueue"], [], true);
$this->evid[] = $this->server->event("server.close", [$this, "close"]);
@ -1142,7 +1152,7 @@ class Player{
}
}
public function sendEntityMovementUpdateQueue(){
if($this->entityMovementQueueLength > 0 and $this->entityMovementQueue instanceof RakNetPacket){
if($this->entityMovementQueueLength > 0 && $this->entityMovementQueue instanceof RakNetPacket){
$this->entityMovementQueue->seqNumber = $this->counter[0]++;
$this->send($this->entityMovementQueue);
}
@ -1150,6 +1160,39 @@ class Player{
$this->entityMovementQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0);
$this->entityMovementQueue->data = [];
}
public function sendBlockUpdateQueue(){
if($this->blockUpdateQueueLength > 0 && $this->blockUpdateQueue instanceof RakNetPacket){
$this->blockUpdateQueue->seqNumber = $this->counter[0]++;
$this->send($this->blockUpdateQueue);
}
$this->blockUpdateQueueLength = 0;
$this->blockUpdateQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0);
$this->blockUpdateQueue->data = [];
}
public function addBlockUpdateIntoQueue($x, $y, $z, $id, $meta){
$packet = new UpdateBlockPacket();
$packet->x = $x;
$packet->y = $y;
$packet->z = $z;
$packet->block = $id;
$packet->meta = $meta;
$packet->encode();
$len = 1 + strlen($packet->buffer);
$MTU = $this->MTU - 24;
if(($this->blockUpdateQueueLength + $len) >= $MTU){
$this->sendBlockUpdateQueue();
}
$packet->messageIndex = $this->counter[3]++;
$packet->reliability = 2;
@$this->blockUpdateQueue->data[] = $packet;
$this->blockUpdateQueueLength = 6 + $len;
}
public function addEntityMovementUpdateToQueue(Entity $e){
$len = 0;
$packets = 0;

View file

@ -133,12 +133,12 @@ abstract class Block extends Position{
INFO_UPDATE2 => "InfoUpdate2Block",
RESERVED6 => "Reserved6Block",
);
protected $id;
protected $meta;
protected $name;
protected $breakTime;
public $id;
public $meta;
public $name;
public $breakTime;
public $boundingBox;
protected $hardness;
public $hardness;
public $isActivable = false;
public $breakable = true;
public $isFlowable = false;

View file

@ -8,24 +8,24 @@ class SugarcaneBlock extends FlowableBlock{
}
public function getDrops(Item $item, Player $player){
return array(
array(SUGARCANE, 0, 1),
);
return [
[SUGARCANE, 0, 1],
];
}
public function onActivate(Item $item, Player $player){
if($item->getID() === DYE and $item->getMetadata() === 0x0F){ //Bonemeal
if($this->getSide(0)->getID() !== SUGARCANE_BLOCK){
if($this->level->level->getBlockID($this->x, $this->y - 1, $this->z) != SUGARCANE_BLOCK){
for($y = 1; $y < 3; ++$y){
$b = $this->level->getBlock(new Vector3($this->x, $this->y + $y, $this->z));
if($b->getID() === AIR){
$this->level->setBlock($b, new SugarcaneBlock(), true, false, true);
break;
$b = $this->level->level->getBlockID($this->x, $this->y + $y, $this->z);
if($b == AIR){
$this->level->fastSetBlockUpdate($this->x, $this->y + $y, $this->z, SUGARCANE_BLOCK, 0, true);
}
}
$this->meta = 0;
$this->level->setBlock($this, $this, true, false, true);
$this->level->fastSetBlockUpdateMeta($this->x, $this->y, $this->z, $this->meta, true);
}
if(($player->gamemode & 0x01) === 0){
$player->removeItem(DYE,0x0F,1);
}

View file

@ -30,10 +30,6 @@ class DirtBlock extends SolidBlock{
}*/
}
public function getBlockID($x, $y, $z){
return $this->level->getBlock(new Vector3($x, $y, $z))->getID();
}
public static function getGrassInRadius(Level $level, $x, $y, $z){ //umwut (although kinda faster than for loop =D)
if($level->level->getBlockID($x+1, $y, $z+1) == 2) return true;
if($level->level->getBlockID($x+1, $y, $z) == 2) return true;

View file

@ -22,7 +22,7 @@ class RailLogic
$this->updateConnections($meta);
}
public function getRail($v){
if(($b = $this->level->getBlock($v)) instanceof RailBaseBlock){
if(($b = $this->level->getBlock($v)) instanceof RailBaseBlock){ //TODO git rid of getBlock
return new RailLogic($b);
}elseif(($b = $this->level->getBlockWithoutVector($v->x, $v->y + 1, $v->z)) instanceof RailBaseBlock){
return new RailLogic($b);

View file

@ -80,6 +80,9 @@ class Utils{
return floor(($v % 360 + 360) % 360);
}
/**
* PHP8 has internal function for doing it: {@link str_ends_with}
*/
public static function endsWith($str, $check) {
return substr($str, -strlen($check)) === $check;
}
@ -100,26 +103,6 @@ class Utils{
return $num >= $min && $num <= $max;
}
public function xrange($start, $limit, $step = 1) {
if ($start <= $limit) {
if ($step <= 0) {
throw new LogicException('Step must be positive');
}
for ($i = $start; $i <= $limit; $i += $step) {
yield $i;
}
} else {
if ($step >= 0) {
throw new LogicException('Step must be negative');
}
for ($i = $start; $i >= $limit; $i += $step) {
yield $i;
}
}
}
public static function getUniqueID($raw = false, $extra = ""){
$machine = php_uname("a");
$machine .= file_exists("/proc/cpuinfo") ? `cat /proc/cpuinfo | grep "model name"` : "";
@ -463,10 +446,6 @@ class Utils{
return $data;
}
public static function php8_nulcoal(&$arr, $else){
return isset($arr) ? $arr : $else;
}
public static function readTriad($str){
return @unpack("N", "\x00" . $str)[1];
}

View file

@ -17,6 +17,7 @@ class Level{
private $time, $startCheck, $startTime, $server, $name, $usedChunks, $changedBlocks, $changedCount, $stopTime;
public $randInt1, $randInt2;
public $queuedBlockUpdates = [];
public static $randomUpdateBlocks = [
FIRE => true,
@ -321,13 +322,7 @@ class Level{
}
public function fastSetBlockUpdateMeta($x, $y, $z, $meta, $updateBlock = false){
$this->level->setBlockDamage($x, $y, $z, $meta);
$pk = new UpdateBlockPacket;
$pk->x = $x;
$pk->y = $y;
$pk->z = $z;
$pk->block = $this->level->getBlockID($x, $y, $z);
$pk->meta = $meta;
$this->server->api->player->broadcastPacket($this->players, $pk);
$this->addBlockToSendQueue($x, $y, $z, $this->level->getBlockID($x, $y, $z), $meta);
if($updateBlock){
$this->server->api->block->blockUpdateAround(new Position($x, $y, $z, $this), BLOCK_UPDATE_NORMAL, 1);
}
@ -335,13 +330,7 @@ class Level{
public function fastSetBlockUpdate($x, $y, $z, $id, $meta, $updateBlocksAround = false){
$this->level->setBlock($x, $y, $z, $id, $meta);
$pk = new UpdateBlockPacket;
$pk->x = $x;
$pk->y = $y;
$pk->z = $z;
$pk->block = $id;
$pk->meta = $meta;
$this->server->api->player->broadcastPacket($this->players, $pk);
$this->addBlockToSendQueue($x, $y, $z, $id, $meta);
if($updateBlocksAround){
$this->server->api->block->blockUpdateAround(new Position($x, $y, $z, $this), BLOCK_UPDATE_NORMAL, 1);
}
@ -392,6 +381,17 @@ class Level{
$player->addEntityMovementUpdateToQueue($e);
}
$player->sendEntityMovementUpdateQueue();
foreach($this->queuedBlockUpdates as $update){
$x = $update[0];
$y = $update[1];
$z = $update[2];
$idmeta = $this->level->getBlock($x, $y, $z);
$id = $idmeta[0];
$meta = $idmeta[1];
$player->addBlockUpdateIntoQueue($x, $y, $z, $id, $meta);
}
$player->sendBlockUpdateQueue();
}
@ -402,6 +402,10 @@ class Level{
}
}
public function addBlockToSendQueue($x, $y, $z, $id, $meta){
$this->queuedBlockUpdates["$x $y $z"] = [$x, $y, $z, $id, $meta];
}
public function setBlock(Vector3 $pos, Block $block, $update = true, $tiles = false, $direct = false){
if(!isset($this->level) or (($pos instanceof Position) and $pos->level !== $this) or $pos->x < 0 or $pos->y < 0 or $pos->z < 0){
return false;
@ -413,13 +417,7 @@ class Level{
}
$block->position($pos);
if($direct === true){
$pk = new UpdateBlockPacket;
$pk->x = $pos->x;
$pk->y = $pos->y;
$pk->z = $pos->z;
$pk->block = $block->getID();
$pk->meta = $block->getMetadata();
$this->server->api->player->broadcastPacket($this->players, $pk);
$this->addBlockToSendQueue($pos->x, $pos->y, $pos->z, $pos->id, $pos->meta);
}else{
$i = ($pos->x >> 4) . ":" . ($pos->y >> 4) . ":" . ($pos->z >> 4);
if(!isset($this->changedBlocks[$i])){