142 protected function matchItems(array &$needItems, array &$haveItems) : void{
145 foreach($this->actions as $key => $action){
146 $targetItem = $action->getTargetItem();
147 if(!$targetItem->isNull()){
148 $needItems[] = $targetItem;
152 $action->validate($this->source);
157 $sourceItem = $action->getSourceItem();
158 if(!$sourceItem->isNull()){
159 $haveItems[] = $sourceItem;
163 foreach($needItems as $i => $needItem){
164 foreach($haveItems as $j => $haveItem){
165 if($needItem->canStackWith($haveItem)){
166 $amount = min($needItem->getCount(), $haveItem->getCount());
167 $needItem->setCount($needItem->getCount() - $amount);
168 $haveItem->setCount($haveItem->getCount() - $amount);
169 if($haveItem->getCount() === 0){
170 unset($haveItems[$j]);
172 if($needItem->getCount() === 0){
173 unset($needItems[$i]);
179 $needItems = array_values($needItems);
180 $haveItems = array_values($haveItems);
198 foreach($this->actions as $key => $action){
200 $slotChanges[$h = (spl_object_hash($action->getInventory()) .
"@" . $action->getSlot())][] = $action;
201 $inventories[$h] = $action->getInventory();
202 $slots[$h] = $action->getSlot();
206 foreach(Utils::stringifyKeys($slotChanges) as $hash => $list){
207 if(count($list) === 1){
211 $inventory = $inventories[$hash];
212 $slot = $slots[$hash];
213 if(!$inventory->slotExists($slot)){
216 $sourceItem = $inventory->getItem($slot);
218 $targetItem = $this->findResultItem($sourceItem, $list);
219 if($targetItem ===
null){
220 throw new TransactionValidationException(
"Failed to compact " . count($list) .
" duplicate actions");
223 foreach($list as $action){
224 unset($this->actions[spl_object_id($action)]);
227 if(!$targetItem->equalsExact($sourceItem)){
229 $this->addAction(
new SlotChangeAction($inventory, $slot, $sourceItem, $targetItem));
239 assert(count($possibleActions) > 0);
242 $newList = $possibleActions;
243 foreach($possibleActions as $i => $action){
244 if($action->getSourceItem()->equalsExact($needOrigin)){
245 if($candidate !==
null){
256 $candidate = $action;
260 if($candidate ===
null){
265 if(count($newList) === 0){
266 return $candidate->getTargetItem();
268 return $this->findResultItem($candidate->getTargetItem(), $newList);
306 if($this->hasExecuted()){
310 $this->shuffleActions();
314 if(!$this->callExecuteEvent()){
318 foreach($this->actions as $action){
319 if(!$action->onPreExecute($this->source)){
320 throw new TransactionCancelledException(
"One of the actions in this transaction was cancelled");
324 foreach($this->actions as $action){
325 $action->execute($this->source);
328 $this->hasExecuted =
true;