Search in sources :

Example 1 with HealthRestorationProperty

use of org.lanternpowered.server.item.property.HealthRestorationProperty 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;
}
Also used : LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer) Player(org.spongepowered.api.entity.living.player.Player) AlwaysConsumableProperty(org.lanternpowered.server.item.property.AlwaysConsumableProperty) HealthRestorationProperty(org.lanternpowered.server.item.property.HealthRestorationProperty) FoodRestorationProperty(org.spongepowered.api.data.property.item.FoodRestorationProperty) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 2 with HealthRestorationProperty

use of org.lanternpowered.server.item.property.HealthRestorationProperty 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

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