Search in sources :

Example 1 with AlwaysConsumableProperty

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;
}
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)

Aggregations

LanternPlayer (org.lanternpowered.server.entity.living.player.LanternPlayer)1 AlwaysConsumableProperty (org.lanternpowered.server.item.property.AlwaysConsumableProperty)1 HealthRestorationProperty (org.lanternpowered.server.item.property.HealthRestorationProperty)1 FoodRestorationProperty (org.spongepowered.api.data.property.item.FoodRestorationProperty)1 Player (org.spongepowered.api.entity.living.player.Player)1 ItemStack (org.spongepowered.api.item.inventory.ItemStack)1