Exemplo n.º 1
0
        private void ExplodeBlock(Block blockToExplode, BlockPos explosionPos, Vec3d shrapnelDirection)
        {
            AssetLocation blockCode = blockToExplode.Code;

            //-- Explosions do not destroy water, mantle or air blocks --//
            switch (blockCode.Path)
            {
            case "water-still-7":
                break;

            case "mantle":
                break;

            case "air":
                break;

            default:
                //-- If a block being destroyed is an inventory, then throw all the contents of it on the ground. Otherwise, for terrain blocks, only spawn items based on a % chance --//
                if (blockToExplode is BlockGenericTypedContainer || blockToExplode is BlockShelf || blockToExplode is BlockDisplayCase || blockToExplode is BlockMoldRack)
                {
                    BlockEntityContainer entityContainer = blockAccessor.GetBlockEntity(explosionPos) as BlockEntityContainer;

                    foreach (ItemSlot slot in entityContainer.Inventory)
                    {
                        if (!slot.Empty)
                        {
                            entity.World.SpawnItemEntity(slot.Itemstack, explosionPos.ToVec3d(), GetNewItemStackVector(shrapnelDirection, itemStackVelocityModifier));
                        }
                    }
                }
                else if (blockToExplode is BlockToolRack)
                {
                    BlockEntityToolrack blockEntityToolrack = blockAccessor.GetBlockEntity(explosionPos) as BlockEntityToolrack;

                    foreach (ItemSlot slot in blockEntityToolrack.inventory)
                    {
                        if (!slot.Empty)
                        {
                            entity.World.SpawnItemEntity(slot.Itemstack, explosionPos.ToVec3d(), GetNewItemStackVector(shrapnelDirection, itemStackVelocityModifier));
                        }
                    }
                }
                else
                {
                    foreach (BlockDropItemStack itemStack in blockAccessor.GetBlock(explosionPos).Drops)
                    {
                        if (explosionRand.Next(0, 101) < terrainDropChance)
                        {
                            entity.World.SpawnItemEntity(itemStack.GetNextItemStack(), explosionPos.ToVec3d(), GetNewItemStackVector(shrapnelDirection, itemStackVelocityModifier));
                        }
                    }
                }

                if (fillWithLiquid == false)
                {
                    blockAccessor.SetBlock(0, explosionPos);
                }
                else
                {
                    if (explosionPos.Y <= fillHeight)
                    {
                        blockAccessor.SetBlock(serverAPI.WorldManager.GetBlockId(liquidAsset), explosionPos);
                    }
                    else
                    {
                        blockAccessor.SetBlock(0, explosionPos);
                    }
                }

                blockAccessor.TriggerNeighbourBlockUpdate(explosionPos);
                break;
            }
        }
Exemplo n.º 2
0
        private bool SpillContents(ItemSlot containerSlot, EntityAgent byEntity, BlockSelection blockSel)
        {
            BlockPos       pos       = blockSel.Position;
            IPlayer        byPlayer  = (byEntity as EntityPlayer)?.Player;
            IBlockAccessor blockAcc  = byEntity.World.BlockAccessor;
            BlockPos       secondPos = blockSel.Position.AddCopy(blockSel.Face);


            WaterTightContainableProps props = GetContentProps(byEntity.World, containerSlot.Itemstack);

            if (props == null || !props.AllowSpill || props.WhenSpilled == null)
            {
                return(false);
            }

            if (!byEntity.World.Claims.TryAccess(byPlayer, secondPos, EnumBlockAccessFlags.BuildOrBreak))
            {
                return(false);
            }

            if (props.WhenSpilled.Action == WaterTightContainableProps.EnumSpilledAction.PlaceBlock)
            {
                Block waterBlock = byEntity.World.GetBlock(props.WhenSpilled.Stack.Code);

                if (props.WhenSpilled.StackByFillLevel != null)
                {
                    float         currentlitres  = GetCurrentLitres(byEntity.World, containerSlot.Itemstack);
                    JsonItemStack fillLevelStack = null;
                    props.WhenSpilled.StackByFillLevel.TryGetValue((int)currentlitres, out fillLevelStack);
                    if (fillLevelStack != null)
                    {
                        waterBlock = byEntity.World.GetBlock(fillLevelStack.Code);
                    }
                }

                Block currentblock = blockAcc.GetBlock(pos);
                if (currentblock.Replaceable >= 6000)
                {
                    blockAcc.SetBlock(waterBlock.BlockId, pos);
                    blockAcc.TriggerNeighbourBlockUpdate(pos);
                    blockAcc.MarkBlockDirty(pos);
                }
                else
                {
                    if (blockAcc.GetBlock(secondPos).Replaceable >= 6000)
                    {
                        blockAcc.SetBlock(waterBlock.BlockId, secondPos);
                        blockAcc.MarkBlockDirty(secondPos);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }

            if (props.WhenSpilled.Action == WaterTightContainableProps.EnumSpilledAction.DropContents)
            {
                props.WhenSpilled.Stack.Resolve(byEntity.World, "liquidcontainerbasespill");

                ItemStack stack = props.WhenSpilled.Stack.ResolvedItemstack.Clone();
                stack.StackSize = (int)(props.ItemsPerLitre * GetContent(byEntity.World, containerSlot.Itemstack).StackSize);

                byEntity.World.SpawnItemEntity(stack, blockSel.Position.ToVec3d().Add(blockSel.HitPosition));
            }


            ItemStack emptyContainerStack = new ItemStack(this);

            if (containerSlot.Itemstack.StackSize <= 1)
            {
                containerSlot.Itemstack = emptyContainerStack;
                containerSlot.MarkDirty();
            }
            else
            {
                containerSlot.TakeOut(1);
                if (!byPlayer.InventoryManager.TryGiveItemstack(emptyContainerStack, true))
                {
                    byEntity.World.SpawnItemEntity(emptyContainerStack, byEntity.SidedPos.XYZ);
                }
            }

            byEntity.World.PlaySoundAt(props.FillSpillSound, pos.X, pos.Y, pos.Z, byPlayer);
            return(true);
        }