use of org.spongepowered.api.data.property.item.SaturationProperty in project SpongeCommon by SpongePowered.
the class SaturationPropertyStore method getFor.
@Override
protected Optional<SaturationProperty> getFor(ItemStack itemStack) {
if (itemStack.getItem() instanceof ItemFood) {
final ItemFood food = (ItemFood) itemStack.getItem();
// Translate's Minecraft's weird internal value to the actual saturation value
final double saturationModifier = food.getSaturationModifier(itemStack) * food.getHealAmount(itemStack) * 2.0;
return Optional.of(new SaturationProperty(saturationModifier));
}
return Optional.empty();
}
use of org.spongepowered.api.data.property.item.SaturationProperty 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