/
EntityVariable.java
60 lines (47 loc) · 2.16 KB
/
EntityVariable.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.entity;
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 static slimeknights.mantle.data.registry.GenericLoaderRegistry.SingletonLoader.singleton;
/** Variable that fetches a property from an entity */
public interface EntityVariable extends IHaveLoader {
GenericLoaderRegistry<EntityVariable> LOADER = new VariableLoaderRegistry<>("Entity Variable", Constant.LOADER.constructor());
/** Gets a value from the given entity */
float getValue(LivingEntity entity);
@Override
IGenericLoader<? extends EntityVariable> getLoader();
/* Singletons */
/** Creates a new singleton variable getter */
static EntityVariable simple(ToFloatFunction<LivingEntity> getter) {
return singleton(loader -> new EntityVariable() {
@Override
public float getValue(LivingEntity entity) {
return getter.apply(entity);
}
@Override
public IGenericLoader<? extends EntityVariable> getLoader() {
return loader;
}
});
}
/** Gets the current health of the entity. For max health, see {@link AttributeEntityVariable} */
EntityVariable HEALTH = simple(LivingEntity::getHealth);
/** Gets the height of the entities feet */
EntityVariable HEIGHT = simple(entity -> (float)entity.getY());
/** Constant value instance for this object */
record Constant(float value) implements VariableLoaderRegistry.ConstantFloat, EntityVariable {
public static final ConstantLoader<Constant> LOADER = new ConstantLoader<>(Constant::new);
@Override
public float getValue(LivingEntity entity) {
return value;
}
@Override
public IGenericLoader<? extends EntityVariable> getLoader() {
return LOADER;
}
}
}