-
Notifications
You must be signed in to change notification settings - Fork 752
/
IncrementalModifierRecipeBuilder.java
166 lines (146 loc) · 5.92 KB
/
IncrementalModifierRecipeBuilder.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
package slimeknights.tconstruct.library.recipe.modifiers.adding;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import lombok.Setter;
import lombok.experimental.Accessors;
import net.minecraft.data.recipes.FinishedRecipe;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.level.ItemLike;
import slimeknights.tconstruct.library.modifiers.ModifierEntry;
import slimeknights.tconstruct.library.modifiers.ModifierId;
import slimeknights.tconstruct.library.modifiers.util.LazyModifier;
import slimeknights.tconstruct.library.recipe.modifiers.ModifierMatch;
import slimeknights.tconstruct.library.utils.JsonUtils;
import slimeknights.tconstruct.tools.TinkerModifiers;
import javax.annotation.Nullable;
import java.util.function.Consumer;
/** Recipe that supports not just adding multiple of an item, but also adding a partial amount */
@SuppressWarnings({"unused", "UnusedReturnValue"})
@Accessors(chain = true)
public class IncrementalModifierRecipeBuilder extends AbstractModifierRecipeBuilder<IncrementalModifierRecipeBuilder> {
private Ingredient input = Ingredient.EMPTY;
private int amountPerItem;
private int neededPerLevel;
@Setter
private ItemStack leftover = ItemStack.EMPTY;
protected IncrementalModifierRecipeBuilder(ModifierEntry result) {
super(result);
}
/**
* Creates a new recipe for multiple levels of a modifier
* @param modifier Modifier
* @return Recipe for multiple levels of the modifier
*/
public static IncrementalModifierRecipeBuilder modifier(ModifierEntry modifier) {
return new IncrementalModifierRecipeBuilder(modifier);
}
/**
* Creates a new recipe for 1 level of a modifier
* @param modifier Modifier
* @return Recipe for 1 level of the modifier
*/
public static IncrementalModifierRecipeBuilder modifier(ModifierId modifier) {
return modifier(new ModifierEntry(modifier, 1));
}
/**
* Creates a new recipe for 1 level of a modifier
* @param modifier Modifier
* @return Recipe for 1 level of the modifier
*/
public static IncrementalModifierRecipeBuilder modifier(LazyModifier modifier) {
return modifier(modifier.getId());
}
/* Inputs */
/**
* Adds an input to the recipe
* @param input Input
* @param amountPerItem Amount each item matches
* @param neededPerLevel Total number needed for this modifier
* @return Builder instance
*/
public IncrementalModifierRecipeBuilder setInput(Ingredient input, int amountPerItem, int neededPerLevel) {
if (amountPerItem < 1) {
throw new IllegalArgumentException("Amount per item must be at least 1");
}
if (neededPerLevel <= amountPerItem) {
throw new IllegalArgumentException("Needed per level must be greater than amount per item");
}
this.input = input;
this.amountPerItem = amountPerItem;
this.neededPerLevel = neededPerLevel;
return this;
}
/**
* Adds an input to the recipe
* @param item Item input
* @param amountPerItem Amount each item matches
* @param neededPerLevel Total number needed for this modifier
* @return Builder instance
*/
public IncrementalModifierRecipeBuilder setInput(ItemLike item, int amountPerItem, int neededPerLevel) {
return setInput(Ingredient.of(item), amountPerItem, neededPerLevel);
}
/**
* Adds an input to the recipe
* @param tag Tag input
* @param amountPerItem Amount each item matches
* @param neededPerLevel Total number needed for this modifier
* @return Builder instance
*/
public IncrementalModifierRecipeBuilder setInput(TagKey<Item> tag, int amountPerItem, int neededPerLevel) {
return setInput(Ingredient.of(tag), amountPerItem, neededPerLevel);
}
/* Building */
@Override
public void save(Consumer<FinishedRecipe> consumer, ResourceLocation id) {
if (input == Ingredient.EMPTY) {
throw new IllegalStateException("Must set input");
}
ResourceLocation advancementId = buildOptionalAdvancement(id, "modifiers");
consumer.accept(new FinishedAdding(id, advancementId, false));
if (includeUnarmed) {
if (requirements != ModifierMatch.ALWAYS) {
throw new IllegalStateException("Cannot use includeUnarmed with requirements");
}
consumer.accept(new FinishedAdding(new ResourceLocation(id.getNamespace(), id.getPath() + "_unarmed"), null, true));
}
}
@Override
public IncrementalModifierRecipeBuilder saveSalvage(Consumer<FinishedRecipe> consumer, ResourceLocation id) {
if (salvageMaxLevel != 0 && salvageMaxLevel < salvageMinLevel) {
throw new IllegalStateException("Max level must be greater than min level");
}
ResourceLocation advancementId = buildOptionalAdvancement(id, "modifiers");
consumer.accept(new SalvageFinishedRecipe(id, advancementId));
return this;
}
/** @deprecated use {@link JsonUtils#serializeItemStack(ItemStack)} */
@Deprecated
public static JsonElement serializeResult(ItemStack result) {
return JsonUtils.serializeItemStack(result);
}
private class FinishedAdding extends ModifierFinishedRecipe {
public FinishedAdding(ResourceLocation ID, @Nullable ResourceLocation advancementID, boolean withUnarmed) {
super(ID, advancementID, withUnarmed);
}
@Override
public void serializeRecipeData(JsonObject json) {
json.add("input", input.toJson());
json.addProperty("amount_per_item", amountPerItem);
json.addProperty("needed_per_level", neededPerLevel);
if (leftover != ItemStack.EMPTY) {
json.add("leftover", serializeResult(leftover));
}
super.serializeRecipeData(json);
}
@Override
public RecipeSerializer<?> getType() {
return TinkerModifiers.incrementalModifierSerializer.get();
}
}
}