use of org.spongepowered.api.data.property.item.FoodRestorationProperty 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;
}
use of org.spongepowered.api.data.property.item.FoodRestorationProperty in project LanternServer by LanternPowered.
the class ConsumableInteractionBehavior method tryUse.
@Override
public BehaviorResult tryUse(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 FoodRestorationProperty foodRestorationProperty = itemStack.getProperty(FoodRestorationProperty.class).orElse(null);
if (foodRestorationProperty != null && foodRestorationProperty.getValue() != 0.0) {
final Optional<Integer> maxFood = player.get(LanternKeys.MAX_FOOD_LEVEL);
final Optional<Integer> optFoodLevel = player.get(Keys.FOOD_LEVEL);
if (optFoodLevel.isPresent()) {
player.offer(Keys.FOOD_LEVEL, Math.min(optFoodLevel.get() + foodRestorationProperty.getValue(), maxFood.orElse(Integer.MAX_VALUE)));
}
}
final HealthRestorationProperty healthRestorationProperty = itemStack.getProperty(HealthRestorationProperty.class).orElse(null);
if (healthRestorationProperty != null && healthRestorationProperty.getValue() != 0.0) {
final Optional<Double> maxHealth = player.get(Keys.MAX_HEALTH);
final Optional<Double> optHealth = player.get(Keys.HEALTH);
if (optHealth.isPresent()) {
player.offer(Keys.HEALTH, Math.min(optHealth.get() + healthRestorationProperty.getValue(), maxHealth.orElse(Double.MAX_VALUE)));
}
}
final SaturationProperty saturationProperty = itemStack.getProperty(SaturationProperty.class).orElse(null);
if (saturationProperty != null && saturationProperty.getValue() != 0.0) {
final Optional<Double> optSaturation = player.get(Keys.SATURATION);
if (optSaturation.isPresent()) {
player.offer(Keys.SATURATION, Math.min(optSaturation.get() + saturationProperty.getValue(), player.get(Keys.FOOD_LEVEL).orElse(20)));
}
}
final ApplicableEffectProperty applicableEffectProperty = itemStack.getProperty(ApplicableEffectProperty.class).orElse(null);
if (applicableEffectProperty != null && !applicableEffectProperty.getValue().isEmpty()) {
final List<PotionEffect> potionEffects = player.get(Keys.POTION_EFFECTS).orElse(Collections.emptyList());
player.offer(Keys.POTION_EFFECTS, PotionEffectHelper.merge(potionEffects, applicableEffectProperty.getValue()));
}
if (this.consumer != null) {
this.consumer.apply(player, pipeline, context);
}
if (!player.get(Keys.GAME_MODE).orElse(GameModes.NOT_SET).equals(GameModes.CREATIVE)) {
final Slot slot = context.requireContext(ContextKeys.USED_SLOT);
slot.poll(1);
if (this.restItemSupplier != null) {
if (slot.peek().isPresent()) {
((LanternPlayer) player).getInventory().getMain().offer(this.restItemSupplier.get());
} else {
slot.set(this.restItemSupplier.get());
}
}
}
return BehaviorResult.SUCCESS;
}
return BehaviorResult.PASS;
}
Aggregations