コード例 #1
0
        public bool ReceiveDistillate(ItemSlot sourceSlot, DistillationProps props)
        {
            if (sourceSlot.Empty)
            {
                lastReceivedDistillateTotalMs = -99999;
                return(true);
            }
            if (inventory[1].Empty)
            {
                lastReceivedDistillateTotalMs = Api.World.ElapsedMilliseconds;
                lastReceivedDistillate        = props.DistilledStack.ResolvedItemstack.Clone();
                return(false);
            }

            ItemStack distilledStack = props.DistilledStack.ResolvedItemstack.Clone();

            lastReceivedDistillate = distilledStack.Clone();

            ItemStack bucketStack = inventory[1].Itemstack;
            BlockLiquidContainerTopOpened bucketBlock = bucketStack.Collectible as BlockLiquidContainerTopOpened;

            if (bucketBlock.IsEmpty(bucketStack))
            {
                if (Api.Side == EnumAppSide.Server)
                {
                    distilledStack.StackSize = 1;
                    bucketBlock.SetContent(bucketStack, distilledStack);
                }
            }
            else
            {
                ItemStack currentLiquidStack = bucketBlock.GetContent(bucketStack);
                if (!currentLiquidStack.Equals(Api.World, distilledStack, GlobalConstants.IgnoredStackAttributes))
                {
                    lastReceivedDistillateTotalMs = -99999;
                    return(false);
                }

                if (Api.Side == EnumAppSide.Server)
                {
                    if (!inventory[0].Empty || Api.World.Rand.NextDouble() > 0.5f) // Missing coolant reduces distillation efficeny by 50%
                    {
                        currentLiquidStack.StackSize++;
                        bucketBlock.SetContent(bucketStack, currentLiquidStack);
                    }

                    if (!inventory[0].Empty && Api.World.Rand.NextDouble() < 0.5f)
                    {
                        inventory[0].TakeOut(1);
                    }
                }
            }


            float itemsToRemove = 1 / props.Ratio - partialStackAccum;
            int   stackSize     = (int)Math.Ceiling(itemsToRemove);

            partialStackAccum = itemsToRemove - stackSize;

            if (stackSize > 0)
            {
                sourceSlot.TakeOut(stackSize);
            }

            MarkDirty(true);
            lastReceivedDistillateTotalMs = Api.World.ElapsedMilliseconds;

            return(true);
        }
コード例 #2
0
        public override void OnHeldInteractStop(float secondsUsed, ItemSlot slot, EntityAgent byEntity, BlockSelection blockSel, EntitySelection entitySel)
        {
            if (blockSel == null)
            {
                return;
            }
            if (secondsUsed < 1.9f)
            {
                return;
            }

            IWorldAccessor world = byEntity.World;

            Block block = byEntity.World.BlockAccessor.GetBlock(blockSel.Position);

            if (!CanSqueezeInto(block, blockSel.Position))
            {
                return;
            }

            ItemStack honeyStack = new ItemStack(world.GetItem(new AssetLocation("honeyportion")), 99999);

            BlockLiquidContainerTopOpened blockCnt = block as BlockLiquidContainerTopOpened;

            if (blockCnt != null)
            {
                if (blockCnt.TryPutLiquid(blockSel.Position, honeyStack, ContainedHoneyLitres) == 0)
                {
                    return;
                }
            }
            else
            {
                var beg = api.World.BlockAccessor.GetBlockEntity(blockSel.Position) as BlockEntityGroundStorage;
                if (beg != null)
                {
                    ItemSlot squeezeIntoSlot = beg.Inventory.FirstOrDefault(gslot => gslot.Itemstack?.Block != null && CanSqueezeInto(gslot.Itemstack.Block, null));
                    if (squeezeIntoSlot != null)
                    {
                        blockCnt = squeezeIntoSlot.Itemstack.Block as BlockLiquidContainerTopOpened;
                        blockCnt.TryPutLiquid(squeezeIntoSlot.Itemstack, honeyStack, ContainedHoneyLitres);
                        beg.MarkDirty(true);
                    }
                }
            }

            slot.TakeOut(1);
            slot.MarkDirty();

            IPlayer byPlayer = null;

            if (byEntity is EntityPlayer)
            {
                byPlayer = world.PlayerByUid(((EntityPlayer)byEntity).PlayerUID);
            }
            ItemStack stack = new ItemStack(world.GetItem(new AssetLocation("beeswax")));

            if (byPlayer?.InventoryManager.TryGiveItemstack(stack) == false)
            {
                byEntity.World.SpawnItemEntity(stack, byEntity.SidedPos.XYZ);
            }
        }