CraftingManager: Reduce footprint of recipe keys

this was using json before, which is horribly inefficient.
This saved about 200 KB of memory on initial startup (which isn't much, but for more complex recipes, it might have been significantly worse.
This commit is contained in:
Dylan K. Taylor
2021-08-29 15:41:10 +01:00
parent c2558573e2
commit dee2062b1b

View File

@ -24,9 +24,11 @@ declare(strict_types=1);
namespace pocketmine\crafting;
use pocketmine\item\Item;
use pocketmine\nbt\LittleEndianNbtSerializer;
use pocketmine\nbt\TreeRoot;
use pocketmine\utils\BinaryStream;
use pocketmine\utils\DestructorCallbackTrait;
use pocketmine\utils\ObjectSet;
use function json_encode;
use function usort;
class CraftingManager{
@ -108,12 +110,16 @@ class CraftingManager{
private static function hashOutputs(array $outputs) : string{
$outputs = self::pack($outputs);
usort($outputs, [self::class, "sort"]);
$result = new BinaryStream();
foreach($outputs as $o){
//this reduces accuracy of hash, but it's necessary to deal with recipe book shift-clicking stupidity
$o->setCount(1);
//count is not written because the outputs might be from multiple repetitions of a single recipe
//this reduces the accuracy of the hash, but it won't matter in most cases.
$result->putVarInt($o->getId());
$result->putVarInt($o->getMeta());
$result->put((new LittleEndianNbtSerializer())->write(new TreeRoot($o->getNamedTag())));
}
return json_encode($outputs);
return $result->getBuffer();
}
/**