use of org.lanternpowered.server.item.property.AlwaysConsumableProperty in project LanternServer by LanternPowered.
the class ConsumableInteractionBehavior method tryInteract.
@Override
public BehaviorResult tryInteract(BehaviorPipeline<Behavior> pipeline, BehaviorContext context) {
final Optional<Player> optPlayer = context.getContext(ContextKeys.PLAYER);
if (optPlayer.isPresent()) {
final Player player = optPlayer.get();
final ItemStack itemStack = context.requireContext(ContextKeys.USED_ITEM_STACK);
final AlwaysConsumableProperty property = itemStack.getProperty(AlwaysConsumableProperty.class).orElse(null);
if (property == null || !property.getValue()) {
int status = 0;
final FoodRestorationProperty foodRestorationProperty = itemStack.getProperty(FoodRestorationProperty.class).orElse(null);
if (foodRestorationProperty != null && foodRestorationProperty.getValue() != 0.0) {
final int maxFood = player.get(LanternKeys.MAX_FOOD_LEVEL).orElse(1);
final int food = player.get(Keys.FOOD_LEVEL).orElse(maxFood);
status = food < maxFood ? 2 : 1;
}
if (status != 2) {
final HealthRestorationProperty healthRestorationProperty = itemStack.getProperty(HealthRestorationProperty.class).orElse(null);
if (healthRestorationProperty != null && healthRestorationProperty.getValue() != 0.0) {
final double maxHealth = player.get(Keys.MAX_HEALTH).orElse(1.0);
final double health = player.get(Keys.HEALTH).orElse(maxHealth);
status = health < maxHealth ? 2 : 1;
}
}
if (status == 1) {
return BehaviorResult.PASS;
}
}
optPlayer.get().offer(LanternKeys.ACTIVE_HAND, Optional.of(context.requireContext(ContextKeys.INTERACTION_HAND)));
return BehaviorResult.SUCCESS;
}
return BehaviorResult.PASS;
}
Aggregations