-
Notifications
You must be signed in to change notification settings - Fork 754
/
MaterialRepairToolHook.java
81 lines (69 loc) · 2.83 KB
/
MaterialRepairToolHook.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
package slimeknights.tconstruct.library.tools.definition.module.material;
import slimeknights.tconstruct.library.materials.definition.MaterialId;
import slimeknights.tconstruct.library.tools.definition.module.ToolHooks;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;
import java.util.Collection;
/** Hook for repairing a tool via tool materials */
public interface MaterialRepairToolHook {
/**
* Checks if the given material can be used to repair this tool
* @param tool Tool to check
* @param material Material to check
* @return True if it can be used to repair this tool
*/
boolean isRepairMaterial(IToolStackView tool, MaterialId material);
/**
* Gets the amount of durability restored by this materail for repair
* @param tool Tool instance
* @param material Material used for repair
* @return Repair amount
*/
float getRepairAmount(IToolStackView tool, MaterialId material);
/**
* Gets the factor to multiply an attempted repair with this material, used for part swapping repair notably.
* @param tool Tool instance
* @param material Material used to repair
* @return Repair factor, 1 is the default
*/
float getRepairFactor(IToolStackView tool, MaterialId material);
/** Gets the repair stat for the given tool */
static boolean canRepairWith(IToolStackView tool, MaterialId material) {
return tool.getHook(ToolHooks.MATERIAL_REPAIR).isRepairMaterial(tool, material);
}
/** Gets the repair stat for the given tool */
static float repairAmount(IToolStackView tool, MaterialId material) {
return tool.getHook(ToolHooks.MATERIAL_REPAIR).getRepairAmount(tool, material);
}
/** Gets the repair stat for the given tool */
static float repairFactor(IToolStackView tool, MaterialId material) {
return tool.getHook(ToolHooks.MATERIAL_REPAIR).getRepairFactor(tool, material);
}
/** Merger that takes the largest option from all nested modules */
record MaxMerger(Collection<MaterialRepairToolHook> hooks) implements MaterialRepairToolHook {
@Override
public boolean isRepairMaterial(IToolStackView tool, MaterialId material) {
for (MaterialRepairToolHook hook : hooks) {
if (hook.isRepairMaterial(tool, material)) {
return true;
}
}
return false;
}
@Override
public float getRepairAmount(IToolStackView tool, MaterialId material) {
float amount = 0;
for (MaterialRepairToolHook hook : hooks) {
amount = Math.max(amount, hook.getRepairAmount(tool, material));
}
return amount;
}
@Override
public float getRepairFactor(IToolStackView tool, MaterialId material) {
float factor = 0;
for (MaterialRepairToolHook hook : hooks) {
factor = Math.max(factor, hook.getRepairFactor(tool, material));
}
return factor;
}
}
}