/
ICauldronRecipe.java
272 lines (236 loc) · 6.83 KB
/
ICauldronRecipe.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
package knightminer.inspirations.library.recipe.cauldron;
import javax.annotation.Nonnull;
import knightminer.inspirations.library.InspirationsRegistry;
import net.minecraft.init.PotionTypes;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.potion.PotionType;
import net.minecraft.util.SoundEvent;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
/**
* Base interface for all cauldron recipes. Contains all methods required to determine new state, itemstack, and level based on the recipe
*
* Parameters are considered stateless and generally should not modify the input stack except in the case of transformInput()
*/
public interface ICauldronRecipe {
/**
* Checks if the recipe matches the given input
* @param stack Input stack
* @param boiling Whether the cauldron is above fire
* @param level Input level
* @param state Input cauldron state
* @return True if the recipe matches
*/
boolean matches(ItemStack stack, boolean boiling, int level, CauldronState state);
/**
* Gets the result stack for this recipe
* @param stack Input stack
* @param boiling Whether the cauldron is above fire
* @param level Input level
* @param state Input cauldron state
* @return ItemStack result
*/
default ItemStack getResult(ItemStack stack, boolean boiling, int level, CauldronState state) {
return ItemStack.EMPTY;
}
/**
* Transforms the input itemstack for the recipe. Default implementation shrinks the input stack by 1
* @param stack Input stack to transform
* @param boiling Whether the cauldron is above fire
* @param level Input level
* @param state Input cauldron state
* @return
*/
default ItemStack transformInput(ItemStack stack, boolean boiling, int level, CauldronState state) {
stack.shrink(1);
return stack;
}
/**
* Gets the new cauldron level as a result of this recipe
* @param level Starting level
* @return New level
*/
default int getLevel(int level) {
return level;
}
/**
* Gets the resulting cauldron state for this recipe
* @param stack Input stack
* @param boiling Whether the cauldron is above fire
* @param level Input level
* @param state Input cauldron state
* @return New cauldron state
*/
default CauldronState getState(ItemStack stack, boolean boiling, int level, CauldronState state) {
return state;
}
/**
* Gets the sound to play when performing this recipe
* @return Sound event
*/
default SoundEvent getSound(ItemStack stack, boolean boiling, int level, CauldronState state) {
return SoundEvents.ENTITY_BOBBER_SPLASH;
}
/**
* Volume to use when playing the sound
* @return Sound volume
*/
default float getVolume(SoundEvent sound) {
return sound == SoundEvents.ENTITY_BOBBER_SPLASH ? 0.3f : 1.0f;
}
/**
* Current cauldron state
*/
public class CauldronState {
private int color;
private PotionType potion;
private FluidStack fluid;
/** Special constant for default cauldron state. Use this instead of setting the state to null */
public static final CauldronState WATER = new CauldronState();
/**
* Creates a new water cauldron state
*/
private CauldronState() {
this.color = -1;
this.potion = null;
this.fluid = null;
}
/* Constructors */
/**
* Gets a new cauldron color state
* @param color Color input
*/
public static CauldronState dye(int color) {
if(color == -1) {
return WATER;
}
CauldronState state = new CauldronState();
state.color = color;
return state;
}
/**
* Gets a potion cauldron state
* @param potion Potion input
*/
public static CauldronState potion(PotionType potion) {
if(potion == PotionTypes.WATER) {
return WATER;
}
CauldronState state = new CauldronState();
state.potion = potion;
return state;
}
/**
* Gets a fluid cauldron state
* @param fluid Fluid input
*/
public static CauldronState fluid(Fluid fluid) {
if(fluid == FluidRegistry.WATER) {
return WATER;
}
CauldronState state = new CauldronState();
state.fluid = new FluidStack(fluid, 1);
return state;
}
/* Getters */
/**
* Check if the state is treated as water. Use if the exact fluid/potion water is not required
* @return true if the state is treated as water
*/
public boolean isWater() {
return this == WATER || (fluid != null && InspirationsRegistry.isCauldronWater(fluid.getFluid()));
}
/**
* Gets the color for this state
* @return color for the state, or -1 if the type is not DYE
*/
public int getColor() {
return color;
}
/**
* Gets the potion for this state
* @return potion for this state
*/
public PotionType getPotion() {
if(this == WATER) {
return PotionTypes.WATER;
}
return potion;
}
/**
* Gets the fluid for this state
* @return fluid for this state
*/
public Fluid getFluid() {
if(this == WATER) {
return FluidRegistry.WATER;
}
return fluid == null ? null : fluid.getFluid();
}
public boolean matches(CauldronState state) {
if(this == state) {
return true;
}
return state.color == this.color
&& state.potion == this.potion
&& state.getFluid() == this.getFluid();
}
/* NBT */
public static final String TAG_WATER = "water";
public static final String TAG_COLOR = "color";
public static final String TAG_POTION = "potion";
public static final String TAG_FLUID = "fluid";
/**
* Creates a new cauldron state from NBT
* @param tags NBT tags
*/
public static CauldronState fromNBT(NBTTagCompound tags) {
// quick boolean for water
if(tags.getBoolean(TAG_WATER)) {
return WATER;
}
// the rest will set properties
CauldronState state = new CauldronState();
if(tags.hasKey(TAG_COLOR)) {
state.color = tags.getInteger(TAG_COLOR);
}
if(tags.hasKey(TAG_POTION)) {
state.potion = PotionType.getPotionTypeForName(tags.getString(TAG_POTION));
}
if(tags.hasKey(TAG_FLUID)) {
Fluid fluid = FluidRegistry.getFluid(tags.getString(TAG_FLUID));
if(fluid != null) {
state.fluid = new FluidStack(fluid, 1);
}
}
return state;
}
/**
* Writes this state to NBT
* @return NBTTagCompound of the state
*/
@Nonnull
public NBTTagCompound writeToNBT() {
NBTTagCompound tags = new NBTTagCompound();
// if water, just set a boolean so we have something
if(this == WATER) {
tags.setBoolean(TAG_WATER, true);
return tags;
}
// otherwise set each property we have
if(color > -1) {
tags.setInteger(TAG_COLOR, color);
}
if(potion != null) {
tags.setString(TAG_POTION, potion.getRegistryName().toString());
}
if(fluid != null) {
tags.setString(TAG_FLUID, fluid.getFluid().getName());
}
return tags;
}
}
}