-
Notifications
You must be signed in to change notification settings - Fork 755
/
IToolStat.java
166 lines (134 loc) · 5.06 KB
/
IToolStat.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.tools.stat;
import com.google.gson.JsonElement;
import net.minecraft.nbt.Tag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.TextColor;
import net.minecraft.util.Mth;
import net.minecraft.world.item.Item;
import slimeknights.tconstruct.library.utils.Util;
import javax.annotation.Nullable;
/**
* Interface for all tool stats, can implement to determine the behavior of stats in the modifier stat builder
* @param <T>
*/
public interface IToolStat<T> {
/** Gets the name of this stat for serializing to NBT */
ToolStatId getName();
/** Gets the default value for this stat */
T getDefaultValue();
/** Clamps the value into a valid range */
default T clamp(T value) {
return value;
}
/**
* Checks if the given item supports this stat. Typically is just a tag check
* @param item Item to validate
* @return True if the stat is supported
*/
default boolean supports(Item item) {
return true;
}
/* Modifier stat builder */
/**
* Creates a builder instance for this stat
* @return Stating value
*/
Object makeBuilder();
/**
* Builds this stat using the given builder
*
* @param parent Builder parent, allows fetching properties from teh parent
* @param builder Builder object, will be the same object you returned in {@link #makeBuilder()} so unchecked casting is safe
* @return Final float value
*/
T build(ModifierStatsBuilder parent, Object builder);
/**
* Updates the stat with a new value. The stat can determine how to merge that with existing values
* @param builder Builder instance
* @param value Amount to add
*/
void update(ModifierStatsBuilder builder, T value);
/* Storing and parsing */
/** Parses this stat from NBT, return null if the type is invalid */
@Nullable
T read(Tag tag);
/** Writes this stat to NBT */
@Nullable
Tag write(T value);
/** Parses this stat from JSON */
T deserialize(JsonElement json);
/** Serializes this stat to JSON */
JsonElement serialize(T value);
/** Parses this stat from from the network */
T fromNetwork(FriendlyByteBuf buffer);
/** Writes this stat to the network */
void toNetwork(FriendlyByteBuf buffer, T value);
/* Display */
/** Gets the prefix for this tool stat */
default MutableComponent getPrefix() {
return Component.translatable(Util.makeTranslationKey("tool_stat", getName()));
}
/** Gets the description for this tool stat */
default MutableComponent getDescription() {
return Component.translatable(Util.makeTranslationKey("tool_stat", getName()) + ".description");
}
/** Formats the value using this tool stat */
Component formatValue(T value);
/* Formatting helpers */
/**
* Creates a text component, coloring the number
* @param loc Translation key
* @param color Color
* @param number Number
* @return Text component
*/
static Component formatNumber(String loc, TextColor color, int number) {
return formatNumber(loc, color, (float) number);
}
/**
* Creates a text component, coloring the number
* @param loc Translation key
* @param color Color
* @param number Number
* @return Text component
*/
static Component formatNumber(String loc, TextColor color, float number) {
return Component.translatable(loc)
.append(Component.literal(Util.COMMA_FORMAT.format(number)).withStyle(style -> style.withColor(color)));
}
/**
* Creates a text component, coloring the number as a percentage
* @param loc Translation key
* @param color Color
* @param number Number
* @return Text component
*/
static Component formatNumberPercent(String loc, TextColor color, float number) {
return Component.translatable(loc)
.append(Component.literal(Util.PERCENT_FORMAT.format(number)).withStyle(style -> style.withColor(color)));
}
/**
* Formats a multiplier with hue shifting
* @param loc Prefix location
* @param number Percentage
* @return Colored percent with prefix
*/
static Component formatColoredMultiplier(String loc, float number) {
// 0.5 is red, 1.0 should be roughly green, 1.5 is blue
float hue = Mth.positiveModulo(number - 0.5f, 2f);
return Component.translatable(loc).append(Component.literal(Util.MULTIPLIER_FORMAT.format(number)).withStyle(style -> style.withColor(TextColor.fromRgb(Mth.hsvToRgb(hue / 1.5f, 1.0f, 0.75f)))));
}
/**
* Formats a multiplier with hue shifting
* @param loc Prefix location
* @param number Percentage
* @return Colored percent with prefix
*/
static Component formatColoredBonus(String loc, float number, float scale) {
// 0.5 is red, 1.0 should be roughly green, 1.5 is blue
float hue = Mth.positiveModulo(0.5f + number / (2*scale), 2f);
return Component.translatable(loc).append(Component.literal(Util.BONUS_FORMAT.format(number)).withStyle(style -> style.withColor(TextColor.fromRgb(Mth.hsvToRgb(hue / 1.5f, 1.0f, 0.75f)))));
}
}