PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
CraftingTransaction.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\inventory\transaction;
25
33use function array_fill_keys;
34use function array_keys;
35use function array_pop;
36use function count;
37use function intdiv;
38use function min;
39use function uasort;
40
57 protected ?CraftingRecipe $recipe = null;
58 protected ?int $repetitions = null;
59
64 protected array $inputs = [];
69 protected array $outputs = [];
70
71 private CraftingManager $craftingManager;
72
73 public function __construct(Player $source, CraftingManager $craftingManager, array $actions = [], ?CraftingRecipe $recipe = null, ?int $repetitions = null){
74 parent::__construct($source, $actions);
75 $this->craftingManager = $craftingManager;
76 $this->recipe = $recipe;
77 $this->repetitions = $repetitions;
78 }
79
87 private static function packItems(array $providedItems) : array{
88 $packedProvidedItems = [];
89 while(count($providedItems) > 0){
90 $item = array_pop($providedItems);
91 foreach($providedItems as $k => $otherItem){
92 if($item->canStackWith($otherItem)){
93 $item->setCount($item->getCount() + $otherItem->getCount());
94 unset($providedItems[$k]);
95 }
96 }
97 $packedProvidedItems[] = $item;
98 }
99
100 return $packedProvidedItems;
101 }
102
110 public static function matchIngredients(array $providedItems, array $recipeIngredients, int $expectedIterations) : void{
111 if(count($recipeIngredients) === 0){
112 throw new TransactionValidationException("No recipe ingredients given");
113 }
114 if(count($providedItems) === 0){
115 throw new TransactionValidationException("No transaction items given");
116 }
117
118 $packedProvidedItems = self::packItems(Utils::cloneObjectArray($providedItems));
119 $packedProvidedItemMatches = array_fill_keys(array_keys($packedProvidedItems), 0);
120
121 $recipeIngredientMatches = [];
122
123 foreach($recipeIngredients as $ingredientIndex => $recipeIngredient){
124 $acceptedItems = [];
125 foreach($packedProvidedItems as $itemIndex => $packedItem){
126 if($recipeIngredient->accepts($packedItem)){
127 $packedProvidedItemMatches[$itemIndex]++;
128 $acceptedItems[$itemIndex] = $itemIndex;
129 }
130 }
131
132 if(count($acceptedItems) === 0){
133 throw new TransactionValidationException("No provided items satisfy ingredient requirement $recipeIngredient");
134 }
135
136 $recipeIngredientMatches[$ingredientIndex] = $acceptedItems;
137 }
138
139 foreach($packedProvidedItemMatches as $itemIndex => $itemMatchCount){
140 if($itemMatchCount === 0){
141 $item = $packedProvidedItems[$itemIndex];
142 throw new TransactionValidationException("Provided item $item is not accepted by any recipe ingredient");
143 }
144 }
145
146 //Most picky ingredients first - avoid picky ingredient getting their items stolen by wildcard ingredients
147 //TODO: this is still insufficient when multiple wildcard ingredients have overlaps, but we don't (yet) have to
148 //worry about those.
149 uasort($recipeIngredientMatches, fn(array $a, array $b) => count($a) <=> count($b));
150
151 foreach($recipeIngredientMatches as $ingredientIndex => $acceptedItems){
152 $needed = $expectedIterations;
153
154 foreach($packedProvidedItems as $itemIndex => $item){
155 if(!isset($acceptedItems[$itemIndex])){
156 continue;
157 }
158
159 $taken = min($needed, $item->getCount());
160 $needed -= $taken;
161 $item->setCount($item->getCount() - $taken);
162
163 if($item->getCount() === 0){
164 unset($packedProvidedItems[$itemIndex]);
165 }
166
167 if($needed === 0){
168 //validation passed!
169 continue 2;
170 }
171 }
172
173 $recipeIngredient = $recipeIngredients[$ingredientIndex];
174 $actualIterations = $expectedIterations - $needed;
175 throw new TransactionValidationException("Not enough items to satisfy recipe ingredient $recipeIngredient for $expectedIterations (only have enough items for $actualIterations iterations)");
176 }
177
178 if(count($packedProvidedItems) > 0){
179 throw new TransactionValidationException("Not all provided items were used");
180 }
181 }
182
192 protected function matchOutputs(array $txItems, array $recipeItems) : int{
193 if(count($recipeItems) === 0){
194 throw new TransactionValidationException("No recipe items given");
195 }
196 if(count($txItems) === 0){
197 throw new TransactionValidationException("No transaction items given");
198 }
199
200 $iterations = 0;
201 while(count($recipeItems) > 0){
203 $recipeItem = array_pop($recipeItems);
204 $needCount = $recipeItem->getCount();
205 foreach($recipeItems as $i => $otherRecipeItem){
206 if($otherRecipeItem->canStackWith($recipeItem)){ //make sure they have the same wildcards set
207 $needCount += $otherRecipeItem->getCount();
208 unset($recipeItems[$i]);
209 }
210 }
211
212 $haveCount = 0;
213 foreach($txItems as $j => $txItem){
214 if($txItem->canStackWith($recipeItem)){
215 $haveCount += $txItem->getCount();
216 unset($txItems[$j]);
217 }
218 }
219
220 if($haveCount % $needCount !== 0){
221 //wrong count for this output, should divide exactly
222 throw new TransactionValidationException("Expected an exact multiple of required $recipeItem (given: $haveCount, needed: $needCount)");
223 }
224
225 $multiplier = intdiv($haveCount, $needCount);
226 if($multiplier < 1){
227 throw new TransactionValidationException("Expected more than zero items matching $recipeItem (given: $haveCount, needed: $needCount)");
228 }
229 if($iterations === 0){
230 $iterations = $multiplier;
231 }elseif($multiplier !== $iterations){
232 //wrong count for this output, should match previous outputs
233 throw new TransactionValidationException("Expected $recipeItem x$iterations, but found x$multiplier");
234 }
235 }
236
237 if(count($txItems) > 0){
238 //all items should be destroyed in this process
239 throw new TransactionValidationException("Expected 0 items left over, have " . count($txItems));
240 }
241
242 return $iterations;
243 }
244
245 private function validateRecipe(CraftingRecipe $recipe, ?int $expectedRepetitions) : int{
246 //compute number of times recipe was crafted
247 $repetitions = $this->matchOutputs($this->outputs, $recipe->getResultsFor($this->source->getCraftingGrid()));
248 if($expectedRepetitions !== null && $repetitions !== $expectedRepetitions){
249 throw new TransactionValidationException("Expected $expectedRepetitions repetitions, got $repetitions");
250 }
251 //assert that $repetitions x recipe ingredients should be consumed
252 self::matchIngredients($this->inputs, $recipe->getIngredientList(), $repetitions);
253
254 return $repetitions;
255 }
256
257 public function validate() : void{
258 $this->squashDuplicateSlotChanges();
259 if(count($this->actions) < 1){
260 throw new TransactionValidationException("Transaction must have at least one action to be executable");
261 }
262
263 $this->matchItems($this->outputs, $this->inputs);
264
265 if($this->recipe === null){
266 $failed = 0;
267 foreach($this->craftingManager->matchRecipeByOutputs($this->outputs) as $recipe){
268 try{
269 //compute number of times recipe was crafted
270 $this->repetitions = $this->matchOutputs($this->outputs, $recipe->getResultsFor($this->source->getCraftingGrid()));
271 //assert that $repetitions x recipe ingredients should be consumed
272 self::matchIngredients($this->inputs, $recipe->getIngredientList(), $this->repetitions);
273
274 //Success!
275 $this->recipe = $recipe;
276 break;
278 //failed
279 ++$failed;
280 }
281 }
282
283 if($this->recipe === null){
284 throw new TransactionValidationException("Unable to match a recipe to transaction (tried to match against $failed recipes)");
285 }
286 }else{
287 $this->repetitions = $this->validateRecipe($this->recipe, $this->repetitions);
288 }
289 }
290
291 protected function callExecuteEvent() : bool{
292 $ev = new CraftItemEvent($this, $this->recipe, $this->repetitions, $this->inputs, $this->outputs);
293 $ev->call();
294 return !$ev->isCancelled();
295 }
296}
static matchIngredients(array $providedItems, array $recipeIngredients, int $expectedIterations)