-
Notifications
You must be signed in to change notification settings - Fork 755
/
MaterialRepairModule.java
96 lines (82 loc) · 3.96 KB
/
MaterialRepairModule.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
package slimeknights.tconstruct.library.tools.definition.module.material;
import net.minecraft.resources.ResourceLocation;
import slimeknights.mantle.data.loadable.primitive.FloatLoadable;
import slimeknights.mantle.data.loadable.record.RecordLoadable;
import slimeknights.tconstruct.TConstruct;
import slimeknights.tconstruct.library.materials.MaterialRegistry;
import slimeknights.tconstruct.library.materials.definition.MaterialId;
import slimeknights.tconstruct.library.materials.stats.IMaterialStats;
import slimeknights.tconstruct.library.materials.stats.IRepairableMaterialStats;
import slimeknights.tconstruct.library.materials.stats.MaterialStatsId;
import slimeknights.tconstruct.library.module.HookProvider;
import slimeknights.tconstruct.library.module.ModuleHook;
import slimeknights.tconstruct.library.tools.definition.ToolDefinition;
import slimeknights.tconstruct.library.tools.definition.module.ToolHooks;
import slimeknights.tconstruct.library.tools.definition.module.ToolModule;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;
import javax.annotation.Nullable;
import java.util.List;
/** Module for repairing a tool using a non-tool part material */
public final class MaterialRepairModule implements MaterialRepairToolHook, ToolModule {
private static final List<ModuleHook<?>> DEFAULT_HOOKS = HookProvider.<MaterialRepairModule>defaultHooks(ToolHooks.MATERIAL_REPAIR);
public static final RecordLoadable<MaterialRepairModule> LOADER = RecordLoadable.create(
MaterialId.PARSER.requiredField("material", m -> m.material),
MaterialStatsId.PARSER.requiredField("stat_type", m -> m.statType),
FloatLoadable.FROM_ZERO.defaultField("factor", 1f, m -> m.factor),
MaterialRepairModule::new);
/** Material used for repairing */
private final MaterialId material;
/** Stat type used for repairing, null means it will be fetched as the first available stat type */
private final MaterialStatsId statType;
/** Repair factor, allows making some materials repair with more weight */
private final float factor;
/** Amount to repair */
private float repairAmount = -1;
public MaterialRepairModule(MaterialId material, MaterialStatsId statType, float factor) {
this.material = material;
this.statType = statType;
this.factor = factor;
}
public MaterialRepairModule(MaterialId material, MaterialStatsId statType) {
this(material, statType, 1);
}
@Override
public boolean isRepairMaterial(IToolStackView tool, MaterialId material) {
return this.material.equals(material);
}
@Override
public float getRepairFactor(IToolStackView tool, MaterialId material) {
return this.material.equals(material) ? factor : 0;
}
/** Gets and caches the repair amount for this module */
private float getRepairAmount(ToolDefinition definition) {
if (repairAmount == -1) {
repairAmount = getDurability(definition.getId(), material, statType) * factor;
}
return repairAmount;
}
@Override
public float getRepairAmount(IToolStackView tool, MaterialId material) {
return this.material.equals(material) ? getRepairAmount(tool.getDefinition()) : 0;
}
@Override
public RecordLoadable<MaterialRepairModule> getLoader() {
return LOADER;
}
@Override
public List<ModuleHook<?>> getDefaultHooks() {
return DEFAULT_HOOKS;
}
/** Gets the durability for the given stat type */
public static int getDurability(@Nullable ResourceLocation toolId, MaterialId material, MaterialStatsId statType) {
IMaterialStats stats = MaterialRegistry.getInstance().getMaterialStats(material, statType).orElse(null);
if (stats instanceof IRepairableMaterialStats repairable) {
return repairable.durability();
} else {
if (toolId != null) {
TConstruct.LOG.warn("Attempting to repair {} using {}, but stat type {}{}. This usually indicates a broken datapack.", toolId, material, statType, stats == null ? " does not exist for the material" : " does not contain durability");
}
return 0;
}
}
}