-
Notifications
You must be signed in to change notification settings - Fork 753
/
ToolDefinitionDataBuilder.java
145 lines (122 loc) · 4.36 KB
/
ToolDefinitionDataBuilder.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
package slimeknights.tconstruct.library.tools.definition;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import slimeknights.mantle.data.loadable.ErrorFactory;
import slimeknights.tconstruct.library.materials.stats.MaterialStatsId;
import slimeknights.tconstruct.library.modifiers.ModifierHook;
import slimeknights.tconstruct.library.modifiers.util.ModifierHookMap.WithHooks;
import slimeknights.tconstruct.library.tools.SlotType;
import slimeknights.tconstruct.library.tools.definition.module.ToolModule;
import slimeknights.tconstruct.library.tools.definition.module.build.ToolSlotsModule;
import slimeknights.tconstruct.library.tools.nbt.MultiplierNBT;
import slimeknights.tconstruct.library.tools.nbt.StatsNBT;
import slimeknights.tconstruct.library.tools.part.IToolPart;
import slimeknights.tconstruct.library.tools.stat.INumericToolStat;
import slimeknights.tconstruct.library.tools.stat.IToolStat;
import java.util.List;
import java.util.function.Supplier;
/**
* Builder for a tool definition data
*/
@NoArgsConstructor(staticName = "builder")
@Accessors(fluent = true)
public class ToolDefinitionDataBuilder {
private final ImmutableList.Builder<PartRequirement> parts = ImmutableList.builder();
private final StatsNBT.Builder bonuses = StatsNBT.builder();
private final MultiplierNBT.Builder multipliers = MultiplierNBT.builder();
private final ImmutableList.Builder<WithHooks<ToolModule>> modules = ImmutableList.builder();
/* Parts */
/**
* Adds a part to the builder
*/
public ToolDefinitionDataBuilder part(IToolPart part, int weight) {
parts.add(PartRequirement.ofPart(part, weight));
return this;
}
/**
* Adds a stat requirement to the builder, for tools that don't have normal tool building recipes
*/
public ToolDefinitionDataBuilder part(MaterialStatsId stat, int weight) {
parts.add(PartRequirement.ofStat(stat, weight));
return this;
}
/**
* Adds a part to the builder
*/
public ToolDefinitionDataBuilder part(Supplier<? extends IToolPart> part, int weight) {
return part(part.get(), weight);
}
/**
* Adds a part to the builder
*/
public ToolDefinitionDataBuilder part(IToolPart part) {
return part(part, 1);
}
/**
* Adds a part to the builder
*/
public ToolDefinitionDataBuilder part(Supplier<? extends IToolPart> part) {
return part(part, 1);
}
/* Stats */
/**
* Adds a bonus to the builder
*/
public <T> ToolDefinitionDataBuilder stat(IToolStat<T> stat, T value) {
bonuses.set(stat, value);
return this;
}
/**
* Adds a bonus to the builder, overload for floats as they come up pretty often, helps with boxing
*/
public ToolDefinitionDataBuilder stat(IToolStat<Float> stat, float value) {
bonuses.set(stat, value);
return this;
}
/**
* Applies a global multiplier
*/
public ToolDefinitionDataBuilder multiplier(INumericToolStat<?> stat, float value) {
multipliers.set(stat, value);
return this;
}
/**
* Sets the starting slots to default
*/
public ToolDefinitionDataBuilder smallToolStartingSlots() {
module(new ToolSlotsModule(ImmutableMap.of(SlotType.UPGRADE, 3, SlotType.ABILITY, 1)));
return this;
}
/**
* Sets the starting slots to default
*/
public ToolDefinitionDataBuilder largeToolStartingSlots() {
module(new ToolSlotsModule(ImmutableMap.of(SlotType.UPGRADE, 2, SlotType.ABILITY, 1)));
return this;
}
/* Modules */
/** Adds a module to the definition with the given hooks */
@SafeVarargs
public final <T extends ToolModule> ToolDefinitionDataBuilder module(T module, ModifierHook<? super T>... hooks) {
modules.add(new WithHooks<>(module, List.of(hooks)));
return this;
}
/** Adds a module to the definition */
public ToolDefinitionDataBuilder module(ToolModule module) {
modules.add(new WithHooks<>(module, List.of()));
return this;
}
/** Adds a module to the definition */
public ToolDefinitionDataBuilder module(ToolModule... modules) {
for (ToolModule module : modules) {
module(module);
}
return this;
}
/** Builds the final definition JSON to serialize */
public ToolDefinitionData build() {
return new ToolDefinitionData(parts.build(), bonuses.build(), multipliers.build(), modules.build(), ErrorFactory.RUNTIME);
}
}