/
InspirationsRegistry.java
550 lines (491 loc) · 17.7 KB
/
InspirationsRegistry.java
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
package knightminer.inspirations.library;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nonnull;
import org.apache.logging.log4j.Logger;
import com.google.common.collect.ImmutableList;
import knightminer.inspirations.common.Config;
import knightminer.inspirations.library.event.RegisterEvent.RegisterCauldronRecipe;
import knightminer.inspirations.library.recipe.cauldron.CauldronFluidRecipe;
import knightminer.inspirations.library.recipe.cauldron.CauldronFluidTransformRecipe;
import knightminer.inspirations.library.recipe.cauldron.FillCauldronRecipe;
import knightminer.inspirations.library.recipe.cauldron.ICauldronRecipe;
import knightminer.inspirations.library.recipe.cauldron.ICauldronRecipe.CauldronState;
import net.minecraft.block.Block;
import net.minecraft.block.BlockBush;
import net.minecraft.block.BlockCrops;
import net.minecraft.block.BlockDeadBush;
import net.minecraft.block.BlockDoublePlant;
import net.minecraft.block.BlockLilyPad;
import net.minecraft.block.BlockMushroom;
import net.minecraft.block.BlockSapling;
import net.minecraft.block.BlockTallGrass;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.oredict.OreDictionary;
import slimeknights.mantle.util.RecipeMatch;
public class InspirationsRegistry {
public static final Logger log = Util.getLogger("api");
private static boolean cauldronBigger = false, expensiveCauldronBrewing = false;
/**
* Sets a value from the Inspirations config into the registry. Used to keep the config out of the library
* @param key Key to set
* @param value Boolean value
* @deprecated For internal use only
*/
@Deprecated
public static void setConfig(String key, boolean value) {
switch(key) {
case "biggerCauldron":
cauldronBigger = value;
break;
case "expensiveCauldronBrewing":
expensiveCauldronBrewing = value;
break;
default:
// should never happen
throw new IllegalArgumentException("Unexpected config key " + key);
}
}
/*
* Books
*/
private static Map<ItemMetaKey,Float> books = new HashMap<>();
private static String[] bookKeywords = new String[0];
/**
* Checks if the given item stack is a book
* @param stack Input stack
* @return True if its a book
*/
public static boolean isBook(ItemStack stack) {
return getBookEnchantingPower(stack) >= 0;
}
/**
* Checks if the given item stack is a book
* @param stack Input stack
* @return True if its a book
*/
public static float getBookEnchantingPower(ItemStack book) {
return books.computeIfAbsent(new ItemMetaKey(book), InspirationsRegistry::isBook);
}
/**
* Helper function to check if a stack is a book, used internally by the book map
* @param key Item meta combination
* @return True if it is a book
*/
@Nonnull
private static Float isBook(ItemMetaKey key) {
Item item = key.getItem();
// blocks are not books, catches bookshelves
if(Block.getBlockFromItem(item) != Blocks.AIR) {
return -1f;
}
// look through every keyword from the config
ItemStack stack = key.makeItemStack();
for(String keyword : bookKeywords) {
// if the unlocalized name or the registry name has the keyword, its a book
if(item.getRegistryName().getResourcePath().contains(keyword)
|| stack.getUnlocalizedName().contains(keyword)) {
return 1.5f; // default power
}
}
return -1f;
}
/**
* Registers an override to state a stack is definately a book or not a book, primarily used by the config
* @param stack Itemstack which is a book
* @param isBook True if its a book, false if its not a book
* @deprecated use {@link #registerBook(ItemStack, float)}
*/
@Deprecated
public static void registerBook(ItemStack stack, boolean isBook) {
registerBook(stack, isBook ? 1.5f : -1f);
}
/**
* Registers an override to state a stack is definately a book or not a book, primarily used by the config
* @param item Item which is a book
* @param meta Meta which is a book
* @param isBook True if its a book, false if its not a book
* @deprecated use {@link #registerBook(ItemStack, float)}
*/
@Deprecated
public static void registerBook(Item item, int meta, boolean isBook) {
books.put(new ItemMetaKey(item, meta), isBook ? 1.5f : -1f);
}
/**
* Registers an override to state a stack is a book with an enchanting power
* @param item Item which is a book
* @param meta Meta which is a book
* @param power Enchanting power, 1.5 is default, NaN is not a book. 0 is a valid power
*/
public static void registerBook(ItemStack stack, float power) {
books.put(new ItemMetaKey(stack), power);
}
/**
* Internal function used to allow the config to set the list of book keywords. Should not need to be called outside of Inspirations
* @param keywords Keyword list
*/
public static void setBookKeywords(String[] keywords) {
bookKeywords = keywords;
}
/*
* Flowers
*/
private static Map<ItemMetaKey,Integer> flowers = new HashMap<>();
/**
* Checks if the given item stack is a flower
* @param stack Input stack
* @return True if its a flower
*/
public static boolean isFlower(ItemStack stack) {
return getFlowerComparatorPower(stack) > 0;
}
/**
* Gets the comparator level for a flower
* @param stack Input flower stack
* @return Comparator level
*/
public static int getFlowerComparatorPower(ItemStack stack) {
return flowers.computeIfAbsent(new ItemMetaKey(stack), (key) -> {
Block block = Block.getBlockFromItem(key.getItem());
// not a flower means 0 override
// this handles vanilla logic excluding cactuses and ferns, which are registered directly
if(!(block instanceof BlockBush)
|| block instanceof BlockDoublePlant
|| block instanceof BlockTallGrass
|| block instanceof BlockCrops
|| block instanceof BlockLilyPad) {
return 0;
}
// cactus: 15
if(block instanceof BlockSapling) {
return 12;
}
if(block instanceof BlockDeadBush) {
return 10;
}
// fern: 4
if(block instanceof BlockMushroom) {
return 1;
}
// flowers mostly
return 7;
});
}
/**
* Registers an override to state a stack is definitely a flower or not a flower, primarily used by the config
* @param stack ItemStack which is a flower
* @param isFlower True if its a flower, false if its not a flower
* @deprecated use {@link #registerFlower(ItemStack, int)}
*/
@Deprecated
public static void registerFlower(ItemStack stack, boolean isFlower) {
registerFlower(stack, isFlower ? 7 : 0);
}
/**
* Registers an override to state a stack is definitely a flower or not a flower, specifying the power
* @param stack ItemStack to override
* @param power Comparator power. Set to 0 blacklist this stack as a flower
*/
public static void registerFlower(ItemStack stack, int power) {
flowers.put(new ItemMetaKey(stack), power);
}
/**
* Registers an override to state a stack is definitely a flower or not a flower, primarily used by the config
* @param block Block which is a flower
* @param meta Meta which is a flower
* @param isFlower True if its a flower, false if its not a flower
*/
@Deprecated
public static void registerFlower(Block block, int meta, boolean isFlower) {
registerFlower(block, meta, isFlower ? 7 : 0);
}
/**
* Registers an override to state a stack is definitely a flower or not a flower, specifying the power
* @param block Block to override
* @param meta Meta to override
* @param power Comparator power. Set to 0 blacklist this stack as a flower
*/
public static void registerFlower(Block block, int meta, int power) {
Item item = Item.getItemFromBlock(block);
if(item != Items.AIR) {
flowers.put(new ItemMetaKey(item, meta), power);
}
}
/*
* Anvil smashing
*/
private static Map<IBlockState, IBlockState> anvilSmashing = new HashMap<>();
private static Map<Block, IBlockState> anvilSmashingBlocks = new HashMap<>();
private static Set<Material> anvilBreaking = new HashSet<>();
/**
* Registers an anvil smashing result for the given block state
* @param input Input state
* @param result Result state
*/
public static void registerAnvilSmashing(IBlockState input, IBlockState result) {
anvilSmashing.put(input, result);
}
/**
* Registers an anvil smashing result for the given block state
* @param input Input state
* @param result Result block
*/
public static void registerAnvilSmashing(IBlockState input, Block result) {
registerAnvilSmashing(input, result.getDefaultState());
}
/**
* Registers an anvil smashing result to break the given blockstate
* @param input Input block
*/
public static void registerAnvilBreaking(IBlockState input) {
registerAnvilSmashing(input, Blocks.AIR);
}
/**
* Registers an anvil smashing result for the given block
* @param input Input block
* @param result Result state
*/
public static void registerAnvilSmashing(Block input, IBlockState result) {
anvilSmashingBlocks.put(input, result);
}
/**
* Registers an anvil smashing result to break the given block
* @param input Input block
* @param result Result block
*/
public static void registerAnvilSmashing(Block input, Block result) {
registerAnvilSmashing(input, result.getDefaultState());
}
/**
* Registers an anvil smashing result to break the given block
* @param input Input block
*/
public static void registerAnvilBreaking(Block input) {
registerAnvilSmashing(input, Blocks.AIR);
}
/**
* Registers an anvil smashing result to break the given material
* @param material Input material
*/
public static void registerAnvilBreaking(Material material) {
anvilBreaking.add(material);
}
/**
* Gets the result of an anvil landing on the given blockstate
* @param state Input blockstate
* @return BlockState result. Will be air if its breaking, or null if there is no behavior
*/
public static IBlockState getAnvilSmashResult(IBlockState state) {
if(anvilSmashing.containsKey(state)) {
return anvilSmashing.get(state);
}
Block block = state.getBlock();
if(anvilSmashingBlocks.containsKey(block)) {
return anvilSmashingBlocks.get(block);
}
if(anvilBreaking.contains(state.getMaterial())) {
return Blocks.AIR.getDefaultState();
}
return null;
}
/**
* Checks if we have a state specific smashing result. Used for JEI to filter out the lists for blocks
* @param state State to check
* @return True if we have a state specific result
*/
public static boolean hasAnvilSmashStateResult(IBlockState state) {
return anvilSmashing.containsKey(state);
}
/**
* Gets all smashing recipes in the form of blockstate to blockstate
* @return List of map entries for the recipes
*/
public static List<Map.Entry<IBlockState,IBlockState>> getAllAnvilStateSmashing() {
return ImmutableList.copyOf(anvilSmashing.entrySet());
}
/**
* Gets all smashing recipes in the form of block to blockstate
* @return List of map entries for the recipes
*/
public static List<Map.Entry<Block,IBlockState>> getAllAnvilBlockSmashing() {
return ImmutableList.copyOf(anvilSmashingBlocks.entrySet());
}
/*
* Cauldron recipes
*/
private static List<ICauldronRecipe> cauldronRecipes = new ArrayList<>();
private static Set<ItemMetaKey> cauldronBlacklist = new HashSet<>();
private static Set<Fluid> cauldronWater = new HashSet<>();
private static Set<Block> cauldronFireBlocks = new HashSet<>();
private static Set<IBlockState> cauldronFireStates = new HashSet<>();
/**
* Returns the maximum size for the cauldron
* @return 4 if bigger cauldron, 3 otherwise
*/
public static int getCauldronMax() {
return cauldronBigger ? 4 : 3;
}
/**
* Returns if cauldron brewing is more expensive
* @return True if cauldron brewing is more expensive
*/
public static boolean expensiveCauldronBrewing() {
return expensiveCauldronBrewing;
}
/**
* Gets the result of a cauldron recipe
* @param input ItemStack input
* @param isBoiling Whether the cauldron is boiling
* @return Result of the recipe
*/
public static ICauldronRecipe getCauldronResult(ItemStack input, boolean boiling, int level, CauldronState state) {
for(ICauldronRecipe recipe : cauldronRecipes) {
if(recipe.matches(input, boiling, level, state)) {
return recipe;
}
}
return null;
}
/**
* Adds a new cauldron recipe
* @param recipe Recipe to add
*/
public static void addCauldronRecipe(ICauldronRecipe recipe) {
if(new RegisterCauldronRecipe(recipe).fire()) {
cauldronRecipes.add(recipe);
} else {
log.debug("Cauldron recipe '{}' canceled by event", recipe);
}
}
/**
* Adds a new cauldron recipe
* @param input ItemStack to check for
* @param output Recipe output
* @param boiling Whether the cauldron must be boiling or not
*/
public static void addCauldronRecipe(ItemStack input, ItemStack output, Boolean boiling) {
addCauldronRecipe(new CauldronFluidRecipe(RecipeMatch.of(input), output, boiling));
}
/**
* Adds a new cauldron recipe
* @param input oreDict name to check for
* @param output Recipe output
* @param boiling Whether the cauldron must be boiling or not
*/
public static void addCauldronRecipe(String input, ItemStack output, Boolean boiling) {
addCauldronRecipe(new CauldronFluidRecipe(RecipeMatch.of(input), output, boiling));
}
/**
* Adds a fluid transform cauldron recipe with a variant for one layer and for 3
* @param stack Input stack. Stack size will be checked, and doubled for a cauldron with more than one bottle
* @param input Input fluid
* @param output Output fluid
* @param boiling Whether the cauldron must be boiling or not. If null, the cauldron being boiling is ignored
*/
public static void addCauldronScaledTransformRecipe(ItemStack stack, Fluid input, Fluid output, Boolean boiling) {
addCauldronRecipe(new CauldronFluidTransformRecipe(RecipeMatch.of(stack, stack.getCount(), 1), input, output, boiling, 1));
stack = stack.copy();
int count = stack.getCount();
stack.setCount(count * 2);
if(Config.enableBiggerCauldron) {
addCauldronRecipe(new CauldronFluidTransformRecipe(RecipeMatch.of(stack, stack.getCount(), 1), input, output, boiling, 2));
stack = stack.copy();
stack.setCount(count * 3);
}
addCauldronRecipe(new CauldronFluidTransformRecipe(RecipeMatch.of(stack, stack.getCount(), 1), input, output, boiling, (Config.enableBiggerCauldron ? 4 : 3)));
}
/**
* Adds a item to empty into and fill from the cauldron
* @param filled Filled version of container
* @param container Empty version of container
* @param fluid Fluid contained
* @param amount Amount this container counts for
*/
public static void addCauldronFluidItem(ItemStack filled, ItemStack container, Fluid fluid, int amount) {
addCauldronRecipe(new FillCauldronRecipe(RecipeMatch.of(filled), fluid, amount, container.copy()));
addCauldronRecipe(new CauldronFluidRecipe(RecipeMatch.of(container), fluid, filled.copy(), null, amount, SoundEvents.ITEM_BOTTLE_FILL));
}
/**
* Adds a item to empty into and fill from the cauldron
* @param filled Filled version of container
* @param container Empty version of container
* @param fluid Fluid contained
*/
public static void addCauldronFluidItem(ItemStack filled, ItemStack container, Fluid fluid) {
addCauldronFluidItem(filled, container, fluid, 1);
}
/**
* Gets all cauldron recipes
* @return A list of all cauldron recipes
*/
public static List<ICauldronRecipe> getAllCauldronRecipes() {
return ImmutableList.copyOf(cauldronRecipes);
}
/**
* Adds the given fluid as cauldron water. Used for rain and fire checks among a few other things
* @param fluid Fluid to add
*/
public static void addCauldronWater(Fluid fluid) {
cauldronWater.add(fluid);
}
/**
* Checks if this fluid is considered water in the cauldron.
* Used for rain checks along with some recipe transformations
* @param fluid Fluid to check
* @return True if the fluid is considered water
*/
public static boolean isCauldronWater(Fluid fluid) {
return fluid != null && cauldronWater.contains(fluid);
}
/**
* Adds an item to the cauldron blacklist, preventing its normal cauldron interaction
* @param item Item to add
* @param meta Metadata to use, supports wildcard
*/
public static void addCauldronBlacklist(Item item, int meta) {
cauldronBlacklist.add(new ItemMetaKey(item, meta));
}
/**
* Checks if an item is blacklisted from its normal cauldron interaction
* @param stack ItemStack to check
* @return True if the item is blacklisted
*/
public static boolean isCauldronBlacklist(ItemStack stack) {
// check both the item with its current meta and with wildcard meta
return cauldronBlacklist.contains(new ItemMetaKey(stack))
|| cauldronBlacklist.contains(new ItemMetaKey(stack.getItem(), OreDictionary.WILDCARD_VALUE));
}
/**
* Registers all variants of a block as acting as fire to heat a cauldron
* @param block Block to register
*/
public static void registerCauldronFire(Block block) {
cauldronFireBlocks.add(block);
}
/**
* Registers a single block state to act as fire for a cauldron
* @param block Block state to register
*/
public static void registerCauldronFire(IBlockState block) {
cauldronFireStates.add(block);
}
/**
* Checks if a state is considered fire in a cauldron
* @param state State to check
* @return True if the state is considered fire
*/
public static boolean isCauldronFire(IBlockState state) {
return cauldronFireBlocks.contains(state.getBlock()) || cauldronFireStates.contains(state);
}
}