/
StatePropertyVariable.java
26 lines (22 loc) · 1.09 KB
/
StatePropertyVariable.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
package slimeknights.tconstruct.library.json.variable.block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.Property;
import slimeknights.mantle.data.loadable.primitive.StringLoadable;
import slimeknights.mantle.data.loadable.record.RecordLoadable;
import slimeknights.mantle.data.registry.GenericLoaderRegistry.IGenericLoader;
/** Fetches a value of an integer property */
public record StatePropertyVariable(String name) implements BlockVariable {
public static final RecordLoadable<StatePropertyVariable> LOADER = RecordLoadable.create(StringLoadable.DEFAULT.field("name", StatePropertyVariable::name), StatePropertyVariable::new);
@Override
public float getValue(BlockState state) {
Property<?> property = state.getBlock().getStateDefinition().getProperty(name);
if (property != null && Number.class.isAssignableFrom(property.getValueClass())) {
return ((Number) state.getValue(property)).floatValue();
}
return 0;
}
@Override
public IGenericLoader<? extends BlockVariable> getLoader() {
return LOADER;
}
}