mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-07 10:31:51 +00:00
Fill null UUIDs in CraftingDataPacket, remove all UUID things from CraftingRecipe
This allows deleting lots of code, and additionally provides a huge reduction in the compressed size of CraftingDataPacket. Since we don't care about these UUIDs (they are only used in CraftingEventPacket, which is broken and unused in PM) we fill them with zeros instead.
This commit is contained in:
parent
a1090623a2
commit
ec332e3e60
@ -30,13 +30,8 @@ use pocketmine\network\mcpe\protocol\CraftingDataPacket;
|
|||||||
use pocketmine\Server;
|
use pocketmine\Server;
|
||||||
use pocketmine\timings\Timings;
|
use pocketmine\timings\Timings;
|
||||||
use pocketmine\utils\Config;
|
use pocketmine\utils\Config;
|
||||||
use pocketmine\utils\UUID;
|
|
||||||
|
|
||||||
class CraftingManager{
|
class CraftingManager{
|
||||||
|
|
||||||
/** @var CraftingRecipe[] */
|
|
||||||
protected $recipes = [];
|
|
||||||
|
|
||||||
/** @var ShapedRecipe[][] */
|
/** @var ShapedRecipe[][] */
|
||||||
protected $shapedRecipes = [];
|
protected $shapedRecipes = [];
|
||||||
/** @var ShapelessRecipe[][] */
|
/** @var ShapelessRecipe[][] */
|
||||||
@ -44,8 +39,6 @@ class CraftingManager{
|
|||||||
/** @var FurnaceRecipe[] */
|
/** @var FurnaceRecipe[] */
|
||||||
protected $furnaceRecipes = [];
|
protected $furnaceRecipes = [];
|
||||||
|
|
||||||
private static $RECIPE_COUNT = 0;
|
|
||||||
|
|
||||||
/** @var BatchPacket */
|
/** @var BatchPacket */
|
||||||
private $craftingDataCache;
|
private $craftingDataCache;
|
||||||
|
|
||||||
@ -93,13 +86,16 @@ class CraftingManager{
|
|||||||
$pk = new CraftingDataPacket();
|
$pk = new CraftingDataPacket();
|
||||||
$pk->cleanRecipes = true;
|
$pk->cleanRecipes = true;
|
||||||
|
|
||||||
foreach($this->recipes as $recipe){
|
foreach($this->shapelessRecipes as $list){
|
||||||
if($recipe instanceof ShapedRecipe){
|
foreach($list as $recipe){
|
||||||
$pk->addShapedRecipe($recipe);
|
|
||||||
}elseif($recipe instanceof ShapelessRecipe){
|
|
||||||
$pk->addShapelessRecipe($recipe);
|
$pk->addShapelessRecipe($recipe);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
foreach($this->shapedRecipes as $list){
|
||||||
|
foreach($list as $recipe){
|
||||||
|
$pk->addShapedRecipe($recipe);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
foreach($this->furnaceRecipes as $recipe){
|
foreach($this->furnaceRecipes as $recipe){
|
||||||
$pk->addFurnaceRecipe($recipe);
|
$pk->addFurnaceRecipe($recipe);
|
||||||
@ -179,22 +175,6 @@ class CraftingManager{
|
|||||||
return json_encode($outputs);
|
return json_encode($outputs);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param UUID $id
|
|
||||||
* @return CraftingRecipe|null
|
|
||||||
*/
|
|
||||||
public function getRecipe(UUID $id) : ?CraftingRecipe{
|
|
||||||
$index = $id->toBinary();
|
|
||||||
return $this->recipes[$index] ?? null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return Recipe[]
|
|
||||||
*/
|
|
||||||
public function getRecipes() : array{
|
|
||||||
return $this->recipes;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return ShapelessRecipe[][]
|
* @return ShapelessRecipe[][]
|
||||||
*/
|
*/
|
||||||
@ -220,8 +200,6 @@ class CraftingManager{
|
|||||||
* @param ShapedRecipe $recipe
|
* @param ShapedRecipe $recipe
|
||||||
*/
|
*/
|
||||||
public function registerShapedRecipe(ShapedRecipe $recipe) : void{
|
public function registerShapedRecipe(ShapedRecipe $recipe) : void{
|
||||||
$recipe->setId($uuid = UUID::fromData((string) ++self::$RECIPE_COUNT, json_encode(self::pack($recipe->getResults()))));
|
|
||||||
$this->recipes[$uuid->toBinary()] = $recipe;
|
|
||||||
$this->shapedRecipes[self::hashOutputs($recipe->getResults())][] = $recipe;
|
$this->shapedRecipes[self::hashOutputs($recipe->getResults())][] = $recipe;
|
||||||
|
|
||||||
$this->craftingDataCache = null;
|
$this->craftingDataCache = null;
|
||||||
@ -231,8 +209,6 @@ class CraftingManager{
|
|||||||
* @param ShapelessRecipe $recipe
|
* @param ShapelessRecipe $recipe
|
||||||
*/
|
*/
|
||||||
public function registerShapelessRecipe(ShapelessRecipe $recipe) : void{
|
public function registerShapelessRecipe(ShapelessRecipe $recipe) : void{
|
||||||
$recipe->setId($uuid = UUID::fromData((string) ++self::$RECIPE_COUNT, json_encode(self::pack($recipe->getResults()))));
|
|
||||||
$this->recipes[$uuid->toBinary()] = $recipe;
|
|
||||||
$this->shapelessRecipes[self::hashOutputs($recipe->getResults())][] = $recipe;
|
$this->shapelessRecipes[self::hashOutputs($recipe->getResults())][] = $recipe;
|
||||||
|
|
||||||
$this->craftingDataCache = null;
|
$this->craftingDataCache = null;
|
||||||
|
@ -24,20 +24,8 @@ declare(strict_types=1);
|
|||||||
namespace pocketmine\inventory;
|
namespace pocketmine\inventory;
|
||||||
|
|
||||||
use pocketmine\item\Item;
|
use pocketmine\item\Item;
|
||||||
use pocketmine\utils\UUID;
|
|
||||||
|
|
||||||
interface CraftingRecipe extends Recipe{
|
interface CraftingRecipe extends Recipe{
|
||||||
|
|
||||||
/**
|
|
||||||
* @return UUID|null
|
|
||||||
*/
|
|
||||||
public function getId() : ?UUID;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param UUID $id
|
|
||||||
*/
|
|
||||||
public function setId(UUID $id);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of items needed to craft this recipe. This MUST NOT include Air items or items with a zero count.
|
* Returns a list of items needed to craft this recipe. This MUST NOT include Air items or items with a zero count.
|
||||||
*
|
*
|
||||||
|
@ -25,12 +25,8 @@ namespace pocketmine\inventory;
|
|||||||
|
|
||||||
use pocketmine\item\Item;
|
use pocketmine\item\Item;
|
||||||
use pocketmine\item\ItemFactory;
|
use pocketmine\item\ItemFactory;
|
||||||
use pocketmine\utils\UUID;
|
|
||||||
|
|
||||||
class ShapedRecipe implements CraftingRecipe{
|
class ShapedRecipe implements CraftingRecipe{
|
||||||
/** @var UUID|null */
|
|
||||||
private $id = null;
|
|
||||||
|
|
||||||
/** @var string[] */
|
/** @var string[] */
|
||||||
private $shape = [];
|
private $shape = [];
|
||||||
/** @var Item[] char => Item map */
|
/** @var Item[] char => Item map */
|
||||||
@ -116,21 +112,6 @@ class ShapedRecipe implements CraftingRecipe{
|
|||||||
return $this->getResults();
|
return $this->getResults();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return UUID|null
|
|
||||||
*/
|
|
||||||
public function getId() : ?UUID{
|
|
||||||
return $this->id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setId(UUID $id){
|
|
||||||
if($this->id !== null){
|
|
||||||
throw new \InvalidStateException("Id is already set");
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->id = $id;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $key
|
* @param string $key
|
||||||
* @param Item $item
|
* @param Item $item
|
||||||
|
@ -24,12 +24,8 @@ declare(strict_types=1);
|
|||||||
namespace pocketmine\inventory;
|
namespace pocketmine\inventory;
|
||||||
|
|
||||||
use pocketmine\item\Item;
|
use pocketmine\item\Item;
|
||||||
use pocketmine\utils\UUID;
|
|
||||||
|
|
||||||
class ShapelessRecipe implements CraftingRecipe{
|
class ShapelessRecipe implements CraftingRecipe{
|
||||||
/** @var UUID|null */
|
|
||||||
private $id = null;
|
|
||||||
|
|
||||||
/** @var Item[] */
|
/** @var Item[] */
|
||||||
private $ingredients = [];
|
private $ingredients = [];
|
||||||
/** @var Item[] */
|
/** @var Item[] */
|
||||||
@ -48,24 +44,6 @@ class ShapelessRecipe implements CraftingRecipe{
|
|||||||
$this->results = array_map(function(Item $item) : Item{ return clone $item; }, $results);
|
$this->results = array_map(function(Item $item) : Item{ return clone $item; }, $results);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return UUID|null
|
|
||||||
*/
|
|
||||||
public function getId() : ?UUID{
|
|
||||||
return $this->id;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param UUID $id
|
|
||||||
*/
|
|
||||||
public function setId(UUID $id){
|
|
||||||
if($this->id !== null){
|
|
||||||
throw new \InvalidStateException("Id is already set");
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->id = $id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getResults() : array{
|
public function getResults() : array{
|
||||||
return array_map(function(Item $item) : Item{ return clone $item; }, $this->results);
|
return array_map(function(Item $item) : Item{ return clone $item; }, $this->results);
|
||||||
}
|
}
|
||||||
|
@ -139,7 +139,7 @@ class CraftingDataPacket extends DataPacket{
|
|||||||
$stream->putSlot($item);
|
$stream->putSlot($item);
|
||||||
}
|
}
|
||||||
|
|
||||||
$stream->putUUID($recipe->getId());
|
$stream->put(str_repeat("\x00", 16)); //Null UUID
|
||||||
|
|
||||||
return CraftingDataPacket::ENTRY_SHAPELESS;
|
return CraftingDataPacket::ENTRY_SHAPELESS;
|
||||||
}
|
}
|
||||||
@ -160,7 +160,7 @@ class CraftingDataPacket extends DataPacket{
|
|||||||
$stream->putSlot($item);
|
$stream->putSlot($item);
|
||||||
}
|
}
|
||||||
|
|
||||||
$stream->putUUID($recipe->getId());
|
$stream->put(str_repeat("\x00", 16)); //Null UUID
|
||||||
|
|
||||||
return CraftingDataPacket::ENTRY_SHAPED;
|
return CraftingDataPacket::ENTRY_SHAPED;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user