コード例 #1
0
        private void onTick100msServer(float dt)
        {
            if (MashSlot.Empty)
            {
                return;
            }

            var    juiceProps = getJuiceableProps(mashStack);
            double totalHours = Api.World.Calendar.TotalHours;

            double squeezeRel       = mashStack.Attributes.GetDouble("squeezeRel", 1);
            double litresToTransfer = Math.Min(juiceableLitresLeft, (totalHours - lastLiquidTransferTotalHours) * 50f);

            if (Api.Side == EnumAppSide.Server && squeezeRel < 1 && !squeezeSoundPlayed && juiceableLitresLeft > 0)
            {
                Api.World.PlaySoundAt(new AssetLocation("sounds/player/wetclothsqueeze.ogg"), Pos.X + 0.5, Pos.Y + 0.5, Pos.Z + 0.5, null, false);
                squeezeSoundPlayed = true;
            }

            if (Api.Side == EnumAppSide.Server && squeezeRel < 1 && totalHours - lastLiquidTransferTotalHours > 0.01)
            {
                ItemStack liquidStack = juiceProps.LiquidStack.ResolvedItemstack;
                liquidStack.StackSize = 999999;
                BlockLiquidContainerBase cntBlock = BucketSlot?.Itemstack?.Collectible as BlockLiquidContainerBase;

                if (cntBlock != null && litresToTransfer > 0)
                {
                    cntBlock.TryPutLiquid(BucketSlot.Itemstack, liquidStack, (float)litresToTransfer);
                }

                juiceableLitresLeft         -= (float)litresToTransfer;
                juiceableLitresTransfered   += (float)litresToTransfer;
                lastLiquidTransferTotalHours = totalHours;
                MarkDirty(true);
            }

            if (juiceableLitresLeft <= 0.01)
            {
                // Fugly hack to fix rounding errors
                BlockLiquidContainerBase cntBlock = BucketSlot?.Itemstack?.Collectible as BlockLiquidContainerBase;
                if (cntBlock != null)
                {
                    float litres = cntBlock.GetCurrentLitres(BucketSlot.Itemstack);
                    cntBlock.SetCurrentLitres(BucketSlot.Itemstack, (float)Math.Round(10 * litres) / 10f);
                }

                UnregisterGameTickListener(listenerId);
                listenerId = 0;

                int stacksize = GameMath.RoundRandom(Api.World.Rand, (float)juiceableLitresTransfered);
                mashStack.Attributes.RemoveAttribute("juiceableLitresTransfered");
                mashStack.Attributes.RemoveAttribute("juiceableLitresLeft");
                mashStack.StackSize = (int)(stacksize * juiceProps.PressedDryRatio);

                MarkDirty(true);
            }
        }