-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathCraftingComponent.java
More file actions
216 lines (187 loc) · 6.9 KB
/
CraftingComponent.java
File metadata and controls
216 lines (187 loc) · 6.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
package gregtech.api.recipes.crafting;
import gregtech.api.GTValues;
import gregtech.api.items.metaitem.MetaItem;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.unification.material.Material;
import gregtech.api.unification.ore.OrePrefix;
import gregtech.api.unification.stack.UnificationEntry;
import net.minecraft.block.Block;
import net.minecraft.item.ItemStack;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public final class CraftingComponent {
private final @NotNull Int2ObjectMap<ItemStack> itemEntries;
private final @NotNull Int2ObjectMap<String> stringEntries;
private @Nullable Object fallbackValue;
private CraftingComponent(@NotNull Int2ObjectMap<ItemStack> itemEntries,
@NotNull Int2ObjectMap<String> stringEntries,
@Nullable Object fallbackValue) {
this.itemEntries = itemEntries;
this.stringEntries = stringEntries;
this.fallbackValue = fallbackValue;
}
/**
* @param tier the tier of the ingredient
* @return the raw ingredient, which may be either an {@link ItemStack} or {@link String}
*/
public @Nullable Object getIngredient(int tier) {
ItemStack stack = itemEntries.get(tier);
if (stack != null) {
return stack;
}
String string = stringEntries.get(tier);
if (string != null) {
return string;
}
return fallbackValue;
}
/**
* Does not update the fallback ingredient
*
* @see #updateIngredients(CraftingComponent, boolean)
*/
@SuppressWarnings("unused")
public void updateIngredients(@NotNull CraftingComponent other) {
updateIngredients(other, false);
}
/**
* Add all ingredients from the provided Crafting Component to this ingredient, replacing existing values with
* new ones.
*
* @param other the component to update with
* @param updateFallback if the fallback ingredient should be updated with the provided component's ingredient
*/
@SuppressWarnings("unused")
public void updateIngredients(@NotNull CraftingComponent other, boolean updateFallback) {
itemEntries.putAll(other.itemEntries);
stringEntries.putAll(other.stringEntries);
if (updateFallback) {
if (other.fallbackValue == null) {
throw new IllegalArgumentException("Cannot update the fallback value to null");
}
this.fallbackValue = other.fallbackValue;
}
}
public static class Builder {
private final @NotNull Int2ObjectMap<ItemStack> itemEntries = new Int2ObjectOpenHashMap<>();
private final @NotNull Int2ObjectMap<String> stringEntries = new Int2ObjectOpenHashMap<>();
private @Nullable Object fallbackValue;
/**
* Create a CraftingComponent without a fallback value.
*/
public Builder() {}
/**
* @param fallback the fallback ingredient
*/
public Builder(@NotNull ItemStack fallback) {
this.fallbackValue = fallback;
}
/**
* @see #Builder(ItemStack)
*/
public Builder(@NotNull MetaItem<?>.MetaValueItem fallback) {
this(fallback.getStackForm());
}
/**
* @see #Builder(ItemStack)
*/
public Builder(@NotNull MetaTileEntity fallback) {
this(fallback.getStackForm());
}
/**
* @see #Builder(Block, int) but defaults to {@link GTValues#W} for meta
*/
public Builder(@NotNull Block fallback) {
this(fallback, GTValues.W);
}
/**
* @see #Builder(ItemStack)
*/
public Builder(@NotNull Block fallback, int meta) {
this(new ItemStack(fallback, 1, meta));
}
/**
* @param fallbackOreDict an OreDict string for the fallback ingredient
*/
public Builder(@NotNull String fallbackOreDict) {
this.fallbackValue = fallbackOreDict;
}
/**
* @see #Builder(String)
*/
public Builder(@NotNull OrePrefix fallbackPrefix, @NotNull Material fallbackMaterial) {
this(new UnificationEntry(fallbackPrefix, fallbackMaterial));
}
/**
* @see #Builder(String)
*/
public Builder(@NotNull UnificationEntry fallback) {
this(fallback.toString());
}
/**
* Add an entry
*
* @param tier the voltage tier for the entry, see {@link GTValues#V}
* @param stack the ingredient for the entry
* @return this
*/
public @NotNull Builder entry(int tier, @NotNull ItemStack stack) {
itemEntries.put(tier, stack);
stringEntries.remove(tier);
return this;
}
/**
* @see #entry(int, ItemStack)
*/
public @NotNull Builder entry(int tier, @NotNull MetaItem<?>.MetaValueItem metaValueItem) {
return entry(tier, metaValueItem.getStackForm());
}
/**
* @see #entry(int, ItemStack)
*/
public @NotNull Builder entry(int tier, @NotNull MetaTileEntity metaTileEntity) {
return entry(tier, metaTileEntity.getStackForm());
}
/**
* @see #entry(int, Block, int) but defaults to {@link GTValues#W} for meta
*/
public @NotNull Builder entry(int tier, @NotNull Block block) {
return entry(tier, new ItemStack(block, 1, GTValues.W));
}
/**
* @see #entry(int, ItemStack)
*/
public @NotNull Builder entry(int tier, @NotNull Block block, int meta) {
return entry(tier, new ItemStack(block, 1, meta));
}
/**
* Add an entry
*
* @param tier the voltage tier for the entry, see {@link GTValues#V}
* @param oreDict the OreDict for the entry
* @return this
*/
public @NotNull Builder entry(int tier, @NotNull String oreDict) {
stringEntries.put(tier, oreDict);
itemEntries.remove(tier);
return this;
}
/**
* @see #entry(int, String)
*/
public @NotNull Builder entry(int tier, @NotNull OrePrefix prefix, @NotNull Material material) {
return entry(tier, new UnificationEntry(prefix, material));
}
/**
* @see #entry(int, String)
*/
public @NotNull Builder entry(int tier, @NotNull UnificationEntry unificationEntry) {
return entry(tier, unificationEntry.toString());
}
public @NotNull CraftingComponent build() {
return new CraftingComponent(itemEntries, stringEntries, fallbackValue);
}
}
}