TypeConverter: clean up absurdly overcomplicated bullshit in createInventoryAction()

This commit is contained in:
Dylan K. Taylor 2021-11-08 20:18:07 +00:00
parent cc4bb91fcb
commit c33f97ae41
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -27,7 +27,6 @@ use pocketmine\block\inventory\AnvilInventory;
use pocketmine\block\inventory\CraftingTableInventory; use pocketmine\block\inventory\CraftingTableInventory;
use pocketmine\block\inventory\EnchantInventory; use pocketmine\block\inventory\EnchantInventory;
use pocketmine\block\inventory\LoomInventory; use pocketmine\block\inventory\LoomInventory;
use pocketmine\inventory\Inventory;
use pocketmine\inventory\transaction\action\CreateItemAction; use pocketmine\inventory\transaction\action\CreateItemAction;
use pocketmine\inventory\transaction\action\DestroyItemAction; use pocketmine\inventory\transaction\action\DestroyItemAction;
use pocketmine\inventory\transaction\action\DropItemAction; use pocketmine\inventory\transaction\action\DropItemAction;
@ -248,17 +247,12 @@ class TypeConverter{
} }
/** /**
* @param int[] $test * @param int[] $test
* @phpstan-param array<int, int> $test * @phpstan-param array<int, int> $test
* @phpstan-param \Closure(Inventory) : bool $c
* @phpstan-return array{int, Inventory}
*/ */
protected function mapUIInventory(int $slot, array $test, ?Inventory $inventory, \Closure $c) : ?array{ protected function mapUIInventory(int $slot, array $test, bool $valid) : ?int{
if($inventory === null){ if(array_key_exists($slot, $test) && $valid){
return null; return $test[$slot];
}
if(array_key_exists($slot, $test) && $c($inventory)){
return [$test[$slot], $inventory];
} }
return null; return null;
} }
@ -283,6 +277,7 @@ class TypeConverter{
} }
switch($action->sourceType){ switch($action->sourceType){
case NetworkInventoryAction::SOURCE_CONTAINER: case NetworkInventoryAction::SOURCE_CONTAINER:
$window = null;
if($action->windowId === ContainerIds::UI and $action->inventorySlot > 0){ if($action->windowId === ContainerIds::UI and $action->inventorySlot > 0){
if($action->inventorySlot === UIInventorySlotOffset::CREATED_ITEM_OUTPUT){ if($action->inventorySlot === UIInventorySlotOffset::CREATED_ITEM_OUTPUT){
return null; //useless noise return null; //useless noise
@ -290,23 +285,22 @@ class TypeConverter{
$pSlot = $action->inventorySlot; $pSlot = $action->inventorySlot;
$craftingGrid = $player->getCraftingGrid(); $craftingGrid = $player->getCraftingGrid();
$mapped = $this->mapUIInventory($pSlot, UIInventorySlotOffset::CRAFTING2X2_INPUT, $craftingGrid, fn() => true); $slot = $this->mapUIInventory($pSlot, UIInventorySlotOffset::CRAFTING2X2_INPUT, true);
if($mapped === null){ if($slot !== null){
$current = $player->getCurrentWindow(); $window = $craftingGrid;
$mapped = }elseif(($current = $player->getCurrentWindow()) !== null){
$this->mapUIInventory($pSlot, UIInventorySlotOffset::ANVIL, $current, $slot =
function(Inventory $i) : bool{ return $i instanceof AnvilInventory; }) ?? $this->mapUIInventory($pSlot, UIInventorySlotOffset::ANVIL, $current instanceof AnvilInventory) ??
$this->mapUIInventory($pSlot, UIInventorySlotOffset::ENCHANTING_TABLE, $current, $this->mapUIInventory($pSlot, UIInventorySlotOffset::ENCHANTING_TABLE, $current instanceof EnchantInventory) ??
function(Inventory $i) : bool{ return $i instanceof EnchantInventory; }) ?? $this->mapUIInventory($pSlot, UIInventorySlotOffset::LOOM, $current instanceof LoomInventory) ??
$this->mapUIInventory($pSlot, UIInventorySlotOffset::LOOM, $current, $this->mapUIInventory($pSlot, UIInventorySlotOffset::CRAFTING3X3_INPUT, $current instanceof CraftingTableInventory);
fn(Inventory $i) => $i instanceof LoomInventory) ?? if($slot !== null){
$this->mapUIInventory($pSlot, UIInventorySlotOffset::CRAFTING3X3_INPUT, $current, $window = $current;
fn(Inventory $i) => $i instanceof CraftingTableInventory); }
} }
if($mapped === null){ if($slot === null){
throw new TypeConversionException("Unmatched UI inventory slot offset $pSlot"); throw new TypeConversionException("Unmatched UI inventory slot offset $pSlot");
} }
[$slot, $window] = $mapped;
}else{ }else{
$window = $inventoryManager->getWindow($action->windowId); $window = $inventoryManager->getWindow($action->windowId);
$slot = $action->inventorySlot; $slot = $action->inventorySlot;