/
BlockVariable.java
60 lines (47 loc) · 2.14 KB
/
BlockVariable.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
package slimeknights.tconstruct.library.json.variable.block;
import net.minecraft.world.level.block.state.BlockState;
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 static slimeknights.mantle.data.registry.GenericLoaderRegistry.SingletonLoader.singleton;
/** Variable that fetches a property from a block state */
public interface BlockVariable extends IHaveLoader {
GenericLoaderRegistry<BlockVariable> LOADER = new VariableLoaderRegistry<>("Block Variable", Constant.LOADER.constructor());
/** Gets a value from the given block state */
float getValue(BlockState state);
@Override
IGenericLoader<? extends BlockVariable> getLoader();
/* Singletons */
/** Creates a new singleton variable getter */
static BlockVariable simple(ToFloatFunction<BlockState> getter) {
return singleton(loader -> new BlockVariable() {
@Override
public float getValue(BlockState state) {
return getter.apply(state);
}
@Override
public IGenericLoader<? extends BlockVariable> getLoader() {
return loader;
}
});
}
/** Gets the block's blast resistance */
BlockVariable BLAST_RESISTANCE = simple(state -> state.getBlock().getExplosionResistance());
/** Gets the block's hardness */
BlockVariable HARDNESS = simple(state -> state.getBlock().defaultDestroyTime());
/** Constant value instance for this object */
record Constant(float value) implements VariableLoaderRegistry.ConstantFloat, BlockVariable {
public static final ConstantLoader<Constant> LOADER = new ConstantLoader<>(Constant::new);
@Override
public float getValue(BlockState state) {
return value;
}
@Override
public IGenericLoader<? extends BlockVariable> getLoader() {
return LOADER;
}
}
}