-
Notifications
You must be signed in to change notification settings - Fork 753
/
ToolMaterialHook.java
53 lines (44 loc) · 2.13 KB
/
ToolMaterialHook.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
package slimeknights.tconstruct.library.tools.definition.module.material;
import slimeknights.mantle.data.loadable.primitive.IntLoadable;
import slimeknights.mantle.data.loadable.record.RecordLoadable;
import slimeknights.tconstruct.library.materials.MaterialRegistry;
import slimeknights.tconstruct.library.materials.definition.MaterialId;
import slimeknights.tconstruct.library.materials.stats.MaterialStatsId;
import slimeknights.tconstruct.library.tools.definition.ToolDefinition;
import slimeknights.tconstruct.library.tools.definition.module.ToolHooks;
import java.util.List;
/** Hook for getting material info on tools */
public interface ToolMaterialHook {
/**
* Gets the list of parts on the tool
* @param definition Tool definition instance
* @return List of part requirements
*/
List<WeightedStatType> getStatTypes(ToolDefinition definition);
/** Checks if this definition has materials */
default boolean hasMaterials(ToolDefinition definition) {
return !getStatTypes(definition).isEmpty();
}
/** Gets the material stats that represent repairing */
default int[] getRepairIndices(ToolDefinition definition) {
return new int[0];
}
default int maxRepairWeight(ToolDefinition definition) {
return 1;
}
/** Stat with weights */
record WeightedStatType(MaterialStatsId stat, int weight) {
public static final RecordLoadable<WeightedStatType> LOADABLE = RecordLoadable.create(
MaterialStatsId.PARSER.requiredField("stat", WeightedStatType::stat),
IntLoadable.FROM_ONE.defaultField("weight", 1, WeightedStatType::weight),
WeightedStatType::new).compact(MaterialStatsId.PARSER.flatXmap(id -> new WeightedStatType(id, 1), WeightedStatType::stat), s -> s.weight == 1);
/** Checks if the given material can be used */
public boolean canUseMaterial(MaterialId material) {
return MaterialRegistry.getInstance().getMaterialStats(material.getId(), stat).isPresent();
}
}
/** Gets the stat types from the given definition */
static List<WeightedStatType> stats(ToolDefinition definition) {
return definition.getHook(ToolHooks.TOOL_MATERIALS).getStatTypes(definition);
}
}