-
Notifications
You must be signed in to change notification settings - Fork 752
/
ValidatedResult.java
100 lines (87 loc) · 2.94 KB
/
ValidatedResult.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
package slimeknights.tconstruct.library.recipe.tinkerstation;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.world.item.ItemStack;
/**
* This class represents the result of a tinker station recipe, which is one of:
* <ul>
* <li>Success: returns an item stack result</li>
* <li>Failure: error state displaying an error message</li>
* <li>Pass: acts like a non-recipe match</li>
* </ul>
* TODO: in 1.19, replace with {@link slimeknights.tconstruct.library.recipe.RecipeResult}
*/
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class ValidatedResult {
/**
* Result for when this recipe is not craftable, but has no message to display
*/
public static final ValidatedResult PASS = new ValidatedResult(false);
/**
* Result containing the item result
* @param result Item result of this recipe
* @return Validation result with the given message
*/
public static ValidatedResult success(ItemStack result) {
return new ValidatedResult.Success(result);
}
/**
* Result for when this recipe is not craftable and has an error message
* @param translationKey Error message translation key
* @param params Arguments to format into the translation key
* @return Validation result with the given message
*/
public static ValidatedResult failure(String translationKey, Object... params) {
return new ValidatedResult.Failure(translationKey, params);
}
/** If true, this recipe passed and can be crafted for the given input */
@Getter
private final boolean success;
/**
* Gets the item result
* @return Item result
*/
public ItemStack getResult() {
return ItemStack.EMPTY;
}
/**
* If true, this recipe failed with an error message. This message should be displayed on screen
* @return true if the recipe failed with an error message
*/
public boolean hasError() {
return false;
}
/**
* Returns the message for this result
* @return result message
* @throws UnsupportedOperationException if this result is success or pass
*/
public Component getMessage() {
throw new UnsupportedOperationException("Cannot show error message on success");
}
/** Class for success, which has an item stack */
private static class Success extends ValidatedResult {
@Getter
private final ItemStack result;
private Success(ItemStack result) {
super(true);
this.result = result;
}
}
/** Class for failure, which has a message */
private static class Failure extends ValidatedResult {
@Getter
private final Component message;
private Failure(String translationKey, Object[] params) {
super(false);
this.message = new TranslatableComponent(translationKey, params);
}
@Override
public boolean hasError() {
return true;
}
}
}