Files
PocketMine-MP/src/pocketmine/inventory/CraftingManager.php
Dylan K. Taylor 8572e9e560 Crafting: nuke
This commit brings in a much-needed rewrite of crafting transaction handling.

The following classes have been removed:
- CraftingTransferMaterialAction
- CraftingTakeResultAction

The following classes have significant changes:
- CraftingTransaction
	- All API methods have been removed and are now handled in CraftItemEvent
- CraftItemEvent
	- added the following:
		- getInputs()
		- getOutputs()
		- getRepetitions() (tells how many times a recipe was crafted in this event)
- Recipe interface:
	- Removed getResult() (individual recipes may handle this differently)
- CraftingRecipe interface
	- removed the following:
		- matchItems()
		- getExtraResults()
		- getAllResults()
	- added the following
		- getResults()
		- getIngredientList() : Item[], which must return a 1D array of items that should be consumed (wildcards accepted).
		- matchesCraftingGrid(CraftingGrid)
- ShapedRecipe
	- constructor now accepts string[], Item[], Item[]
- ShapelessRecipe
	- constructor now accepts Item[], Item[]
2018-03-29 12:05:22 +01:00

296 lines
7.6 KiB
PHP

<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\inventory;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
use pocketmine\network\mcpe\protocol\BatchPacket;
use pocketmine\network\mcpe\protocol\CraftingDataPacket;
use pocketmine\Server;
use pocketmine\timings\Timings;
use pocketmine\utils\Config;
use pocketmine\utils\UUID;
class CraftingManager{
/** @var CraftingRecipe[] */
protected $recipes = [];
/** @var ShapedRecipe[][] */
protected $shapedRecipes = [];
/** @var ShapelessRecipe[][] */
protected $shapelessRecipes = [];
/** @var FurnaceRecipe[] */
protected $furnaceRecipes = [];
private static $RECIPE_COUNT = 0;
/** @var BatchPacket */
private $craftingDataCache;
public function __construct(){
$this->init();
}
public function init() : void{
$recipes = new Config(\pocketmine\RESOURCE_PATH . "recipes.json", Config::JSON, []);
foreach($recipes->getAll() as $recipe){
switch($recipe["type"]){
case 0:
$this->registerRecipe(new ShapelessRecipe(
array_map(function(array $data) : Item{ return Item::jsonDeserialize($data); }, $recipe["input"]),
array_map(function(array $data) : Item{ return Item::jsonDeserialize($data); }, $recipe["output"])
));
break;
case 1:
$this->registerRecipe(new ShapedRecipe(
$recipe["shape"],
array_map(function(array $data) : Item{ return Item::jsonDeserialize($data); }, $recipe["input"]),
array_map(function(array $data) : Item{ return Item::jsonDeserialize($data); }, $recipe["output"])
));
break;
case 2:
case 3:
$result = $recipe["output"];
$resultItem = Item::jsonDeserialize($result);
$this->registerRecipe(new FurnaceRecipe($resultItem, ItemFactory::get($recipe["inputId"], $recipe["inputDamage"] ?? -1, 1)));
break;
default:
break;
}
}
$this->buildCraftingDataCache();
}
/**
* Rebuilds the cached CraftingDataPacket.
*/
public function buildCraftingDataCache() : void{
Timings::$craftingDataCacheRebuildTimer->startTiming();
$pk = new CraftingDataPacket();
$pk->cleanRecipes = true;
foreach($this->recipes as $recipe){
if($recipe instanceof ShapedRecipe){
$pk->addShapedRecipe($recipe);
}elseif($recipe instanceof ShapelessRecipe){
$pk->addShapelessRecipe($recipe);
}
}
foreach($this->furnaceRecipes as $recipe){
$pk->addFurnaceRecipe($recipe);
}
$pk->encode();
$batch = new BatchPacket();
$batch->addPacket($pk);
$batch->setCompressionLevel(Server::getInstance()->networkCompressionLevel);
$batch->encode();
$this->craftingDataCache = $batch;
Timings::$craftingDataCacheRebuildTimer->stopTiming();
}
/**
* Returns a pre-compressed CraftingDataPacket for sending to players. Rebuilds the cache if it is not found.
*
* @return BatchPacket
*/
public function getCraftingDataPacket() : BatchPacket{
if($this->craftingDataCache === null){
$this->buildCraftingDataCache();
}
return $this->craftingDataCache;
}
/**
* Function used to arrange Shapeless Recipe ingredient lists into a consistent order.
*
* @param Item $i1
* @param Item $i2
*
* @return int
*/
public static function sort(Item $i1, Item $i2){
//Use spaceship operator to compare each property, then try the next one if they are equivalent.
($retval = $i1->getId() <=> $i2->getId()) === 0 && ($retval = $i1->getDamage() <=> $i2->getDamage()) === 0 && ($retval = $i1->getCount() <=> $i2->getCount());
return $retval;
}
/**
* @param Item[] $items
*
* @return Item[]
*/
private static function pack(array $items) : array{
/** @var Item[] $result */
$result = [];
foreach($items as $i => $item){
foreach($result as $otherItem){
if($item->equals($otherItem)){
$otherItem->setCount($otherItem->getCount() + $item->getCount());
continue 2;
}
}
//No matching item found
$result[] = clone $item;
}
return $result;
}
private static function hashOutputs(array $outputs) : string{
$outputs = self::pack($outputs);
usort($outputs, [self::class, "sort"]);
foreach($outputs as $o){
//this reduces accuracy of hash, but it's necessary to deal with recipe book shift-clicking stupidity
$o->setCount(1);
}
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[][]
*/
public function getShapelessRecipes() : array{
return $this->shapelessRecipes;
}
/**
* @return ShapedRecipe[][]
*/
public function getShapedRecipes() : array{
return $this->shapedRecipes;
}
/**
* @return FurnaceRecipe[]
*/
public function getFurnaceRecipes() : array{
return $this->furnaceRecipes;
}
/**
* @param ShapedRecipe $recipe
*/
public function registerShapedRecipe(ShapedRecipe $recipe) : void{
$this->shapedRecipes[self::hashOutputs($recipe->getResults())][] = $recipe;
$this->craftingDataCache = null;
}
/**
* @param ShapelessRecipe $recipe
*/
public function registerShapelessRecipe(ShapelessRecipe $recipe) : void{
$this->shapelessRecipes[self::hashOutputs($recipe->getResults())][] = $recipe;
$this->craftingDataCache = null;
}
/**
* @param FurnaceRecipe $recipe
*/
public function registerFurnaceRecipe(FurnaceRecipe $recipe) : void{
$input = $recipe->getInput();
$this->furnaceRecipes[$input->getId() . ":" . ($input->hasAnyDamageValue() ? "?" : $input->getDamage())] = $recipe;
$this->craftingDataCache = null;
}
/**
* @param CraftingGrid $grid
* @param Item[] $outputs
*
* @return CraftingRecipe|null
*/
public function matchRecipe(CraftingGrid $grid, array $outputs) : ?CraftingRecipe{
//TODO: try to match special recipes before anything else (first they need to be implemented!)
$outputHash = self::hashOutputs($outputs);
if(isset($this->shapedRecipes[$outputHash])){
foreach($this->shapedRecipes[$outputHash] as $recipe){
if($recipe->matchesCraftingGrid($grid)){
return $recipe;
}
}
}
if(isset($this->shapelessRecipes[$outputHash])){
foreach($this->shapelessRecipes[$outputHash] as $recipe){
if($recipe->matchesCraftingGrid($grid)){
return $recipe;
}
}
}
return null;
}
/**
* @param Item $input
*
* @return FurnaceRecipe|null
*/
public function matchFurnaceRecipe(Item $input) : ?FurnaceRecipe{
return $this->furnaceRecipes[$input->getId() . ":" . $input->getDamage()] ?? $this->furnaceRecipes[$input->getId() . ":?"] ?? null;
}
/**
* @param Recipe $recipe
*/
public function registerRecipe(Recipe $recipe) : void{
if($recipe instanceof CraftingRecipe){
$recipe->setId($uuid = UUID::fromData((string) ++self::$RECIPE_COUNT, json_encode(self::pack($recipe->getResults()))));
$this->recipes[$uuid->toBinary()] = $recipe;
}
$recipe->registerToCraftingManager($this);
}
}