Пример #1
0
        public override void OnHeldInteractStop(float secondsUsed, IItemSlot slot, IEntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel)
        {
            if (secondsUsed > 0.7f && byEntity.World.Side == EnumAppSide.Server)
            {
                JsonObject attr   = slot.Itemstack.Collectible.Attributes;
                float      health = attr["health"].AsFloat();
                byEntity.ReceiveDamage(new DamageSource()
                {
                    Source = EnumDamageSource.Internal,
                    Type   = health > 0 ? EnumDamageType.Heal : EnumDamageType.Poison
                }, Math.Abs(health));

                slot.TakeOut(1);
            }
        }
Пример #2
0
        /// <summary>
        /// Called when the player successfully completed the using action, always called once an interaction is over
        /// </summary>
        /// <param name="secondsUsed"></param>
        /// <param name="slot"></param>
        /// <param name="byEntity"></param>
        /// <param name="blockSel"></param>
        /// <param name="entitySel"></param>
        public override void OnHeldInteractStop(float secondsUsed, IItemSlot slot, IEntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel)
        {
            FoodNutritionProperties[] multiProps = GetContentNutritionProperties(byEntity.World, slot.Itemstack, byEntity as Entity);

            if (byEntity.World.Side == EnumAppSide.Server && multiProps != null && secondsUsed >= 0.95f)
            {
                slot.TakeOut(1);
                slot.MarkDirty();
                IPlayer player = (byEntity as EntityPlayer).Player;

                Block block = byEntity.World.GetBlock(new AssetLocation("bowl-burned"));
                if (player == null || !player.InventoryManager.TryGiveItemstack(new ItemStack(block), true))
                {
                    byEntity.World.SpawnItemEntity(new ItemStack(block), byEntity.LocalPos.XYZ);
                }

                foreach (var nutriProps in multiProps)
                {
                    player.Entity.ReceiveSaturation(nutriProps.Saturation, nutriProps.FoodCategory, 10 + nutriProps.Saturation / 100f * 30f);

                    if (nutriProps.EatenStack?.ResolvedItemstack != null)
                    {
                        if (player == null || !player.InventoryManager.TryGiveItemstack(nutriProps.EatenStack.ResolvedItemstack.Clone(), true))
                        {
                            byEntity.World.SpawnItemEntity(nutriProps.EatenStack.ResolvedItemstack.Clone(), byEntity.LocalPos.XYZ);
                        }
                    }

                    if (nutriProps.Health != 0)
                    {
                        byEntity.ReceiveDamage(new DamageSource()
                        {
                            Source = EnumDamageSource.Internal,
                            Type   = nutriProps.Health > 0 ? EnumDamageType.Heal : EnumDamageType.Poison
                        }, Math.Abs(nutriProps.Health));
                    }
                }
            }
        }