-
Notifications
You must be signed in to change notification settings - Fork 755
/
AutosmeltModifier.java
109 lines (101 loc) · 3.61 KB
/
AutosmeltModifier.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
package slimeknights.tconstruct.tools.modifiers.ability.tool;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.item.crafting.SmeltingRecipe;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.storage.loot.LootContext;
import slimeknights.tconstruct.common.TinkerTags;
import slimeknights.tconstruct.common.recipe.RecipeCacheInvalidator;
import slimeknights.tconstruct.library.modifiers.ModifierEntry;
import slimeknights.tconstruct.library.modifiers.impl.NoLevelsModifier;
import slimeknights.tconstruct.library.recipe.SingleItemContainer;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;
import javax.annotation.Nullable;
import java.util.List;
import java.util.ListIterator;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
public class AutosmeltModifier extends NoLevelsModifier {
/** Cache of relevant smelting recipes */
private final Cache<Item,Optional<SmeltingRecipe>> recipeCache = CacheBuilder
.newBuilder()
.maximumSize(64)
.build();
/** Inventory instance to use for recipe search */
private final SingleItemContainer inventory = new SingleItemContainer();
public AutosmeltModifier() {
RecipeCacheInvalidator.addReloadListener(client -> {
if (!client) {
recipeCache.invalidateAll();
}
});
}
/**
* Gets a furnace recipe without using the cache
* @param stack Stack to try
* @param world World instance
* @return Furnace recipe
*/
private Optional<SmeltingRecipe> findRecipe(ItemStack stack, Level world) {
inventory.setStack(stack);
return world.getRecipeManager().getRecipeFor(RecipeType.SMELTING, inventory, world);
}
/**
* Gets a cached furnace recipe
* @param stack Stack for recipe
* @param world World instance
* @return Cached recipe
*/
@Nullable
private SmeltingRecipe findCachedRecipe(ItemStack stack, Level world) {
// don't use the cache if there is a tag, prevent breaking NBT sensitive recipes
if (stack.hasTag()) {
return findRecipe(stack, world).orElse(null);
}
try {
return recipeCache.get(stack.getItem(), () -> findRecipe(stack, world)).orElse(null);
} catch (ExecutionException e) {
return null;
}
}
/**
* Smelts an item using the relevant furnace recipe
* @param stack Stack to smelt
* @param world World instance
* @return Smelted item, or original if no recipe
*/
private ItemStack smeltItem(ItemStack stack, Level world) {
// skip blacklisted entries
if (stack.is(TinkerTags.Items.AUTOSMELT_BLACKLIST)) {
return stack;
}
SmeltingRecipe recipe = findCachedRecipe(stack, world);
if (recipe != null) {
inventory.setStack(stack);
ItemStack output = recipe.assemble(inventory);
if (stack.getCount() > 1) {
// recipe output is a copy, safe to modify
output.setCount(output.getCount() * stack.getCount());
}
return output;
}
return stack;
}
@Override
public void processLoot(IToolStackView tool, ModifierEntry modifier, List<ItemStack> generatedLoot, LootContext context) {
Level world = context.getLevel();
if (!generatedLoot.isEmpty()) {
ListIterator<ItemStack> iterator = generatedLoot.listIterator();
while (iterator.hasNext()) {
ItemStack stack = iterator.next();
ItemStack smelted = smeltItem(stack, world);
if (stack != smelted) {
iterator.set(smelted);
}
}
}
}
}