mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-16 18:59:00 +00:00
Utils: added cloneCallback() and cloneObjectArray() to reduce list copying boilerplate
This commit is contained in:
parent
47b120fa0e
commit
1e057394c0
@ -36,6 +36,7 @@ use pocketmine\math\AxisAlignedBB;
|
||||
use pocketmine\math\Facing;
|
||||
use pocketmine\math\Vector3;
|
||||
use pocketmine\player\Player;
|
||||
use pocketmine\utils\Utils;
|
||||
use pocketmine\world\BlockTransaction;
|
||||
use function assert;
|
||||
use function floor;
|
||||
@ -65,7 +66,7 @@ class Banner extends Transparent{
|
||||
}
|
||||
|
||||
public function __clone(){
|
||||
$this->patterns = $this->patterns->map(function(BannerPattern $pattern) : BannerPattern{ return clone $pattern; });
|
||||
$this->patterns = $this->patterns->map(Utils::cloneCallback());
|
||||
}
|
||||
|
||||
public function getId() : int{
|
||||
|
@ -24,7 +24,7 @@ declare(strict_types=1);
|
||||
namespace pocketmine\block;
|
||||
|
||||
use pocketmine\utils\RegistryTrait;
|
||||
use function array_map;
|
||||
use pocketmine\utils\Utils;
|
||||
use function assert;
|
||||
|
||||
/**
|
||||
@ -689,7 +689,7 @@ final class VanillaBlocks{
|
||||
* @return Block[]
|
||||
*/
|
||||
public static function getAll() : array{
|
||||
return array_map(function(Block $member){ return clone $member; }, self::_registryGetAll());
|
||||
return Utils::cloneObjectArray(self::_registryGetAll());
|
||||
}
|
||||
|
||||
protected static function setup() : void{
|
||||
|
@ -25,7 +25,7 @@ namespace pocketmine\crafting;
|
||||
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\item\ItemFactory;
|
||||
use function array_map;
|
||||
use pocketmine\utils\Utils;
|
||||
use function array_values;
|
||||
use function count;
|
||||
use function implode;
|
||||
@ -95,7 +95,7 @@ class ShapedRecipe implements CraftingRecipe{
|
||||
$this->ingredientList[$char] = clone $i;
|
||||
}
|
||||
|
||||
$this->results = array_map(function(Item $item) : Item{ return clone $item; }, $results);
|
||||
$this->results = Utils::cloneObjectArray($results);
|
||||
}
|
||||
|
||||
public function getWidth() : int{
|
||||
@ -110,7 +110,7 @@ class ShapedRecipe implements CraftingRecipe{
|
||||
* @return Item[]
|
||||
*/
|
||||
public function getResults() : array{
|
||||
return array_map(function(Item $item) : Item{ return clone $item; }, $this->results);
|
||||
return Utils::cloneObjectArray($this->results);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -24,7 +24,7 @@ declare(strict_types=1);
|
||||
namespace pocketmine\crafting;
|
||||
|
||||
use pocketmine\item\Item;
|
||||
use function array_map;
|
||||
use pocketmine\utils\Utils;
|
||||
use function count;
|
||||
|
||||
class ShapelessRecipe implements CraftingRecipe{
|
||||
@ -49,11 +49,11 @@ class ShapelessRecipe implements CraftingRecipe{
|
||||
}
|
||||
}
|
||||
|
||||
$this->results = array_map(function(Item $item) : Item{ return clone $item; }, $results);
|
||||
$this->results = Utils::cloneObjectArray($results);
|
||||
}
|
||||
|
||||
public function getResults() : array{
|
||||
return array_map(function(Item $item) : Item{ return clone $item; }, $this->results);
|
||||
return Utils::cloneObjectArray($this->results);
|
||||
}
|
||||
|
||||
public function getResultsFor(CraftingGrid $grid) : array{
|
||||
@ -64,7 +64,7 @@ class ShapelessRecipe implements CraftingRecipe{
|
||||
* @return Item[]
|
||||
*/
|
||||
public function getIngredientList() : array{
|
||||
return array_map(function(Item $item) : Item{ return clone $item; }, $this->ingredients);
|
||||
return Utils::cloneObjectArray($this->ingredients);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -24,7 +24,7 @@ declare(strict_types=1);
|
||||
namespace pocketmine\item;
|
||||
|
||||
use pocketmine\utils\RegistryTrait;
|
||||
use function array_map;
|
||||
use pocketmine\utils\Utils;
|
||||
use function assert;
|
||||
|
||||
/**
|
||||
@ -317,7 +317,7 @@ final class VanillaItems{
|
||||
* @return Item[]
|
||||
*/
|
||||
public static function getAll() : array{
|
||||
return array_map(function(Item $member){ return clone $member; }, self::_registryGetAll());
|
||||
return Utils::cloneObjectArray(self::_registryGetAll());
|
||||
}
|
||||
|
||||
protected static function setup() : void{
|
||||
|
@ -142,6 +142,16 @@ class Utils{
|
||||
return $reflect->getName();
|
||||
}
|
||||
|
||||
public static function cloneCallback() : \Closure{
|
||||
return static function(object $o){
|
||||
return clone $o;
|
||||
};
|
||||
}
|
||||
|
||||
public static function cloneObjectArray(array $array) : array{
|
||||
return array_map(self::cloneCallback(), $array);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets this machine / server instance unique ID
|
||||
* Returns a hash, the first 32 characters (or 16 if raw)
|
||||
|
Loading…
x
Reference in New Issue
Block a user