-
Notifications
You must be signed in to change notification settings - Fork 755
/
FloatToolStat.java
163 lines (139 loc) · 4.33 KB
/
FloatToolStat.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
package slimeknights.tconstruct.library.tools.stat;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import lombok.Getter;
import net.minecraft.nbt.FloatTag;
import net.minecraft.nbt.NumericTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextColor;
import net.minecraft.tags.TagKey;
import net.minecraft.util.GsonHelper;
import net.minecraft.util.Mth;
import net.minecraft.world.item.Item;
import slimeknights.mantle.util.RegistryHelper;
import slimeknights.tconstruct.library.utils.TagUtil;
import slimeknights.tconstruct.library.utils.Util;
import javax.annotation.Nullable;
/**
* Tool stat representing a float value, used for most numbers
*/
public class FloatToolStat implements INumericToolStat<Float> {
/** Name of this tool stat */
@Getter
private final ToolStatId name;
/** Color for this stat type */
@Getter
private final TextColor color;
/** Gets the default value for this stat */
private final float defaultValue;
/** Min value for this stat */
@Getter
private final float minValue;
/** Max value for this stat */
@Getter
private final float maxValue;
@Nullable
private final TagKey<Item> tag;
public FloatToolStat(ToolStatId name, int color, float defaultValue, float minValue, float maxValue, @Nullable TagKey<Item> tag) {
this.name = name;
this.color = TextColor.fromRgb(color);
this.defaultValue = defaultValue;
this.minValue = minValue;
this.maxValue = maxValue;
this.tag = tag;
}
public FloatToolStat(ToolStatId name, int color, float defaultValue, float minValue, float maxValue) {
this(name, color, defaultValue, minValue, maxValue, null);
}
@Override
public boolean supports(Item item) {
return tag == null || RegistryHelper.contains(tag, item);
}
@Override
public Float getDefaultValue() {
return defaultValue;
}
@Override
public Float clamp(Float value) {
return Mth.clamp(value, getMinValue(), getMaxValue());
}
@Override
public FloatBuilder makeBuilder() {
return new FloatBuilder(defaultValue);
}
@Override
public void update(ModifierStatsBuilder builder, Float value) {
builder.<FloatBuilder>updateStat(this, b -> {
b.add += value;
b.base = 0;
});
}
@Override
public void add(ModifierStatsBuilder builder, double value) {
builder.<FloatBuilder>updateStat(this, b -> b.add += value);
}
@Override
public void multiply(ModifierStatsBuilder builder, double factor) {
builder.<FloatBuilder>updateStat(this, b -> b.multiply *= factor);
}
@Override
public void multiplyAll(ModifierStatsBuilder builder, double factor) {
builder.<FloatBuilder>updateStat(this, b -> b.multiply *= factor);
builder.multiplier(this, factor);
}
@Override
public Float build(ModifierStatsBuilder parent, Object builderObj) {
FloatBuilder builder = (FloatBuilder)builderObj;
return (builder.base + builder.add) * builder.multiply;
}
@Nullable
@Override
public Float read(Tag tag) {
if (TagUtil.isNumeric(tag)) {
return ((NumericTag) tag).getAsFloat();
}
return null;
}
@Override
public Tag write(Float value) {
return FloatTag.valueOf(value);
}
@Override
public Float deserialize(JsonElement json) {
return GsonHelper.convertToFloat(json, getName().toString());
}
@Override
public JsonElement serialize(Float value) {
return new JsonPrimitive(value);
}
@Override
public Float fromNetwork(FriendlyByteBuf buffer) {
return buffer.readFloat();
}
@Override
public void toNetwork(FriendlyByteBuf buffer, Float value) {
buffer.writeFloat(value);
}
/** Generic friendly way to format the value */
@Override
public Component formatValue(float value) {
return IToolStat.formatNumber(Util.makeTranslationKey("tool_stat", getName()), getColor(), value);
}
@Override
public String toString() {
return "FloatToolStat{" + name + '}';
}
/** Internal builder to store the add and multiply value */
protected static class FloatBuilder {
private float base;
/** Value summed with the base, applies first */
private float add = 0;
/** Value multiplied by the sum, applies second */
private float multiply = 1;
public FloatBuilder(float base) {
this.base = base;
}
}
}