Skip to content

Commit

Permalink
Do not store a pre-compressed cache for crafting data
Browse files Browse the repository at this point in the history
this reduces bandwidth efficiency because it can't be compressed with everything else this way. If we want to cache stuff sent during the login sequence, it's better to stuff a bunch of stuff into a batch (e.g. crafting, creative, actor ids, biome defs) and pre-compress that as one big package instead.
  • Loading branch information
dktapps committed Nov 11, 2020
1 parent 8f36957 commit c4d35d5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
32 changes: 11 additions & 21 deletions src/crafting/CraftingManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@
namespace pocketmine\crafting;

use pocketmine\item\Item;
use pocketmine\network\mcpe\compression\CompressBatchPromise;
use pocketmine\network\mcpe\compression\Compressor;
use pocketmine\network\mcpe\convert\TypeConverter;
use pocketmine\network\mcpe\protocol\CraftingDataPacket;
use pocketmine\network\mcpe\protocol\serializer\PacketBatch;
use pocketmine\network\mcpe\protocol\types\inventory\ItemStack;
use pocketmine\network\mcpe\protocol\types\recipe\FurnaceRecipe as ProtocolFurnaceRecipe;
use pocketmine\network\mcpe\protocol\types\recipe\RecipeIngredient;
Expand All @@ -39,7 +36,6 @@
use pocketmine\uuid\UUID;
use function array_map;
use function json_encode;
use function spl_object_id;
use function str_repeat;
use function usort;

Expand All @@ -52,20 +48,20 @@ class CraftingManager{
/** @var FurnaceRecipeManager */
protected $furnaceRecipeManager;

/** @var CompressBatchPromise[] */
private $craftingDataCaches = [];
/** @var CraftingDataPacket|null */
private $craftingDataCache = null;

public function __construct(){
$this->furnaceRecipeManager = new FurnaceRecipeManager();
$this->furnaceRecipeManager->getRecipeRegisteredCallbacks()->add(function(FurnaceRecipe $recipe) : void{
$this->craftingDataCaches = [];
$this->craftingDataCache = null;
});
}

/**
* Rebuilds the cached CraftingDataPacket.
*/
private function buildCraftingDataCache(Compressor $compressor) : CompressBatchPromise{
private function buildCraftingDataCache() : CraftingDataPacket{
Timings::$craftingDataCacheRebuildTimer->startTiming();
$pk = new CraftingDataPacket();
$pk->cleanRecipes = true;
Expand Down Expand Up @@ -126,24 +122,18 @@ private function buildCraftingDataCache(Compressor $compressor) : CompressBatchP
);
}

$promise = new CompressBatchPromise();
$promise->resolve($compressor->compress(PacketBatch::fromPackets($pk)->getBuffer()));

Timings::$craftingDataCacheRebuildTimer->stopTiming();
return $promise;
return $pk;
}

/**
* Returns a pre-compressed CraftingDataPacket for sending to players. Rebuilds the cache if it is not found.
*/
public function getCraftingDataPacket(Compressor $compressor) : CompressBatchPromise{
$compressorId = spl_object_id($compressor);

if(!isset($this->craftingDataCaches[$compressorId])){
$this->craftingDataCaches[$compressorId] = $this->buildCraftingDataCache($compressor);
public function getCraftingDataPacket() : CraftingDataPacket{
if($this->craftingDataCache === null){
$this->craftingDataCache = $this->buildCraftingDataCache();
}

return $this->craftingDataCaches[$compressorId];
return $this->craftingDataCache;
}

/**
Expand Down Expand Up @@ -215,13 +205,13 @@ public function getFurnaceRecipeManager() : FurnaceRecipeManager{
public function registerShapedRecipe(ShapedRecipe $recipe) : void{
$this->shapedRecipes[self::hashOutputs($recipe->getResults())][] = $recipe;

$this->craftingDataCaches = [];
$this->craftingDataCache = null;
}

public function registerShapelessRecipe(ShapelessRecipe $recipe) : void{
$this->shapelessRecipes[self::hashOutputs($recipe->getResults())][] = $recipe;

$this->craftingDataCaches = [];
$this->craftingDataCache = null;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/network/mcpe/handler/PreSpawnPacketHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function setUp() : void{
$this->session->getInvManager()->syncAll();
$this->session->getInvManager()->syncCreative();
$this->session->getInvManager()->syncSelectedHotbarSlot();
$this->session->queueCompressed($this->server->getCraftingManager()->getCraftingDataPacket($this->session->getCompressor()));
$this->session->sendDataPacket($this->server->getCraftingManager()->getCraftingDataPacket());

$this->session->syncPlayerList($this->server->getOnlinePlayers());
}
Expand Down

0 comments on commit c4d35d5

Please sign in to comment.