Vintagestory.GameContent.BEBehaviorFruiting.OnPlayerInteractStop C# (CSharp) Method

OnPlayerInteractStop() public method

public OnPlayerInteractStop ( float secondsUsed, IPlayer player, Vec3d hit ) : void
secondsUsed float
player IPlayer
hit Vec3d
return void
        public virtual void OnPlayerInteractStop(float secondsUsed, IPlayer player, Vec3d hit)
        {
            if (secondsUsed < 0.2f) return;

            for (int i = 0; i < fruitPoints.Length; i++)
            {
                FruitData val = fruitPoints[i];
                if (val.variant >= 0 && val.currentStage >= this.ripeStage)
                {
                    Item item = Api.World.GetItem(new AssetLocation(fruitCodeBases[val.variant] + val.currentStage));
                    if (item == null) continue;
                    if (item.Attributes != null && item.Attributes["onGround"].AsBool(false)) continue;  // ignore fruits already on the ground

                    if (Api.Side == EnumAppSide.Client) manager.RemoveFruit(fruitCodeBases[val.variant] + val.currentStage, positions[i]);
                    val.variant = -1;   // Flag this fruit as picked
                    val.transitionDate = Double.MaxValue;

                    if (val.currentStage >= this.ripeStage)  // allow both ripe and overripe fruits to be picked
                    {
                        // Play snickety sound
                        double posx = Blockentity.Pos.X + hit.X;
                        double posy = Blockentity.Pos.Y + hit.Y;
                        double posz = Blockentity.Pos.Z + hit.Z;
                        player.Entity.World.PlaySoundAt(new AssetLocation("sounds/effect/squish1"), posx, posy, posz, player, 1.1f + (float)Api.World.Rand.NextDouble() * 0.4f, 16, 0.25f);

                        // Update transition time of any non-started fruit, otherwise they may all start immediately
                        double now = Api.World.Calendar.TotalDays;
                        for (int j = 0; j < fruitPoints.Length; j++)
                        {
                            val = fruitPoints[j];
                            if (val.variant >= 0 && val.currentStage == 0 && val.transitionDate < now)
                            {
                                val.transitionDate = now + Api.World.Rand.NextDouble() * maxGerminationDays / 2;
                            }
                        }

                        // Give the player one item
                        ItemStack stack = new ItemStack(Api.World.GetItem(dropCode), 1);

                        if (!player.InventoryManager.TryGiveItemstack(stack))
                        {
                            Api.World.SpawnItemEntity(stack, Blockentity.Pos.ToVec3d().Add(0.5, 0.25, 0.5));
                        }
                    }
                    Blockentity.MarkDirty();
                    break;
                }
            }
        }
    }

Usage Example

 public override void OnBlockInteractStop(float secondsUsed, IWorldAccessor world, IPlayer byPlayer, BlockSelection blockSel)
 {
     BEBehaviorFruiting bef = world.BlockAccessor.GetBlockEntity(blockSel.Position)?.GetBehavior<BEBehaviorFruiting>();
     if (bef != null) bef.OnPlayerInteractStop(secondsUsed, byPlayer, blockSel.HitPosition);
 }