/
ToolVariable.java
90 lines (69 loc) · 3.25 KB
/
ToolVariable.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
package slimeknights.tconstruct.library.json.variable.tool;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.LivingEntity;
import slimeknights.mantle.data.registry.GenericLoaderRegistry;
import slimeknights.mantle.data.registry.GenericLoaderRegistry.IGenericLoader;
import slimeknights.mantle.data.registry.GenericLoaderRegistry.IHaveLoader;
import slimeknights.tconstruct.library.json.variable.ToFloatFunction;
import slimeknights.tconstruct.library.json.variable.VariableLoaderRegistry;
import slimeknights.tconstruct.library.json.variable.VariableLoaderRegistry.ConstantLoader;
import slimeknights.tconstruct.library.json.variable.melee.MeleeVariable;
import slimeknights.tconstruct.library.json.variable.stat.ConditionalStatVariable;
import slimeknights.tconstruct.library.tools.context.ToolAttackContext;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;
import javax.annotation.Nullable;
import static slimeknights.mantle.data.registry.GenericLoaderRegistry.SingletonLoader.singleton;
/**
* Variable that fetches a value from a tool instance.
* All tool variables automatically work as melee, mining speed, and conditional stat variables due to the superset parameter space.
*/
public interface ToolVariable extends IHaveLoader, MeleeVariable, ConditionalStatVariable {
GenericLoaderRegistry<ToolVariable> LOADER = new VariableLoaderRegistry<>("Tool Variable", ToolVariable.Constant.LOADER.constructor());
/** Gets a value from the given tool */
float getValue(IToolStackView tool);
@Override
IGenericLoader<? extends ToolVariable> getLoader();
/* delegating methods, all tool variables are automatically the other types */
@Override
default float getValue(IToolStackView tool, @Nullable LivingEntity entity) {
return getValue(tool);
}
@Override
default float getValue(IToolStackView tool, @Nullable ToolAttackContext context, @Nullable LivingEntity attacker) {
return getValue(tool);
}
/* Singletons */
/** Creates a new singleton variable getter */
static ToolVariable simple(ToFloatFunction<IToolStackView> getter) {
return singleton(loader -> new ToolVariable() {
@Override
public float getValue(IToolStackView tool) {
return getter.apply(tool);
}
@Override
public IGenericLoader<? extends ToolVariable> getLoader() {
return loader;
}
});
}
/** Current durability of the tool */
ToolVariable CURRENT_DURABILITY = simple(IToolStackView::getCurrentDurability);
/** Registers a variable with tools, melee, conditional stat, and mining speed */
static void register(ResourceLocation name, IGenericLoader<? extends ToolVariable> loader) {
LOADER.register(name, loader);
MeleeVariable.LOADER.register(name, loader);
ConditionalStatVariable.register(name, loader);
}
/** Constant value instance for this object */
record Constant(float value) implements VariableLoaderRegistry.ConstantFloat, ToolVariable {
public static final ConstantLoader<ToolVariable.Constant> LOADER = new ConstantLoader<>(ToolVariable.Constant::new);
@Override
public float getValue(IToolStackView tool) {
return value;
}
@Override
public IGenericLoader<? extends ToolVariable> getLoader() {
return LOADER;
}
}
}