/
AnvilRecipe.java
588 lines (539 loc) · 20.6 KB
/
AnvilRecipe.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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
package knightminer.inspirations.library.recipe.anvil;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.Dynamic;
import com.mojang.serialization.JsonOps;
import knightminer.inspirations.library.InspirationsRegistry;
import knightminer.inspirations.library.recipe.BlockIngredient;
import knightminer.inspirations.library.recipe.RecipeSerializers;
import knightminer.inspirations.library.recipe.RecipeTypes;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.item.ItemEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.IRecipeSerializer;
import net.minecraft.item.crafting.IRecipeType;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.loot.LootContext;
import net.minecraft.loot.LootEntry;
import net.minecraft.loot.LootParameterSets;
import net.minecraft.loot.LootParameters;
import net.minecraft.loot.LootPool;
import net.minecraft.loot.LootSerializers;
import net.minecraft.loot.RandomRanges;
import net.minecraft.loot.RandomValueRange;
import net.minecraft.loot.conditions.ILootCondition;
import net.minecraft.loot.functions.ILootFunction;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.INBT;
import net.minecraft.nbt.NBTDynamicOps;
import net.minecraft.network.PacketBuffer;
import net.minecraft.state.Property;
import net.minecraft.resources.IReloadableResourceManager;
import net.minecraft.state.StateContainer;
import net.minecraft.util.JSONUtils;
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.common.crafting.CompoundIngredient;
import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
import net.minecraftforge.registries.ForgeRegistries;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.stream.Collectors;
public class AnvilRecipe implements IRecipe<AnvilInventory> {
// These are uppercase, which isn't allowed for namespaces so they won't conflict.
/** Used for property values or block to indicate it should be copied from the existing state. */
public static final String FROM_INPUT = "INPUT";
/** The kind of block transform to apply. */
public enum ConvertType {
/** Mine the block and drop contents. */
SMASH,
/** Convert to the specified blockstate. */
TRANSFORM,
/** Don't change the block, only change state. */
KEEP,
}
// GSON instance setup to handle loot tables.
public static final Gson GSON_LOOT = LootSerializers.func_237387_b_()
.registerTypeAdapter(LootPool.class, new LootPoolSerializer())
.create();
/**
* A list of all recipes, sorted by ingredient count.
* That ensures recipes with more ingredients are preferred.
* */
@Nullable
private static List<AnvilRecipe> sortedRecipes = null;
private final ResourceLocation id;
private final NonNullList<Ingredient> ingredients;
private final String group;
/** The block to produce. */
private final ConvertType blockConvert;
/** The state to produce, if blockConvert==TRANSFORM. */
private final Block transformResult;
/**
* Loot table pools used for item generation.
*/
private final List<LootPool> lootPools;
/**
* Properties to assign to the result, unparsed.
* If value == FROM_INPUT, copy over.
*/
private final List<Pair<String, String>> properties;
/** After matching, holds the number of times each item was used. */
@Nullable
private int[] used = null;
public AnvilRecipe(
ResourceLocation id,
String group,
NonNullList<Ingredient> ingredients,
ConvertType conversion,
Block transformResult,
List<LootPool> lootPools,
List<Pair<String, String>> properties
) {
this.id = id;
this.group = group;
this.ingredients = ingredients;
this.lootPools = lootPools;
this.blockConvert = conversion;
this.transformResult = transformResult;
this.properties = properties;
}
/**
* Return the appropriate recipe for this input.
* This is equivalent to the RecipeManager call, but prefers recipes with more ingredients.
*/
public static Optional<AnvilRecipe> matchRecipe(@Nonnull AnvilInventory inv, @Nonnull World world) {
if (sortedRecipes == null) {
// On first call, or after datapack reload sort all the recipes.
sortedRecipes = world.getRecipeManager()
.getRecipes(RecipeTypes.ANVIL)
.values()
.stream()
.map(AnvilRecipe.class::cast)
.sorted(Comparator.comparingInt(
(AnvilRecipe rec) -> rec.getIngredients().size()
).reversed())
.collect(Collectors.toList());
}
for(AnvilRecipe recipe: sortedRecipes) {
if (recipe.matches(inv, world)) {
return Optional.of(recipe);
}
}
return Optional.empty();
}
/**
* Register a reload listener which clears the sortedRecipes cache.
* We can't do the cache in the listener since the recipe manager may run after us.
*/
public static void onServerStart(FMLServerStartingEvent event) {
IReloadableResourceManager resman = (IReloadableResourceManager) event.getServer().getDataPackRegistries().getResourceManager();
resman.addReloadListener(
(stage, resMan, prepProp, reloadProf, bgExec, gameExec) -> CompletableFuture
.runAsync(() -> sortedRecipes = null, gameExec)
.thenCompose(stage::markCompleteAwaitingOthers)
);
}
/**
* Test if the given recipe matches the input.
* When successful, inv.used is modified to reflect the used items.
*/
@Override
public boolean matches(@Nonnull AnvilInventory inv, @Nonnull World worldIn) {
// Used is set to true if that item was used in this recipe. First reset it.
used = new int[inv.getItems().size()];
Arrays.fill(used, 0);
boolean result = ingredients.stream().allMatch(ing -> checkIngredient(inv, ing));
if (!result) {
// Clear this, since it's not useful.
used = null;
}
return result;
}
private boolean checkIngredient(AnvilInventory inv, Ingredient ing) {
assert this.used != null;
int[] used = this.used.clone();
if (ing instanceof BlockIngredient) {
// It's a block, just test the state.
return ((BlockIngredient) ing).testBlock(inv.getState());
} else if (ing instanceof CompoundIngredient) {
for(Ingredient subIng: ((CompoundIngredient) ing).getChildren()) {
if (checkIngredient(inv, subIng)) {
// Keep the state for this one.
return true;
}
// Restore the state, since the compound didn't match.
this.used = used.clone();
}
return false;
} else {
// It's an item. We want to see if any item matches,
// but not reuse items twice - since they're consumed.
boolean found = false;
for(int i = 0; i < this.used.length; i++) {
ItemStack item = inv.getItems().get(i);
if (this.used[i] < item.getCount() && ing.test(item)) {
this.used[i]++;
found = true;
break;
}
}
if (!found) {
// Restore the state, since the ingredient didn't match.
this.used = used.clone();
}
return found;
}
}
/** If a loot table is defined, suppress the drops from the block.
* The user can do those themselves if they want them.
*/
public boolean allowRegularDrops() {
return lootPools.size() == 0;
}
/** Return the type of conversion this does. */
@Nonnull
public ConvertType getConversion() {
return blockConvert;
}
/**
* Equivalent to getCraftingResult, but for blocks.
* @param inv The inventory that was matched.
* @return The block which should replace the existing one.
*/
@Nonnull
public BlockState getBlockResult(@Nonnull AnvilInventory inv) {
BlockState state;
switch(blockConvert) {
case TRANSFORM:
state = transformResult.getDefaultState();
break;
case KEEP:
state = inv.getState();
break;
case SMASH:
return Blocks.AIR.getDefaultState();
default:
throw new IllegalStateException(String.format("Unexpected blockConvert value %s for recipe %s", blockConvert, id));
}
StateContainer<Block, BlockState> cont = state.getBlock().getStateContainer();
StateContainer<Block, BlockState> inpContainer = inv.getState().getBlock().getStateContainer();
for(Pair<String, String> prop: properties) {
String key = prop.getFirst();
String value = prop.getSecond();
if (value.equals(FROM_INPUT)) {
Property<?> inpProp = inpContainer.getProperty(key);
if (inpProp == null) {
InspirationsRegistry.log.warn(
"No property \"{}\" to copy from block {} in Anvil recipe {}!",
key, inv.getState().getBlock().getRegistryName(), id
);
continue;
}
// Convert to a string, so differing types and identical but distinct IProperty objects
// still work.
value = getProperty(state, inpProp);
}
Property<?> targProp = cont.getProperty(key);
if(targProp == null) {
InspirationsRegistry.log.warn(
"Property \"{}\" is not valid for block {} in Anvil recipe {}!",
key, state.getBlock().getRegistryName(), id
);
continue;
}
state = setProperty(state, targProp, value);
}
return state;
}
/**
* Consume the items used by the recipe, killing empty items.
* @param items Item entities involved.
*/
public void consumeItemEnts(List<ItemEntity> items) {
if (used == null || items.size() != used.length) {
return;
}
for(int i = 0; i < items.size(); i++) {
if(used[i] > 0) {
ItemEntity item = items.get(i);
ItemStack newStack = item.getItem().copy();
newStack.shrink(used[i]);
if(newStack.isEmpty()) {
item.remove();
} else {
item.setItem(newStack);
}
}
}
}
/**
* Consume the items used by the recipe.
* @param items ItemStacks in the same order as passed to match().
*/
public void consumeItemStacks(List<ItemStack> items) {
if (used == null || items.size() != used.length) {
return;
}
for(int i = 0; i < items.size(); i++) {
items.get(i).shrink(used[i]);
}
}
/**
* Generate the items produced by this recipe.
*/
public void generateItems(ServerWorld world, BlockPos pos, Consumer<ItemStack> itemConsumer) {
LootContext context = new LootContext.Builder(world)
.withParameter(LootParameters.field_237457_g_, Vector3d.copy(pos).add(0.5, 0.5, 0.5))
.withParameter(LootParameters.BLOCK_STATE, world.getBlockState(pos))
// Not a tool, but the thing that makes the most sense...
.withParameter(LootParameters.TOOL, new ItemStack(Items.ANVIL))
.withNullableParameter(LootParameters.BLOCK_ENTITY, world.getTileEntity(pos))
.build(LootParameterSets.BLOCK);
for (LootPool pool: lootPools) {
pool.generate(itemConsumer, context);
}
}
/**
* Setting the property needs a generic arg, so the parsed value can have the same type as the property.
*/
private <T extends Comparable<T>> BlockState setProperty(BlockState state, Property<T>prop, String value) {
Optional<T> parsedValue = prop.parseValue(value);
if (parsedValue.isPresent()) {
return state.with(prop, parsedValue.get());
} else {
InspirationsRegistry.log.warn(
"Invalid value \"{}\" for block property {} of {} in anvil recipe {}!",
value, prop.getName(), state.getBlock().getRegistryName(), id);
return state;
}
}
/**
* Getting the property needs a generic arg, so the parsed value can have the same type as the property.
*/
private <T extends Comparable<T>> String getProperty(BlockState state, Property<T> prop) {
return state.get(prop).toString();
}
/**
* Not used, call getBlockResult.
* @param inv The inventory that was matched.
* @deprecated Use getBlockResult
*/
@Nonnull
@Override
public ItemStack getCraftingResult(@Nonnull AnvilInventory inv) {
return ItemStack.EMPTY;
}
@Override
public boolean canFit(int width, int height) {
return true;
}
@Nonnull
@Override
public ItemStack getRecipeOutput() {
return ItemStack.EMPTY;
}
@Nonnull
@Override
public NonNullList<ItemStack> getRemainingItems(@Nonnull AnvilInventory inv) {
return NonNullList.create();
}
@Nonnull
@Override
public NonNullList<Ingredient> getIngredients() {
return ingredients;
}
/**
* The recipe book cannot accept this.
*/
@Override
public boolean isDynamic() {
return true;
}
@Nonnull
@Override
public String getGroup() {
return group;
}
@Nonnull
@Override
public ItemStack getIcon() {
return new ItemStack(Items.ANVIL);
}
@Nonnull
@Override
public ResourceLocation getId() {
return id;
}
@Nonnull
@Override
public IRecipeSerializer<?> getSerializer() {
return RecipeSerializers.ANVIL_SMASHING;
}
@Nonnull
@Override
public IRecipeType<?> getType() {
return RecipeTypes.ANVIL;
}
/**
* Forge hooks into the LootPool constructor to add a name, whcih breaks if you try deserializing by itself.
* So use the builder instead.
*/
private static class LootPoolSerializer extends LootPool.Serializer {
@Nonnull
public LootPool deserialize(@Nonnull JsonElement jsonElement, @Nonnull Type type, @Nonnull JsonDeserializationContext ctx) throws JsonParseException {
LootPool.Builder builder = LootPool.builder();
JsonObject json = JSONUtils.getJsonObject(jsonElement, "loot pool");
builder.entries = Lists.newArrayList(JSONUtils.deserializeClass(json, "entries", ctx, LootEntry[].class));
builder.conditions = Lists.newArrayList(JSONUtils.deserializeClass(json, "conditions", new ILootCondition[0], ctx, ILootCondition[].class));
builder.functions = Lists.newArrayList(JSONUtils.deserializeClass(json, "functions", new ILootFunction[0], ctx, ILootFunction[].class));
builder.rolls(RandomRanges.deserialize(json.get("rolls"), ctx));
if (json.has("bonus_rolls")) {
RandomValueRange bonus_rolls = JSONUtils.deserializeClass(json, "bonus_rolls", new RandomValueRange(0.0F, 0.0F), ctx, RandomValueRange.class);
builder.bonusRolls(bonus_rolls.getMin(), bonus_rolls.getMax());
}
return builder.build();
}
}
public static class Serializer
extends net.minecraftforge.registries.ForgeRegistryEntry<IRecipeSerializer<?>>
implements IRecipeSerializer<AnvilRecipe>
{
@Nonnull
@Override
public AnvilRecipe read(@Nonnull ResourceLocation recipeId, @Nonnull JsonObject json) {
String group = JSONUtils.getString(json, "group", "");
NonNullList<Ingredient> inputs = NonNullList.create();
JsonArray inputJSON = JSONUtils.getJsonArray(json, "ingredients");
for(int i = 0; i < inputJSON.size(); i++) {
Ingredient ingredient = Ingredient.deserialize(inputJSON.get(i));
if (!ingredient.hasNoMatchingItems()) {
inputs.add(ingredient);
}
}
// Generate the output blockstate.
JsonObject result = JSONUtils.getJsonObject(json, "result");
Block block = Blocks.AIR;
ConvertType convertType;
if (result.has("block")) {
convertType = ConvertType.TRANSFORM;
String blockName = JSONUtils.getString(result, "block");
block = ForgeRegistries.BLOCKS.getValue(new ResourceLocation(blockName));
if(block == null) {
throw new JsonParseException("Unknown block \"" + blockName + "\"");
}
// Only treat vanilla air specially, users might want to spawn modded "air" blocks
// specifically for other reasons.
if(block == Blocks.AIR || block == Blocks.CAVE_AIR || block == Blocks.VOID_AIR) {
convertType = ConvertType.SMASH;
}
} else {
convertType = ConvertType.KEEP;
}
JsonObject props = JSONUtils.getJsonObject(result, "properties", new JsonObject());
List<Pair<String, String>> propsMap = new ArrayList<>();
for(Map.Entry<String, JsonElement> entry: props.entrySet()) {
if (!entry.getValue().isJsonPrimitive()) {
throw new JsonParseException("Expected simple value for property \"" + entry.getKey() + "\", but got a " + entry.getValue().getClass().getSimpleName());
}
propsMap.add(Pair.of(entry.getKey(), entry.getValue().getAsString()));
}
if (convertType == ConvertType.KEEP && props.size() == 0) {
throw new JsonParseException("Block result must either change block type or alter properties!");
}
List<LootPool> lootPools = Collections.emptyList();
if (result.has("pools")) {
lootPools = new ArrayList<>();
JsonArray pools = JSONUtils.getJsonArray(result, "pools");
for (JsonElement pool: pools) {
lootPools.add(GSON_LOOT.fromJson(pool, LootPool.class));
}
}
return new AnvilRecipe(recipeId, group, inputs, convertType, block, lootPools, propsMap);
}
@Nullable
@Override
public AnvilRecipe read(@Nonnull ResourceLocation recipeId, @Nonnull PacketBuffer buffer) {
ConvertType convertType = buffer.readEnumValue(ConvertType.class);
int ingredientCount = buffer.readVarInt();
int propsCount = buffer.readVarInt();
int poolsCount = buffer.readVarInt();
String group = buffer.readString();
Block blockResult = Blocks.AIR;
if (convertType == ConvertType.TRANSFORM) {
// Should never be missing, since we've already validated it.
blockResult = ForgeRegistries.BLOCKS.getValue(new ResourceLocation(buffer.readString()));
}
NonNullList<Ingredient> inputs = NonNullList.withSize(ingredientCount, Ingredient.EMPTY);
for(int i = 0; i < ingredientCount; i++) {
inputs.set(i, Ingredient.read(buffer));
}
List<Pair<String, String>> props = Collections.emptyList();
if (propsCount > 0) {
props = new ArrayList<>(propsCount);
for(int i = 0; i < propsCount; i++) {
props.add(Pair.of(buffer.readString(), buffer.readString()));
}
}
List<LootPool> lootPools = Collections.emptyList();
if (poolsCount > 0) {
lootPools = new ArrayList<>(poolsCount);
for(int i = 0; i < poolsCount; i++) {
CompoundNBT nbt = buffer.readCompoundTag();
JsonElement json = Dynamic.convert(NBTDynamicOps.INSTANCE, JsonOps.INSTANCE, nbt);
lootPools.add(GSON_LOOT.fromJson(json, LootPool.class));
}
}
return new AnvilRecipe(recipeId, group, inputs, convertType, blockResult, lootPools, props);
}
@Override
public void write(PacketBuffer buffer, AnvilRecipe recipe) {
buffer.writeEnumValue(recipe.blockConvert);
buffer.writeVarInt(recipe.ingredients.size());
buffer.writeVarInt(recipe.properties.size());
buffer.writeVarInt(recipe.lootPools.size());
buffer.writeString(recipe.group);
// We only need block type when transforming, so don't bother writing otherwise.
if (recipe.blockConvert == ConvertType.TRANSFORM) {
buffer.writeString(recipe.transformResult.getRegistryName().toString());
}
for(Ingredient ingredient: recipe.ingredients) {
ingredient.write(buffer);
}
for(Pair<String, String> prop: recipe.properties) {
buffer.writeString(prop.getFirst());
buffer.writeString(prop.getSecond());
}
// This is a bit expensive, but loot pools should be fairly rare.
for (LootPool pool: recipe.lootPools) {
JsonElement json = GSON_LOOT.toJsonTree(pool);
INBT nbt = Dynamic.convert(JsonOps.INSTANCE, NBTDynamicOps.INSTANCE, json);
buffer.writeCompoundTag((CompoundNBT) nbt);
}
}
}
}