Search in sources :

Example 1 with SaturationProperty

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();
}
Also used : SaturationProperty(org.spongepowered.api.data.property.item.SaturationProperty) ItemFood(net.minecraft.item.ItemFood)

Example 2 with SaturationProperty

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;
}
Also used : SaturationProperty(org.spongepowered.api.data.property.item.SaturationProperty) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer) Player(org.spongepowered.api.entity.living.player.Player) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) FoodRestorationProperty(org.spongepowered.api.data.property.item.FoodRestorationProperty) HealthRestorationProperty(org.lanternpowered.server.item.property.HealthRestorationProperty) Slot(org.spongepowered.api.item.inventory.Slot) ApplicableEffectProperty(org.spongepowered.api.data.property.item.ApplicableEffectProperty) ItemStack(org.spongepowered.api.item.inventory.ItemStack) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Aggregations

SaturationProperty (org.spongepowered.api.data.property.item.SaturationProperty)2 ItemFood (net.minecraft.item.ItemFood)1 LanternPlayer (org.lanternpowered.server.entity.living.player.LanternPlayer)1 HealthRestorationProperty (org.lanternpowered.server.item.property.HealthRestorationProperty)1 ApplicableEffectProperty (org.spongepowered.api.data.property.item.ApplicableEffectProperty)1 FoodRestorationProperty (org.spongepowered.api.data.property.item.FoodRestorationProperty)1 PotionEffect (org.spongepowered.api.effect.potion.PotionEffect)1 Player (org.spongepowered.api.entity.living.player.Player)1 ItemStack (org.spongepowered.api.item.inventory.ItemStack)1 Slot (org.spongepowered.api.item.inventory.Slot)1