-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathCraftingManager.php
More file actions
383 lines (322 loc) · 10.9 KB
/
CraftingManager.php
File metadata and controls
383 lines (322 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<?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\crafting;
use pmmp\encoding\ByteBufferWriter;
use pmmp\encoding\VarInt;
use pocketmine\item\Item;
use pocketmine\nbt\LittleEndianNbtSerializer;
use pocketmine\nbt\TreeRoot;
use pocketmine\utils\DestructorCallbackTrait;
use pocketmine\utils\ObjectSet;
use function array_shift;
use function array_search;
use function count;
use function implode;
use function ksort;
use function spl_object_id;
use const SORT_STRING;
class CraftingManager{
use DestructorCallbackTrait;
/**
* @var ShapedRecipe[][]
* @phpstan-var array<string, list<ShapedRecipe>>
*/
protected array $shapedRecipes = [];
/**
* @var ShapelessRecipe[][]
* @phpstan-var array<string, list<ShapelessRecipe>>
*/
protected array $shapelessRecipes = [];
/**
* @var CraftingRecipe[]
* @phpstan-var array<int, CraftingRecipe>
*/
private array $craftingRecipeIndex = [];
/**
* @var FurnaceRecipeManager[]
* @phpstan-var array<int, FurnaceRecipeManager>
*/
protected array $furnaceRecipeManagers = [];
/**
* @var PotionTypeRecipe[][]
* @phpstan-var list<PotionTypeRecipe>
*/
protected array $potionTypeRecipes = [];
/**
* @var PotionContainerChangeRecipe[]
* @phpstan-var list<PotionContainerChangeRecipe>
*/
protected array $potionContainerChangeRecipes = [];
/**
* @var BrewingRecipe[][]
* @phpstan-var array<int, array<int, BrewingRecipe>>
*/
private array $brewingRecipeCache = [];
/** @phpstan-var ObjectSet<\Closure() : void> */
private ObjectSet $recipeRegisteredCallbacks;
/** @phpstan-var ObjectSet<\Closure() : void> */
private ObjectSet $recipeUnregisteredCallbacks;
public function __construct(){
$this->recipeRegisteredCallbacks = new ObjectSet();
$this->recipeUnregisteredCallbacks = new ObjectSet();
foreach(FurnaceType::cases() as $furnaceType){
$this->furnaceRecipeManagers[spl_object_id($furnaceType)] = new FurnaceRecipeManager();
}
$recipeRegisteredCallbacks = $this->recipeRegisteredCallbacks;
$recipeUnregisteredCallbacks = $this->recipeUnregisteredCallbacks;
foreach($this->furnaceRecipeManagers as $furnaceRecipeManager){
$furnaceRecipeManager->getRecipeRegisteredCallbacks()->add(static function(FurnaceRecipe $recipe) use ($recipeRegisteredCallbacks) : void{
foreach($recipeRegisteredCallbacks as $callback){
$callback();
}
});
$furnaceRecipeManager->getRecipeUnregisteredCallbacks()->add(static function(FurnaceRecipe $recipe) use ($recipeUnregisteredCallbacks) : void{
foreach($recipeUnregisteredCallbacks as $callback){
$callback();
}
});
}
}
/** @phpstan-return ObjectSet<\Closure() : void> */
public function getRecipeRegisteredCallbacks() : ObjectSet{ return $this->recipeRegisteredCallbacks; }
/** @phpstan-return ObjectSet<\Closure() : void> */
public function getRecipeUnregisteredCallbacks() : ObjectSet{ return $this->recipeUnregisteredCallbacks; }
private static function hashOutput(Item $output) : string{
$write = new ByteBufferWriter();
VarInt::writeSignedInt($write, $output->getStateId());
//TODO: the NBT serializer allocates its own ByteBufferWriter, we should change the API in the future to
//allow passing our own to avoid this extra allocation
$write->writeByteArray((new LittleEndianNbtSerializer())->write(new TreeRoot($output->getNamedTag())));
return $write->getData();
}
/**
* @param Item[] $outputs
*/
private static function hashOutputs(array $outputs) : string{
if(count($outputs) === 1){
return self::hashOutput(array_shift($outputs));
}
$unique = [];
foreach($outputs as $o){
//count is not written because the outputs might be from multiple repetitions of a single recipe
//this reduces the accuracy of the hash, but it won't matter in most cases.
$hash = self::hashOutput($o);
$unique[$hash] = $hash;
}
ksort($unique, SORT_STRING);
return implode("", $unique);
}
/**
* @return ShapelessRecipe[][]
* @phpstan-return array<string, list<ShapelessRecipe>>
*/
public function getShapelessRecipes() : array{
return $this->shapelessRecipes;
}
/**
* @return ShapedRecipe[][]
* @phpstan-return array<string, list<ShapedRecipe>>
*/
public function getShapedRecipes() : array{
return $this->shapedRecipes;
}
/**
* @return CraftingRecipe[]
* @phpstan-return array<int, CraftingRecipe>
*/
public function getCraftingRecipeIndex() : array{
return $this->craftingRecipeIndex;
}
public function getCraftingRecipeFromIndex(int $index) : ?CraftingRecipe{
return $this->craftingRecipeIndex[$index] ?? null;
}
public function getFurnaceRecipeManager(FurnaceType $furnaceType) : FurnaceRecipeManager{
return $this->furnaceRecipeManagers[spl_object_id($furnaceType)];
}
/**
* @return PotionTypeRecipe[]
* @phpstan-return list<PotionTypeRecipe>
*/
public function getPotionTypeRecipes() : array{
return $this->potionTypeRecipes;
}
/**
* @return PotionContainerChangeRecipe[]
* @phpstan-return list<PotionContainerChangeRecipe>
*/
public function getPotionContainerChangeRecipes() : array{
return $this->potionContainerChangeRecipes;
}
public function registerShapedRecipe(ShapedRecipe $recipe) : void{
$this->shapedRecipes[self::hashOutputs($recipe->getResults())][] = $recipe;
$this->craftingRecipeIndex[] = $recipe;
foreach($this->recipeRegisteredCallbacks as $callback){
$callback();
}
}
public function unregisterShapedRecipe(ShapedRecipe $recipe) : void{
$changed = false;
$hash = self::hashOutputs($recipe->getResults());
foreach($this->shapedRecipes[$hash] ?? [] as $i => $r){
if($r === $recipe){
array_splice($this->shapedRecipes[$hash], $i, 1);
if(count($this->shapedRecipes[$hash]) === 0){
unset($this->shapedRecipes[$hash]);
$changed = true;
}
break;
}
}
$index = array_search($recipe, $this->craftingRecipeIndex, true);
if($index !== false){
unset($this->craftingRecipeIndex[$index]);
$changed = true;
}
if($changed){
foreach($this->recipeUnregisteredCallbacks as $callback){
$callback();
}
}
}
public function registerShapelessRecipe(ShapelessRecipe $recipe) : void{
$this->shapelessRecipes[self::hashOutputs($recipe->getResults())][] = $recipe;
$this->craftingRecipeIndex[] = $recipe;
foreach($this->recipeRegisteredCallbacks as $callback){
$callback();
}
}
public function unregisterShapelessRecipe(ShapelessRecipe $recipe) : void{
$changed = false;
$hash = self::hashOutputs($recipe->getResults());
foreach($this->shapelessRecipes[$hash] ?? [] as $i => $r){
if($r->isEquivalent($recipe)){
array_splice($this->shapelessRecipes[$hash], $i, 1);
if(count($this->shapelessRecipes[$hash]) === 0){
unset($this->shapelessRecipes[$hash]);
$changed = true;
}
// We don't break as it can have many similar recipes ?
}
}
foreach($this->craftingRecipeIndex as $index => $testRecipe){
if($testRecipe instanceof ShapelessRecipe && $recipe->isEquivalent($testRecipe)){
unset($this->craftingRecipeIndex[$index]);
$changed = true;
}
}
if($changed){
foreach($this->recipeUnregisteredCallbacks as $callback){
$callback();
}
}
}
public function registerPotionTypeRecipe(PotionTypeRecipe $recipe) : void{
$this->potionTypeRecipes[] = $recipe;
foreach($this->recipeRegisteredCallbacks as $callback){
$callback();
}
}
public function unregisterPotionTypeRecipe(PotionTypeRecipe $recipe) : void{
$recipeIndex = array_search($recipe, $this->potionTypeRecipes, true);
if($recipeIndex !== false){
array_splice($this->potionTypeRecipes, $recipeIndex, 1);
foreach($this->recipeUnregisteredCallbacks as $callback){
$callback();
}
}
}
public function registerPotionContainerChangeRecipe(PotionContainerChangeRecipe $recipe) : void{
$this->potionContainerChangeRecipes[] = $recipe;
foreach($this->recipeRegisteredCallbacks as $callback){
$callback();
}
}
public function unregisterPotionContainerChangeRecipe(PotionContainerChangeRecipe $recipe) : void{
$recipeIndex = array_search($recipe, $this->potionContainerChangeRecipes, true);
if($recipeIndex !== false){
array_splice($this->potionContainerChangeRecipes, $recipeIndex, 1);
foreach($this->recipeUnregisteredCallbacks as $callback){
$callback();
}
}
}
/**
* @param Item[] $outputs
*/
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[] $outputs
*
* @return CraftingRecipe[]|\Generator
* @phpstan-return \Generator<int, CraftingRecipe, void, void>
*/
public function matchRecipeByOutputs(array $outputs) : \Generator{
//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){
yield $recipe;
}
}
if(isset($this->shapelessRecipes[$outputHash])){
foreach($this->shapelessRecipes[$outputHash] as $recipe){
yield $recipe;
}
}
}
public function matchBrewingRecipe(Item $input, Item $ingredient) : ?BrewingRecipe{
$inputHash = $input->getStateId();
$ingredientHash = $ingredient->getStateId();
$cached = $this->brewingRecipeCache[$inputHash][$ingredientHash] ?? null;
if($cached !== null){
return $cached;
}
foreach($this->potionContainerChangeRecipes as $recipe){
if($recipe->getIngredient()->accepts($ingredient) && $recipe->getResultFor($input) !== null){
return $this->brewingRecipeCache[$inputHash][$ingredientHash] = $recipe;
}
}
foreach($this->potionTypeRecipes as $recipe){
if($recipe->getIngredient()->accepts($ingredient) && $recipe->getResultFor($input) !== null){
return $this->brewingRecipeCache[$inputHash][$ingredientHash] = $recipe;
}
}
return null;
}
}